Skip to content

Commit

Permalink
fixed formatting again
Browse files Browse the repository at this point in the history
  • Loading branch information
Mast3rwaf1z committed Apr 26, 2024
1 parent d33ad68 commit 93fe1e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 100 deletions.
14 changes: 3 additions & 11 deletions stregsystem/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,17 @@ def send_csv_mail(member):
payments: list[Payment] = member.payment_set.order_by("-timestamp")

sales_csv = "Name, Price, Timestamp"
sales_csv += "\n".join(
[f"{sale.product.name},{sale.price},{sale.timestamp}" for sale in sales]
)
sales_csv += "\n".join([f"{sale.product.name},{sale.price},{sale.timestamp}" for sale in sales])
payments_csv = "Timestamp, Amount"
payments_csv += "\n".join(
[f"{payment.timestamp},{payment.amount}" for payment in payments]
)
payments_csv += "\n".join([f"{payment.timestamp},{payment.amount}" for payment in payments])
userdata_csv = "Id, Name, First name, Last name, Email, Registration year"
userdata_csv += f"{member.id},{member.username},{member.firstname},{member.lastname},{member.email},{member.year}"

msg = MIMEMultipart()
msg["From"] = "[email protected]"
msg["To"] = member.email
msg["Subject"] = f"[TREO] {member.username} user data request"
msg.attach(
MIMEText(
render_to_string("mail/send_csv.html", {"fember": member.username}), "html"
)
)
msg.attach(MIMEText(render_to_string("mail/send_csv.html", {"fember": member.username}), "html"))
for name, csv in {
"sales": sales_csv,
"payments": payments_csv,
Expand Down
118 changes: 29 additions & 89 deletions stregsystem/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,13 @@
def __get_news():
try:
current_time = timezone.now()
return (
News.objects.filter(stop_date__gte=current_time, pub_date__lte=current_time)
.order_by("?")
.first()
)
return News.objects.filter(stop_date__gte=current_time, pub_date__lte=current_time).order_by("?").first()
except News.DoesNotExist:
return None


def __get_productlist(room_id):
return make_active_productlist_query(Product.objects).filter(
make_room_specific_query(room_id)
)
return make_active_productlist_query(Product.objects).filter(make_room_specific_query(room_id))


def roomindex(request):
Expand All @@ -101,9 +95,7 @@ def _pre_process(buy_string):

for item in items[1:]:
if type(item) is not int:
_item = NamedProduct.objects.filter(
name=item.split(":")[0].lower() if ":" in item else item
)
_item = NamedProduct.objects.filter(name=item.split(":")[0].lower() if ":" in item else item)
if _item:
item = item.replace(item.split(":")[0], str(_item.get().product.pk))
_items.append(str(item))
Expand Down Expand Up @@ -148,12 +140,8 @@ def _multibuy_hint(now, member):
# Get a timestamp to fetch sales for the member for the last 60 sec
earliest_recent_purchase = now - datetime.timedelta(seconds=60)
# get the sales with this timestamp
recent_purchases = Sale.objects.filter(
member=member, timestamp__gt=earliest_recent_purchase
)
number_of_recent_distinct_purchases = (
recent_purchases.values("timestamp").distinct().count()
)
recent_purchases = Sale.objects.filter(member=member, timestamp__gt=earliest_recent_purchase)
number_of_recent_distinct_purchases = recent_purchases.values("timestamp").distinct().count()

# add hint for multibuy
if number_of_recent_distinct_purchases > 1:
Expand Down Expand Up @@ -181,9 +169,7 @@ def quicksale(request, room, member: Member, bought_ids):

# Retrieve products and construct transaction
products: List[Product] = []
msg, status, result = __append_bought_ids_to_product_list(
products, bought_ids, now, room
)
msg, status, result = __append_bought_ids_to_product_list(products, bought_ids, now, room)
if status == 400:
return render(
request,
Expand All @@ -197,9 +183,7 @@ def quicksale(request, room, member: Member, bought_ids):
if "Out of stock" in msg:
return render(request, "stregsystem/error_stregforbud.html", locals())
elif "Stregforbud" in msg:
return render(
request, "stregsystem/error_stregforbud.html", locals(), status=402
)
return render(request, "stregsystem/error_stregforbud.html", locals(), status=402)

(
promille,
Expand Down Expand Up @@ -238,9 +222,7 @@ def usermenu(request, room, member, bought, from_sale=False):
give_multibuy_hint, sale_hints = _multibuy_hint(timezone.now(), member)
give_multibuy_hint = give_multibuy_hint and from_sale

heatmap_context = prepare_heatmap_template_context(
member, 12, datetime.date.today()
)
heatmap_context = prepare_heatmap_template_context(member, 12, datetime.date.today())

if member.has_stregforbud():
return render(request, "stregsystem/error_stregforbud.html", locals())
Expand Down Expand Up @@ -322,9 +304,9 @@ def ranking(category_ids, from_d, to_d):

def get_product_ids_for_category(category) -> list:
return list(
Product.objects.filter(
categories__exact=Category.objects.get(name__exact=category)
).values_list("id", flat=True)
Product.objects.filter(categories__exact=Category.objects.get(name__exact=category)).values_list(
"id", flat=True
)
)

def category_per_uni_day(category_ids, from_d, to_d):
Expand All @@ -337,9 +319,7 @@ def category_per_uni_day(category_ids, from_d, to_d):
if member not in qs:
return 0
else:
return "{:.2f}".format(
qs.count() / ((to_d - from_d).days * 162.14 / 365)
) # university workdays in 2021
return "{:.2f}".format(qs.count() / ((to_d - from_d).days * 162.14 / 365)) # university workdays in 2021

# let user know when they first purchased a product
member_first_purchase = "Ikke endnu, køb en limfjordsporter!"
Expand Down Expand Up @@ -395,8 +375,7 @@ def menu_sale(request, room_id, member_id, product_id=None):
Q(pk=purchase.cleaned_data["product_id"]),
Q(active=True),
Q(rooms__id=room_id) | Q(rooms=None),
Q(deactivate_date__gte=timezone.now())
| Q(deactivate_date__isnull=True),
Q(deactivate_date__gte=timezone.now()) | Q(deactivate_date__isnull=True),
)

order = Order.from_products(member=member, room=room, products=(product,))
Expand Down Expand Up @@ -475,14 +454,8 @@ def mobilepaytool(request):
# TODO-cont: have that information at this point in time - add back 'customer_name' if available in future
data = dict()
if request.method == "GET":
data["formset"] = paytool_form_set(
queryset=make_unprocessed_mobilepayment_query()
)
elif (
request.method == "POST"
and "csv_file" in request.FILES
and request.POST["action"] == "Import MobilePay CSV"
):
data["formset"] = paytool_form_set(queryset=make_unprocessed_mobilepayment_query())
elif request.method == "POST" and "csv_file" in request.FILES and request.POST["action"] == "Import MobilePay CSV":
# Prepare uploaded CSV to be read
csv_file = request.FILES["csv_file"]
csv_file.seek(0)
Expand All @@ -492,66 +465,44 @@ def mobilepaytool(request):
)

# refresh form after submission
data["formset"] = paytool_form_set(
queryset=make_unprocessed_mobilepayment_query()
)
data["formset"] = paytool_form_set(queryset=make_unprocessed_mobilepayment_query())

elif (
request.method == "POST"
and request.POST["action"] == "Import via MobilePay API"
):
elif request.method == "POST" and request.POST["action"] == "Import via MobilePay API":
before_count = MobilePayment.objects.count()
management.call_command("importmobilepaypayments")
count = MobilePayment.objects.count() - before_count

data["api"] = f"Successfully imported {count} MobilePay transactions"
data["formset"] = paytool_form_set(
queryset=make_unprocessed_mobilepayment_query()
)
data["formset"] = paytool_form_set(queryset=make_unprocessed_mobilepayment_query())

elif (
request.method == "POST" and request.POST["action"] == "Submit matched payments"
):
before_count = MobilePayment.objects.filter(
status=MobilePayment.APPROVED
).count()
elif request.method == "POST" and request.POST["action"] == "Submit matched payments":
before_count = MobilePayment.objects.filter(status=MobilePayment.APPROVED).count()
MobilePayment.approve_member_filled_mobile_payments()
MobilePayment.submit_processed_mobile_payments(request.user)
count = (
MobilePayment.objects.filter(status=MobilePayment.APPROVED).count()
- before_count
)
count = MobilePayment.objects.filter(status=MobilePayment.APPROVED).count() - before_count

data["submitted_count"] = count
data["formset"] = paytool_form_set(
queryset=make_unprocessed_mobilepayment_query()
)
data["formset"] = paytool_form_set(queryset=make_unprocessed_mobilepayment_query())

elif request.method == "POST" and request.POST["action"] == "Submit payments":
form = paytool_form_set(request.POST)

if form.is_valid():
try:
# Do custom validation on form to avoid race conditions with autopayment
count = MobilePayment.process_submitted_mobile_payments(
form.cleaned_data, request.user
)
count = MobilePayment.process_submitted_mobile_payments(form.cleaned_data, request.user)
data["submitted_count"] = count
except MobilePaytoolException as e:
data["error_count"] = e.inconsistent_mbpayments_count
data["error_transaction_ids"] = e.inconsistent_transaction_ids

# refresh form after submission
data["formset"] = paytool_form_set(
queryset=make_unprocessed_mobilepayment_query()
)
data["formset"] = paytool_form_set(queryset=make_unprocessed_mobilepayment_query())
else:
# update form with errors
data["formset"] = form
else:
data["formset"] = paytool_form_set(
queryset=make_unprocessed_mobilepayment_query()
)
data["formset"] = paytool_form_set(queryset=make_unprocessed_mobilepayment_query())

return render(request, "admin/stregsystem/mobilepaytool.html", data)

Expand Down Expand Up @@ -609,12 +560,7 @@ def convert_username_to_id(request):


def dump_product_category_mappings(request):
return JsonResponse(
{
p.id: [(cat.id, cat.name) for cat in p.categories.all()]
for p in Product.objects.all()
}
)
return JsonResponse({p.id: [(cat.id, cat.name) for cat in p.categories.all()] for p in Product.objects.all()})


def get_user_sales(request):
Expand All @@ -623,9 +569,7 @@ def get_user_sales(request):
return HttpResponseBadRequest("Missing member_id")
elif not member_id.isdigit():
return HttpResponseBadRequest("Invalid member_id")
count = (
10 if request.GET.get("count") is None else int(request.GET.get("count") or 10)
)
count = 10 if request.GET.get("count") is None else int(request.GET.get("count") or 10)
sales = Sale.objects.filter(member=member_id).order_by("-timestamp")[:count]
return JsonResponse(
{
Expand Down Expand Up @@ -734,9 +678,7 @@ def api_quicksale(request, room, member: Member, bought_ids):
# Retrieve products and construct transaction
products: List[Product] = []

msg, status, result = __append_bought_ids_to_product_list(
products, bought_ids, now, room
)
msg, status, result = __append_bought_ids_to_product_list(products, bought_ids, now, room)
if status == 400:
return msg, status, result

Expand Down Expand Up @@ -821,9 +763,7 @@ def __set_local_values(member, room, products, order, now):

caffeine = member.calculate_caffeine_in_body()
cups = caffeine_mg_to_coffee_cups(caffeine)
product_contains_caffeine = any(
product.caffeine_content_mg > 0 for product in products
)
product_contains_caffeine = any(product.caffeine_content_mg > 0 for product in products)
is_coffee_master = member.is_leading_coffee_addict()

cost = order.total
Expand Down

0 comments on commit 93fe1e7

Please sign in to comment.