def clean(self): """ Pre save validation: - A payment product and all its variants must exist before creating a LicenceType. - Check for senior voucher if applicable. :return: raise an exception if error """ variant_codes = payment_utils.generate_product_title_variants(self) missing_product_variants = [] for variant_code in variant_codes: if payment_utils.get_product(variant_code) is None: missing_product_variants.append(variant_code) if missing_product_variants: msg = mark_safe( "The payments products with titles matching the below list of product codes were not " "found. Note: You must create a payment product(s) for a new licence type and all its " "variants, even if the licence has no fee. <ul><li>{}</li></ul>" .format('</li><li>'.join(missing_product_variants))) raise ValidationError(msg) if self.senior_applicable and payment_utils.get_voucher( settings.WL_SENIOR_VOUCHER_CODE) is None: msg = mark_safe( "The senior voucher with code={} cannot be found. It must be created before setting a " "licence type to be senior applicable.<br>" "Note: the senior voucher code can be changed in the settings of the application." .format(settings.WL_SENIOR_VOUCHER_CODE)) raise ValidationError(msg)
def clean(self): """ Pre save validation: - A payment product and all its variants must exist before creating a LicenceType. - Check for senior voucher if applicable. :return: raise an exception if error """ variant_codes = payment_utils.generate_product_title_variants(self) missing_product_variants = [] for variant_code in variant_codes: if payment_utils.get_product(variant_code) is None: missing_product_variants.append(variant_code) if missing_product_variants: msg = mark_safe("The payments products with titles matching the below list of product codes were not " "found. Note: You must create a payment product(s) for a new licence type and all its " "variants, even if the licence is free. <ul><li>{}</li></ul>". format('</li><li>'.join(missing_product_variants))) raise ValidationError(msg) if self.senior_applicable and payment_utils.get_voucher(settings.WL_SENIOR_VOUCHER_CODE) is None: msg = mark_safe("The senior voucher with code={} cannot be found. It must be created before setting a " "licence type to be senior applicable.<br>" "Note: the senior voucher code can be changed in the settings of the application." .format(settings.WL_SENIOR_VOUCHER_CODE)) raise ValidationError(msg)
def clean(self): """ Checks if there are any licence types with this group or one it this group's parents set and makes sure there's products for every variant of those licence types. type. :return: """ if self.instance is not None: related_variant_groups = [] def __get_group_and_parent_groups(current_variant_group, groups): groups.append(current_variant_group) for group in VariantGroup.objects.filter( child=current_variant_group): __get_group_and_parent_groups(group, groups) __get_group_and_parent_groups(self.instance, related_variant_groups) # keep previous variants in case the validation fails if hasattr(self.instance, 'variants'): previous_variants = list(self.instance.variants.all()) self.instance.variants = self.cleaned_data['variants'] missing_product_variants = [] for licence_type in WildlifeLicenceType.objects.filter( variant_group__in=related_variant_groups): variant_codes = payment_utils.generate_product_title_variants( licence_type) for variant_code in variant_codes: if payment_utils.get_product(variant_code) is None: missing_product_variants.append(variant_code) if missing_product_variants: msg = mark_safe( "The payments products with titles matching the below list of product codes were not " "found. Note: You must create a payment product(s) for variants of each licence type linked " "to this variant group, even if the licence has no fee. <ul><li>{}</li></ul>" .format('</li><li>'.join(missing_product_variants))) if hasattr(self.instance, 'variants'): # revert back to previous variants list self.instance.variants = previous_variants raise ValidationError(msg)
def clean(self): """ Checks if there are any licence types with this group or one it this group's parents set and makes sure there's products for every variant of those licence types. type. :return: """ if self.instance is not None: related_variant_groups = [] def __get_group_and_parent_groups(current_variant_group, groups): groups.append(current_variant_group) for group in VariantGroup.objects.filter(child=current_variant_group): __get_group_and_parent_groups(group, groups) __get_group_and_parent_groups(self.instance, related_variant_groups) # keep previous variants in case the validation fails if hasattr(self.instance, "variants"): previous_variants = list(self.instance.variants.all()) self.instance.variants = self.cleaned_data["variants"] missing_product_variants = [] for licence_type in WildlifeLicenceType.objects.filter(variant_group__in=related_variant_groups): variant_codes = payment_utils.generate_product_title_variants(licence_type) for variant_code in variant_codes: if payment_utils.get_product(variant_code) is None: missing_product_variants.append(variant_code) if missing_product_variants: msg = mark_safe( "The payments products with titles matching the below list of product codes were not " "found. Note: You must create a payment product(s) for variants of each licence type linked " "to this variant group, even if the licence is free. <ul><li>{}</li></ul>".format( "</li><li>".join(missing_product_variants) ) ) if hasattr(self.instance, "variants"): # revert back to previous variants list self.instance.variants = previous_variants raise ValidationError(msg)