Add a safety valve for creating tickets when marking an order as paid. Fix #136

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-17 16:28:01 +01:00
parent 512be60da5
commit 8fb9e60601

View file

@ -16,7 +16,6 @@ from unidecode import unidecode
from django.utils.dateparse import parse_datetime from django.utils.dateparse import parse_datetime
from utils.models import UUIDModel, CreatedUpdatedModel from utils.models import UUIDModel, CreatedUpdatedModel
from tickets.models import ShopTicket
from .managers import ProductQuerySet, OrderQuerySet from .managers import ProductQuerySet, OrderQuerySet
logger = logging.getLogger("bornhack.%s" % __name__) logger = logging.getLogger("bornhack.%s" % __name__)
@ -193,43 +192,73 @@ class Order(CreatedUpdatedModel):
def get_absolute_url(self): def get_absolute_url(self):
return str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk})) return str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk}))
def mark_as_paid(self, request): def create_tickets(self, request=None):
self.paid = True
self.open = None
for order_product in self.orderproductrelation_set.all(): for order_product in self.orderproductrelation_set.all():
# if this is a Ticket product? # if this is a Ticket product?
if order_product.product.ticket_type: if order_product.product.ticket_type:
query_kwargs = dict(
product=order_product.product,
ticket_type=order_product.product.ticket_type,
)
already_created_tickets = self.shoptickets.filter(**query_kwargs).count()
tickets_to_create = max(0, order_product.quantity - already_created_tickets)
# create the number of tickets required # create the number of tickets required
for _ in range(0, order_product.quantity): if tickets_to_create > 0:
ticket = ShopTicket( for _ in range(0, (order_product.quantity - already_created_tickets)):
ticket_type=order_product.product.ticket_type, self.shoptickets.create(
order=self, **query_kwargs
product=order_product.product, )
)
ticket.save() msg = "Created %s tickets of type: %s" % (order_product.quantity, order_product.product.ticket_type.name)
if request: if request:
messages.success(request, "Created %s tickets of type: %s" % (order_product.quantity, order_product.product.ticket_type.name)) messages.success(request, msg)
# and mark the OPR as handed_out=True else:
order_product.handed_out = True print(msg)
order_product.save()
# and mark the OPR as handed_out=True
order_product.handed_out = True
order_product.save()
def mark_as_paid(self, request=None):
self.paid = True
self.open = None
self.create_tickets(request)
self.save() self.save()
def mark_as_refunded(self, request): def mark_as_refunded(self, request=None):
if not self.paid: if not self.paid:
messages.error(request, "Order %s is not paid, so cannot mark it as refunded!" % self.pk) msg = "Order %s is not paid, so cannot mark it as refunded!" % self.pk
if request:
messages.error(request, msg)
else:
print(msg)
else: else:
self.refunded = True self.refunded = True
# delete any tickets related to this order # delete any tickets related to this order
if self.tickets.all(): if self.shoptickets.all():
messages.success(request, "Order %s marked as refunded, deleting %s tickets..." % (self.pk, self.tickets.count())) msg = "Order %s marked as refunded, deleting %s tickets..." % (self.pk, self.shoptickets.count())
self.tickets.all().delete() if request:
messages.success(request, msg)
else:
print(msg)
self.shoptickets.all().delete()
else: else:
messages.success(request, "Order %s marked as refunded, no tickets to delete" % self.pk) msg = "Order %s marked as refunded, no tickets to delete" % self.pk
if request:
messages.success(request, msg)
else:
print(msg)
self.save() self.save()
def mark_as_cancelled(self, request): def mark_as_cancelled(self, request=None):
if self.paid: if self.paid:
messages.error(request, "Order %s is paid, cannot cancel a paid order!" % self.pk) msg = "Order %s is paid, cannot cancel a paid order!" % self.pk
if request:
messages.error(request, msg)
else:
print(msg)
else: else:
self.cancelled = True self.cancelled = True
self.open = None self.open = None