Commit d31a1c7e authored by MOREAU Ulysse's avatar MOREAU Ulysse

Merge branch 'fix-issue-46-illegal-baskets' into 'master'

Delete illegal products from basket

See merge request !56
parents 22f0e658 3ed2dc11
......@@ -112,6 +112,22 @@ class Item(models.Model):
return True
return False
@property
def is_available_atm(self) -> bool:
return (not self.hidden) and self.end_date > timezone.now()
@property
def contains_illegal_items(self) -> bool:
# I'm commenting this out, beacause it could be useful to
# put illegal items in a legal pack. It could get chaged
# in a future update
# if self.is_pack:
# for child in self.children.all():
# if child.contains_illegal_items:
# return True
return not self.is_available_atm
def no_action(self, orderitem: "OrderItem") -> None:
pass
......@@ -209,6 +225,10 @@ class Item(models.Model):
option: str = "",
second_option: str = "",
) -> "OrderItem":
if self.hidden or self.end_date < timezone.now():
raise ValidationError(
"Ce produit est périmé ou a été caché par un administrateur. Il n'a pas été ajouté au panier."
)
order = Order.objects.get_current_order(user)
return self.add_to_order(order, quantity, customization, option, second_option)
......@@ -265,6 +285,17 @@ class Order(models.Model):
super().save(*args, **kwargs)
def clean_illegal_items(self) -> int:
# No suppression if already ordered
if self.ordered:
return 0
deleted_count = 0
for orderitem in self.items.filter(pack_parent=None):
if orderitem.item.contains_illegal_items:
orderitem.delete()
deleted_count += 1
return deleted_count
class Payment(models.Model):
mean = models.CharField(max_length=10, choices=settings.PAYMENT_MEANS)
......
......@@ -4,6 +4,7 @@ from unittest.mock import Mock
from unittest.mock import patch
from django.templatetags.static import static
from django.utils import timezone as django_timezone
from . import CategoryTestCase
from . import OrderItemTestCase
......@@ -722,3 +723,167 @@ class OrderTest(PaymentTestCase, OrderItemTestCase):
with self.assertRaises(ValueError):
self.order.delete()
def test_delete_illegal_items_on_ordered_order(self) -> None:
hidden_item = self.create_item()
ood_item = self.create_item()
pack_with_illegal_children = self.create_pack()
legal_item = self.create_item()
illegal_child_legal_pack = self.create_item()
legal_child_legal_pack = self.create_item()
illegal_pack_with_children = self.create_pack()
illegal_child_illegal_pack = self.create_item()
legal_child_illegal_pack = self.create_item()
illegal_pack_with_children.hidden = True
illegal_child_illegal_pack.hidden = True
illegal_pack_with_children.children.add(legal_child_illegal_pack)
illegal_pack_with_children.children.add(illegal_child_illegal_pack)
illegal_pack_with_children.save()
illegal_pack_with_children.refresh_from_db()
illegal_child_legal_pack.hidden = True
illegal_child_legal_pack.save()
illegal_child_legal_pack.refresh_from_db()
legal_child_legal_pack.save()
legal_child_legal_pack.refresh_from_db()
hidden_item.hidden = True
hidden_item.save()
hidden_item.refresh_from_db()
# YES, this would have failed anywhere near the year 1970
ood_item.end_date = datetime.datetime(
django_timezone.now().year - 2, 1, 1, 0, 0
)
ood_item.save()
ood_item.refresh_from_db()
legal_item.save()
legal_item.refresh_from_db()
pack_with_illegal_children.children.add(illegal_child_legal_pack)
pack_with_illegal_children.children.add(legal_child_legal_pack)
pack_with_illegal_children.save()
pack_with_illegal_children.refresh_from_db()
hidden_item.add_to_order(self.order)
ood_item.add_to_order(self.order)
legal_item.add_to_order(self.order)
pack_with_illegal_children.add_to_order(self.order)
illegal_pack_with_children.add_to_order(self.order)
self.order.ordered = True
self.order.save()
self.order.refresh_from_db()
self.assertTrue(self.order.ordered)
self.assertTrue(hidden_item.contains_illegal_items)
self.assertTrue(ood_item.contains_illegal_items)
self.assertTrue(illegal_pack_with_children.contains_illegal_items)
# You could have illegal items in a legal pack, this should still be valid
# e.g you could hide an item outside of a pack
self.assertFalse(pack_with_illegal_children.contains_illegal_items)
self.assertFalse(legal_item.contains_illegal_items)
previous_items_in_order = [oi.item for oi in self.order.items.all()]
deleted_count = self.order.clean_illegal_items()
self.assertEqual(deleted_count, 0)
self.order.save()
self.order.refresh_from_db()
items_in_order = [oi.item for oi in self.order.items.all()]
self.assertEqual(previous_items_in_order, items_in_order)
def test_delete_illegal_items_on_unordered_order(self) -> None:
hidden_item = self.create_item()
ood_item = self.create_item()
legal_item = self.create_item()
pack_with_illegal_children = self.create_pack()
illegal_child_legal_pack = self.create_item()
legal_child_legal_pack = self.create_item()
illegal_pack_with_children = self.create_pack()
illegal_child_illegal_pack = self.create_item()
legal_child_illegal_pack = self.create_item()
# YES, this would have failed anywhere near the year 1970
ood_item.end_date = datetime.datetime(
django_timezone.now().year - 2, 1, 1, 0, 0
)
hidden_item.hidden = True
illegal_pack_with_children.hidden = True
illegal_child_illegal_pack.hidden = True
illegal_child_legal_pack.hidden = True
hidden_item.save()
ood_item.save()
legal_item.save()
illegal_child_legal_pack.save()
legal_child_legal_pack.save()
illegal_child_illegal_pack.save()
legal_child_illegal_pack.save()
hidden_item.refresh_from_db()
ood_item.refresh_from_db()
legal_item.refresh_from_db()
illegal_child_legal_pack.refresh_from_db()
legal_child_legal_pack.refresh_from_db()
illegal_child_illegal_pack.refresh_from_db()
legal_child_illegal_pack.refresh_from_db()
illegal_pack_with_children.children.add(legal_child_illegal_pack)
illegal_pack_with_children.children.add(illegal_child_illegal_pack)
illegal_pack_with_children.save()
illegal_pack_with_children.refresh_from_db()
pack_with_illegal_children.children.add(illegal_child_legal_pack)
pack_with_illegal_children.children.add(legal_child_legal_pack)
pack_with_illegal_children.save()
pack_with_illegal_children.refresh_from_db()
hidden_item.add_to_order(self.order)
ood_item.add_to_order(self.order)
legal_item.add_to_order(self.order)
pack_with_illegal_children.add_to_order(self.order)
illegal_pack_with_children.add_to_order(self.order)
self.order.save()
self.order.refresh_from_db()
self.assertFalse(self.order.ordered)
self.assertTrue(hidden_item.contains_illegal_items)
self.assertTrue(ood_item.contains_illegal_items)
self.assertTrue(illegal_pack_with_children.contains_illegal_items)
# You could have illegal items in a legal pack, this should still be valid
# e.g you could hide an item outside of a pack
self.assertFalse(pack_with_illegal_children.contains_illegal_items)
self.assertFalse(legal_item.contains_illegal_items)
items_in_order = [oi.item for oi in self.order.items.all()]
self.assertTrue(hidden_item in items_in_order)
self.assertTrue(ood_item in items_in_order)
self.assertTrue(illegal_pack_with_children in items_in_order)
self.assertTrue(pack_with_illegal_children in items_in_order)
self.assertTrue(legal_item in items_in_order)
deleted_count = self.order.clean_illegal_items()
self.assertEqual(deleted_count, 3)
self.order.save()
self.order.refresh_from_db()
items_in_order = [oi.item for oi in self.order.items.all()]
self.assertFalse(hidden_item in items_in_order)
self.assertFalse(ood_item in items_in_order)
self.assertFalse(illegal_pack_with_children in items_in_order)
self.assertTrue(pack_with_illegal_children in items_in_order)
self.assertTrue(legal_item in items_in_order)
......@@ -13,7 +13,11 @@ urlpatterns = [
path("create", views.ItemCreateView.as_view(), name="item_create"),
path("<int:pk>", views.ItemDetailView.as_view(), name="item_detail"),
path("manage", views.ItemManageView.as_view(), name="item_manage"),
path("csvexport/<int:pk>", views.CsvExportView.as_view(), name="csvexport"),
path(
"csvexport/<int:pk>",
views.CsvExportView.as_view(),
name="csvexport",
),
path("sell", views.ItemSellView.as_view(), name="item_sell"),
path("edit/<int:pk>", views.ItemEditView.as_view(), name="item_edit"),
]
......
......@@ -173,19 +173,36 @@ class CsvExportView(PermissionRequiredMixin, SingleObjectMixin, View):
"order_items__order__payment", "order_items__order__user__profile"
).get()
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename="{self.object.name}.csv"'
response = HttpResponse(content_type="text/csv")
response[
"Content-Disposition"
] = f'attachment; filename="{self.object.name}.csv"'
writer = csv.writer(response)
writer.writerow(['Username', 'Email', 'Quantity', 'Paid', 'Customization', 'Option', 'Second Option', 'Quantity delivered'])
writer.writerow(
[
"Username",
"Email",
"Quantity",
"Paid",
"Customization",
"Option",
"Second Option",
"Quantity delivered",
]
)
for oi in item.order_items.all():
writer.writerow([str(oi.order.user.profile),
oi.order.user.email,
oi.quantity,
oi.order.paid,
oi.customization,
oi.option,
oi.second_option,
oi.quantity_delivered])
writer.writerow(
[
str(oi.order.user.profile),
oi.order.user.email,
oi.quantity,
oi.order.paid,
oi.customization,
oi.option,
oi.second_option,
oi.quantity_delivered,
]
)
return response
......@@ -308,6 +325,16 @@ class OrderEditView(LoginRequiredMixin, UpdateView):
def get_object(self) -> Order: # type: ignore[override] # override not supported
return self.model.objects.get_current_order(self.request.user)
def setup(self, request, *args, **kwargs):
"""Delete illegal objects prior to display"""
super().setup(request, *args, **kwargs)
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
return inlineformset_factory(
self.model,
......@@ -374,7 +401,15 @@ class OrderSendView(LoginRequiredMixin, RedirectView, SingleObjectMixin):
def get_redirect_url(self, *args, **kwargs) -> str:
self.order = self.get_object()
if self.order.items.count() < 1:
# Check if all items are legal
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:
messages.error(self.request, "Vous ne pouvez pas faire une commande vide")
return reverse("boutique:order_edit")
else:
......
Markdown is supported
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