Skip to content

Commit

Permalink
Optimize product id validation on counter click
Browse files Browse the repository at this point in the history
  • Loading branch information
klmp200 committed Dec 22, 2024
1 parent ff475c2 commit 73bb53b
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions counter/views/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(
self,
customer: Customer,
counter: Counter,
allowed_products: list[Product],
allowed_products: dict[int, Product],
*args,
**kwargs,
):
Expand All @@ -64,25 +64,24 @@ def __init__(
self.allowed_products = allowed_products
super().__init__(*args, **kwargs)

def clean(self):
cleaned_data = super().clean()
if len(self.errors) > 0:
return
def clean_id(self):
data = self.cleaned_data["id"]

# We store self.product so we can use it later on the formset validation
self.product = next(
(
product
for product in self.allowed_products
if product.id == cleaned_data["id"]
),
None,
)
# And also in the global clean
self.product = self.allowed_products.get(data, None)
if self.product is None:
raise ValidationError(
_("The selected product isn't available for this user")
)

return data

def clean(self):
cleaned_data = super().clean()
if len(self.errors) > 0:
return

# Compute prices
cleaned_data["bonus_quantity"] = 0
if self.product.tray:
Expand Down Expand Up @@ -183,7 +182,9 @@ def get_form_kwargs(self):
kwargs["form_kwargs"] = {
"customer": self.customer,
"counter": self.object,
"allowed_products": self.get_products(),
"allowed_products": {
product.id: product for product in self.get_products()
},
}
return kwargs

Expand Down

0 comments on commit 73bb53b

Please sign in to comment.