diff --git a/bornhack/settings/base.py b/bornhack/settings/base.py index 386cbaa8..cc81d94d 100644 --- a/bornhack/settings/base.py +++ b/bornhack/settings/base.py @@ -98,3 +98,5 @@ BOOTSTRAP3 = { EPAY_MERCHANT_NUMBER = env('EPAY_MERCHANT_NUMBER') EPAY_MD5_SECRET = env('EPAY_MD5_SECRET') + +TICKET_CATEGORY_ID = env('TICKET_CATEGORY_ID') \ No newline at end of file diff --git a/bornhack/settings/env.dist b/bornhack/settings/env.dist index dea3f432..a3bc5d31 100644 --- a/bornhack/settings/env.dist +++ b/bornhack/settings/env.dist @@ -9,3 +9,4 @@ EMAIL_USE_TLS=True EMAIL_FROM='noreply@example.com' EPAY_MERCHANT_NUMBER=something EPAY_MD5_SECRET=something +TICKET_CATEGORY_ID='' \ No newline at end of file diff --git a/shop/admin.py b/shop/admin.py index 8e3054f6..75795a2f 100644 --- a/shop/admin.py +++ b/shop/admin.py @@ -27,6 +27,13 @@ class ProductInline(admin.TabularInline): model = models.OrderProductRelation +class TicketInline(admin.TabularInline): + model = models.Ticket + exclude = ['qrcode_base64'] + + + + @admin.register(models.Order) class OrderAdmin(admin.ModelAdmin): list_display = [ @@ -45,7 +52,14 @@ class OrderAdmin(admin.ModelAdmin): exclude = ['products'] - inlines = [ProductInline] + inlines = [ProductInline, TicketInline] + + actions = ['mark_order_as_paid'] + + def mark_order_as_paid(self, request, queryset): + for order in queryset.filter(paid=False): + order.mark_as_paid() + mark_order_as_paid.description = 'Mark order(s) as paid' @admin.register(models.Ticket) diff --git a/shop/models.py b/shop/models.py index 53daf494..9d3323fa 100644 --- a/shop/models.py +++ b/shop/models.py @@ -105,6 +105,21 @@ class Order(CreatedUpdatedModel): def get_absolute_url(self): return str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk})) + def mark_as_paid(self): + self.paid = True + for order_product in self.orderproductrelation_set.all(): + category_pk = str(order_product.product.category.pk) + print(order_product, category_pk, settings.TICKET_CATEGORY_ID) + if category_pk == settings.TICKET_CATEGORY_ID: + for _ in range(0, order_product.quantity): + ticket = Ticket( + order=self, + product=order_product.product, + ) + ticket.save() + self.save() + + class ProductCategory(CreatedUpdatedModel, UUIDModel): class Meta: