Verified Commit 966d1f93 authored by VAN ZUIJLEN Nils's avatar VAN ZUIJLEN Nils
Browse files

Black

parent 21206bbe
......@@ -120,7 +120,9 @@ class SellForm(forms.Form):
with transaction.atomic():
order = Order.objects.create(user=user, ordered=True)
Payment.objects.create(mean=mean, paid=True, order=order)
order_item = item.add_to_order(order, quantity, customization, option, second_option)
order_item = item.add_to_order(
order, quantity, customization, option, second_option
)
order.compute_price()
order.save()
order.post_payment()
......
......@@ -159,12 +159,12 @@ class Item(models.Model):
return static("images/default_product.png")
def add_to_order(
self,
order: "Order",
quantity: int = 1,
customization: str = "",
option: str = "",
second_option: str = "",
self,
order: "Order",
quantity: int = 1,
customization: str = "",
option: str = "",
second_option: str = "",
) -> "OrderItem":
oi, created = None, False
if self.customizable:
......@@ -196,7 +196,7 @@ class Item(models.Model):
if not oi.is_pack_item:
break
if (
oi is None or oi.is_pack_item
oi is None or oi.is_pack_item
): # The preceding queries in the try-except block were empty or they
# ended on a pack_item
oi = OrderItem.objects.create(
......@@ -218,12 +218,12 @@ class Item(models.Model):
return oi
def add_to_user(
self,
user,
quantity: int = 1,
customization: str = "",
option: str = "",
second_option: str = "",
self,
user,
quantity: int = 1,
customization: str = "",
option: str = "",
second_option: str = "",
) -> "OrderItem":
if self.hidden or self.end_date < timezone.now():
raise ValidationError(
......@@ -267,7 +267,7 @@ class Order(models.Model):
try:
return self.payment.mean
except Payment.DoesNotExist:
return 'None'
return "None"
@property
def contains_cotiz(self) -> bool:
......@@ -453,7 +453,7 @@ class OrderItem(models.Model):
)
def delete(
self, *args, override_pack=False, **kwargs
self, *args, override_pack=False, **kwargs
) -> Tuple[int, Dict[str, int]]:
deleted = 0
which: typing.Counter[str] = Counter()
......@@ -483,7 +483,7 @@ class OrderItem(models.Model):
@receiver(pre_save, sender=OrderItem)
@receiver(pre_delete, sender=OrderItem)
def orderitem_price_computing(
sender: Type[OrderItem], instance: OrderItem, **kwargs
sender: Type[OrderItem], instance: OrderItem, **kwargs
) -> None:
if kwargs.get("raw", False):
return
......@@ -495,9 +495,9 @@ def orderitem_price_computing(
@receiver(pre_delete, sender=OrderItem)
@receiver(pre_delete, sender=Order)
def no_delete_for_ordered(
sender: Union[Type[Order], Type[OrderItem]],
instance: Union[Order, OrderItem],
**kwargs,
sender: Union[Type[Order], Type[OrderItem]],
instance: Union[Order, OrderItem],
**kwargs,
) -> None:
if instance.ordered:
raise ValueError("Deletion of ordered instances is not allowed")
......@@ -505,7 +505,7 @@ def no_delete_for_ordered(
@receiver(pre_save, sender=lydiaRequest)
def mark_payment_as_paid(
sender: Type[lydiaRequest], instance: lydiaRequest, **kwargs
sender: Type[lydiaRequest], instance: lydiaRequest, **kwargs
) -> None:
if kwargs.get("raw", False):
return
......
......@@ -510,7 +510,11 @@ class OrderEditViewTest(OrderItemTestCase):
resp = self.client.get(url)
self.assertRedirects(resp, "/accounts/login?next=/boutique/order/edit", fetch_redirect_response=False)
self.assertRedirects(
resp,
"/accounts/login?next=/boutique/order/edit",
fetch_redirect_response=False,
)
class DeliveryViewTest(PaymentTestCase, OrderItemTestCase):
......
......@@ -49,5 +49,4 @@ urlpatterns = [
),
),
path("delivery", views.DeliveryView.as_view(), name="delivery"),
]
......@@ -224,12 +224,21 @@ class ItemManageView(PermissionRequiredMixin, ListView):
message = req.get("message")
if message == "begin-delivery":
item_qs.get().order_items.filter(order__payment__paid=True).update(beeing_delivered=True)
return JsonResponse({"status": "success", "data": "Début des livraisons des articles déjà commandé"})
item_qs.get().order_items.filter(order__payment__paid=True).update(
beeing_delivered=True
)
return JsonResponse(
{
"status": "success",
"data": "Début des livraisons des articles déjà commandé",
}
)
if message == "stop-delivery":
item_qs.get().order_items.update(beeing_delivered=False)
return JsonResponse({"status": "success", "data": "Arrêt des livraisons des articles"})
return JsonResponse(
{"status": "success", "data": "Arrêt des livraisons des articles"}
)
elif message == "see-orders":
item = item_qs.prefetch_related(
......@@ -240,7 +249,6 @@ class ItemManageView(PermissionRequiredMixin, ListView):
"status": "success",
"data": [
{
"display_name": str(oi.order.user.profile),
"email": str(oi.order.user.email),
"quantity": oi.quantity,
......@@ -253,9 +261,10 @@ class ItemManageView(PermissionRequiredMixin, ListView):
"paid": oi.order.paid,
"ordered_date": oi.order.ordered_date,
"mean": oi.order.mean,
}
for oi in item.order_items.all().order_by("-order__ordered_date")
for oi in item.order_items.all().order_by(
"-order__ordered_date"
)
],
}
)
......@@ -263,13 +272,29 @@ class ItemManageView(PermissionRequiredMixin, ListView):
raise SuspiciousOperation()
def get_queryset(self) -> "QuerySet[Item]":
return Item.objects.annotate(
total_quantity=Coalesce(Sum('order_items__quantity', filter=Q(order_items__order__payment__paid=True)),
Value(0)),
quantity_delivered=Coalesce(Sum('order_items__quantity_delivered'), Value(0)),
quantity_to_deliver=Coalesce(Sum('order_items__quantity', filter=Q(order_items__beeing_delivered=True)),
Value(0))) \
.order_by("hidden").prefetch_related("order_items")
return (
Item.objects.annotate(
total_quantity=Coalesce(
Sum(
"order_items__quantity",
filter=Q(order_items__order__payment__paid=True),
),
Value(0),
),
quantity_delivered=Coalesce(
Sum("order_items__quantity_delivered"), Value(0)
),
quantity_to_deliver=Coalesce(
Sum(
"order_items__quantity",
filter=Q(order_items__beeing_delivered=True),
),
Value(0),
),
)
.order_by("hidden")
.prefetch_related("order_items")
)
class ItemSellView(PermissionRequiredMixin, CreateView):
......@@ -546,9 +571,9 @@ class DeliveryView(PermissionRequiredMixin, ListView):
).select_related("order__user__profile", "order__user", "item")
def boolQueryParam(self, param: str) -> Optional[bool]:
if param == 'true':
if param == "true":
return True
if param == 'false':
if param == "false":
return False
else:
return None
......@@ -575,34 +600,44 @@ class DeliveryView(PermissionRequiredMixin, ListView):
if paid is True:
itemList = itemList.filter(order__payment__paid=True)
elif paid is False:
itemList = itemList.filter(Q(order__payment__isnull=True) | Q(order__payment__paid=False))
itemList = itemList.filter(
Q(order__payment__isnull=True) | Q(order__payment__paid=False)
)
itemList = itemList.filter(Q(item__name__icontains=request.GET["productContains"])
& Q(customization__icontains=request.GET["customizationContains"])
& Q(order__user__email__icontains=request.GET["emailContains"]))
itemList = itemList.filter(
Q(item__name__icontains=request.GET["productContains"])
& Q(customization__icontains=request.GET["customizationContains"])
& Q(order__user__email__icontains=request.GET["emailContains"])
)
optionContains = request.GET["optionContains"]
if optionContains != "":
itemList = itemList.filter(Q(option__iexact=optionContains) | Q(second_option__iexact=optionContains))
itemList = itemList.filter(
Q(option__iexact=optionContains)
| Q(second_option__iexact=optionContains)
)
orderedItems = [
{"id": orderItem.id,
"item": str(orderItem.item),
"image": orderItem.item.get_image_url(),
"display_name": str(orderItem.order.user.profile),
"email": str(orderItem.order.user.email),
"quantity": orderItem.quantity,
"quantity_delivered": orderItem.quantity_delivered,
"option_label": orderItem.item.option_label,
"option": orderItem.option,
"second_option_label": orderItem.item.second_option_label,
"second_option": orderItem.second_option,
"customization": orderItem.customization,
"paid": orderItem.order.paid,
"ordered_date": orderItem.order.ordered_date,
"mean": orderItem.order.mean,
}
for orderItem in itemList.select_related("order__user__profile", "order__user", "item").all()
{
"id": orderItem.id,
"item": str(orderItem.item),
"image": orderItem.item.get_image_url(),
"display_name": str(orderItem.order.user.profile),
"email": str(orderItem.order.user.email),
"quantity": orderItem.quantity,
"quantity_delivered": orderItem.quantity_delivered,
"option_label": orderItem.item.option_label,
"option": orderItem.option,
"second_option_label": orderItem.item.second_option_label,
"second_option": orderItem.second_option,
"customization": orderItem.customization,
"paid": orderItem.order.paid,
"ordered_date": orderItem.order.ordered_date,
"mean": orderItem.order.mean,
}
for orderItem in itemList.select_related(
"order__user__profile", "order__user", "item"
).all()
]
return JsonResponse({"orderedItems": orderedItems})
......@@ -635,7 +670,11 @@ class DeliveryView(PermissionRequiredMixin, ListView):
orderitem = queryset.get()
except OrderItem.DoesNotExist:
return JsonResponse(
{"status": "error", "code": 404, "message": "Commande introuvable ou impayée"},
{
"status": "error",
"code": 404,
"message": "Commande introuvable ou impayée",
},
status=404,
)
......@@ -701,8 +740,8 @@ class DeliveryView(PermissionRequiredMixin, ListView):
{
"status": "success",
"code": 200,
"message": f"{quantity} {orderitem.item} remis à {orderitem.order.user.profile} " +
f"({orderitem.quantity_delivered}/{orderitem.quantity} remis au total)",
"message": f"{quantity} {orderitem.item} remis à {orderitem.order.user.profile} "
+ f"({orderitem.quantity_delivered}/{orderitem.quantity} remis au total)",
"quantity_delivered": orderitem.quantity_delivered,
}
)
......
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