From a8c814cd1da3730d325dbac1c221bc3a7a70c978 Mon Sep 17 00:00:00 2001 From: Patrick Reiffenstein Date: Wed, 12 Jun 2024 01:23:06 +0200 Subject: [PATCH 1/3] Optimized multibuy hint --- stregsystem/views.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stregsystem/views.py b/stregsystem/views.py index dcc48afc..b53109b7 100644 --- a/stregsystem/views.py +++ b/stregsystem/views.py @@ -24,7 +24,6 @@ from django_select2 import forms as s2forms import urllib.parse - from stregsystem import parser from stregsystem.models import ( Member, @@ -141,14 +140,15 @@ def _multibuy_hint(now, member): recent_purchases = Sale.objects.filter(member=member, timestamp__gt=earliest_recent_purchase) number_of_recent_distinct_purchases = recent_purchases.values('timestamp').distinct().count() + recent_unique_purchases = recent_purchases.values('product').distinct().annotate(total=Count('product')) + print("TEST TING" + str(recent_unique_purchases)) + # add hint for multibuy if number_of_recent_distinct_purchases > 1: sale_dict = {} - for sale in recent_purchases: - if not str(sale.product.id) in sale_dict: - sale_dict[str(sale.product.id)] = 1 - else: - sale_dict[str(sale.product.id)] = sale_dict[str(sale.product.id)] + 1 + for unique_sale in recent_unique_purchases: + sale_dict[str(unique_sale['product'])] = unique_sale['total'] + sale_hints = ["{}".format(member.username)] if all(sale_count == 1 for sale_count in sale_dict.values()): return (False, None) From 06fee8d59e83b47d065f414b0923796b272e87c2 Mon Sep 17 00:00:00 2001 From: Patrick Reiffenstein Date: Wed, 12 Jun 2024 19:35:59 +0200 Subject: [PATCH 2/3] Removed duplicate queries --- stregsystem/models.py | 10 +++++----- stregsystem/views.py | 13 +++++++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/stregsystem/models.py b/stregsystem/models.py index afc9d428..5b103742 100644 --- a/stregsystem/models.py +++ b/stregsystem/models.py @@ -128,15 +128,15 @@ def execute(self): self.member = Member.objects.select_for_update().get(id=self.member.id) self.member.fulfill(transaction) + # Collect all the sales of the order + Sales = [] for item in self.items: - # @HACK Since we want to use the old database layout, we need to - # add a sale for every item and every instance of that item for i in range(item.count): s = Sale(member=self.member, product=item.product, room=self.room, price=item.product.price) - s.save() + Sales.append(s) + # Save all the sales + Sale.objects.bulk_create(Sales) - # Bought (used above) is automatically calculated, so we don't need - # to update it # We changed the user balance, so save that self.member.save() diff --git a/stregsystem/views.py b/stregsystem/views.py index b53109b7..4eacdcba 100644 --- a/stregsystem/views.py +++ b/stregsystem/views.py @@ -715,14 +715,23 @@ def api_quicksale(request, room, member: Member, bought_ids): def __append_bought_ids_to_product_list(products, bought_ids, time_now, room): try: - for i in bought_ids: + # Get the amount of unique items bought + unique_product_dict = {} + for unique_id in bought_ids: + if str(unique_id) not in unique_product_dict: + unique_product_dict[str(unique_id)] = 1 + else: + unique_product_dict[str(unique_id)] += 1 + + # Add the given amount of different products + for i in unique_product_dict: product = Product.objects.get( Q(pk=i), Q(active=True), Q(deactivate_date__gte=time_now) | Q(deactivate_date__isnull=True), Q(rooms__id=room.id) | Q(rooms=None), ) - products.append(product) + products.extend([product for x in range(unique_product_dict[str(i)])]) except Product.DoesNotExist: return "Invalid product id", 400, i return "OK", 200, None From e03da44c14c103096aea1c3a7a2a39a9c0d9e37a Mon Sep 17 00:00:00 2001 From: Patrick Reiffenstein Date: Wed, 12 Jun 2024 19:49:08 +0200 Subject: [PATCH 3/3] Removed debugging I left behind --- stregsystem/views.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/stregsystem/views.py b/stregsystem/views.py index 4eacdcba..5fd1e02f 100644 --- a/stregsystem/views.py +++ b/stregsystem/views.py @@ -139,9 +139,7 @@ def _multibuy_hint(now, member): # 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_unique_purchases = recent_purchases.values('product').distinct().annotate(total=Count('product')) - print("TEST TING" + str(recent_unique_purchases)) # add hint for multibuy if number_of_recent_distinct_purchases > 1: