# # Copyright 2022 # - Maréchal None: """Perform all the checks, but return nothing. To know if the form is valid, the `is_valid()` method must be used. The form shall be considered as valid if it meets all the following conditions : - it contains a "basket_items" key in the cookies of the request given in the constructor - this cookie is a list of objects formatted this way : `[{'id': , 'quantity': , 'name': , 'unit_price': }, ...]`. The order of the fields in each object does not matter - all the ids are positive integers - all the ids refer to products available in the EBOUTIC - all the ids refer to products the user is allowed to buy - all the quantities are positive integers """ try: basket = PurchaseItemList.validate_json( unquote(self.cookies.get("basket_items", "[]")) ) except ValidationError: self.error_messages.add(_("The request was badly formatted.")) return if len(basket) == 0: self.error_messages.add(_("Your basket is empty.")) return existing_ids = {product.id for product in get_eboutic_products(self.user)} for item in basket: # check a product with this id does exist if item.product_id in existing_ids: self.correct_items.append(item) else: self.error_messages.add( _( "%(name)s : this product does not exist or may no longer be available." ) % {"name": item.name} ) continue # this function does not return anything. # instead, it fills a set containing the collected error messages # an empty set means that no error was seen thus everything is ok # and the form is valid. # a non-empty set means there was at least one error thus # the form is invalid def is_valid(self) -> bool: """Return True if the form is correct else False. If the `clean()` method has not been called beforehand, call it. """ if not self.error_messages and not self.correct_items: self.clean() if self.error_messages: return False return True @cached_property def errors(self) -> list[str]: return list(self.error_messages) @cached_property def cleaned_data(self) -> list[PurchaseItemSchema]: return self.correct_items