Commit 32760b3d authored by MOREAU Ulysse's avatar MOREAU Ulysse
Browse files

Factoring code to make tests easier

parent d9f77082
......@@ -267,6 +267,17 @@ class Order(models.Model):
super().save(*args, **kwargs)
def clean_illegal_items(self):
# No suppression if already ordered
if self.ordered:
return False
deleted_count = 0
for orderitem in self.items.all():
if not orderitem.is_available_atm:
orderitem.delete()
deleted_count += 1
return deleted_count
class Payment(models.Model):
mean = models.CharField(max_length=10, choices=settings.PAYMENT_MEANS)
......@@ -341,6 +352,10 @@ class OrderItem(models.Model):
def margin_deepness(self):
return self.pack_deepness * 50 + 10
@property
def is_available_atm(self):
return (not self.item.hidden) or orderitem.item.end_date > timezone.now()
def get_price(self) -> Decimal:
return self.quantity * self.item.price
......
......@@ -312,12 +312,8 @@ class OrderEditView(LoginRequiredMixin, UpdateView):
""" Delete illegal objects prior to display
"""
super().setup(request, *args, **kwargs)
failed = False
for orderitem in self.get_object().items.all():
if orderitem.item.hidden or orderitem.item.end_date < timezone.now():
orderitem.delete()
failed = True
if failed:
if self.get_object().clean_illegal_items():
messages.error(self.request, "Certains objets sont périmés ou ont été cachés par un administrateur. Ils ont été supprimé du panier.")
def get_form_class(self) -> Type[BaseInlineFormSet]: # type: ignore[override] # override not supported
......@@ -388,12 +384,7 @@ class OrderSendView(LoginRequiredMixin, RedirectView, SingleObjectMixin):
self.order = self.get_object()
# Check if all items are legal
failed = False
for orderitem in self.order.items.all():
if orderitem.item.hidden or orderitem.item.end_date < timezone.now():
orderitem.delete()
failed = True
if failed:
if self.get_object().clean_illegal_items():
messages.error(self.request, "Certains objets sont périmés ou ont été cachés par un administrateur. Ils ont été supprimé du panier, et la commande n'a pas été passée.")
return reverse("boutique:order_edit")
elif self.order.items.count() < 1:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment