diff --git a/stregsystem/models.py b/stregsystem/models.py index 1cb7fed2..168d1d2b 100644 --- a/stregsystem/models.py +++ b/stregsystem/models.py @@ -129,15 +129,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 0468daca..c4558f7f 100644 --- a/stregsystem/views.py +++ b/stregsystem/views.py @@ -163,19 +163,18 @@ def sale(request, room_id): 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) + earliest_recent_sale = 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_sales = Sale.objects.filter(member=member, timestamp__gt=earliest_recent_sale) + number_of_recent_distinct_sales = recent_sales.values('timestamp').distinct().count() + recent_unique_sales = recent_sales.values('product').distinct().annotate(total=Count('product')) # add hint for multibuy - if number_of_recent_distinct_purchases > 1: + if number_of_recent_distinct_sales > 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_sales: + 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) @@ -889,16 +888,25 @@ 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 unique_id not in unique_product_dict: + unique_product_dict[unique_id] = 1 + else: + unique_product_dict[unique_id] += 1 + + # Add the given amount of different products + for key, value in unique_product_dict.items(): product = Product.objects.get( - Q(pk=i), + Q(pk=key), 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 _ in range(value)]) except Product.DoesNotExist: - return "Invalid product id", 400, i + return "Invalid product id", 400, key return "OK", 200, None