class DiscountForm(forms.ModelForm): discount_percentage = PercentageField( max_digits=6, decimal_places=5, required=False, label=_("Discount percentage"), help_text=_("The discount percentage for this discount.")) class Meta: model = Discount exclude = ("shops", "created_by", "modified_by") widgets = { "category": QuickAddCategorySelect(editable_model="shuup.Category"), "contact_group": QuickAddContactGroupSelect(editable_model="shuup.ContactGroup"), "coupon_code": QuickAddCouponCodeSelect(editable_model="discounts.CouponCode"), "availability_exceptions": QuickAddAvailabilityExceptionMultiSelect(), "happy_hours": QuickAddHappyHourMultiSelect() } def __init__(self, *args, **kwargs): self.request = kwargs.pop("request") self.shop = get_shop(self.request) super(DiscountForm, self).__init__(*args, **kwargs) self.fields["availability_exceptions"].queryset = AvailabilityException.objects.filter(shops=self.shop) self.fields["category"].queryset = Category.objects.filter(shops=self.shop) self.fields["contact"].widget = ContactChoiceWidget(clearable=True) self.fields["contact_group"].queryset = ContactGroup.objects.filter(shop=self.shop) self.fields["coupon_code"].queryset = CouponCode.objects.filter(shops=self.shop) self.fields["happy_hours"].queryset = HappyHour.objects.filter(shops=self.shop) self.fields["product"].widget = ProductChoiceWidget(clearable=True) self.fields["supplier"].queryset = Supplier.objects.enabled(shop=self.shop) def save(self, commit=True): instance = super(DiscountForm, self).save(commit) instance.shops.set([self.shop]) return instance
class DiscountPercentageFromUndiscountedForm(BaseEffectModelForm): discount_percentage = PercentageField( max_digits=6, decimal_places=5, label=_("discount percentage"), help_text=_("The discount percentage for this campaign.")) class Meta(BaseEffectModelForm.Meta): model = DiscountPercentageFromUndiscounted
class ProductDiscountPercentageForm(BaseEffectModelForm): discount_percentage = PercentageField( max_digits=6, decimal_places=5, label=_("discount percentage"), help_text=_("The discount percentage for this campaign.")) class Meta(BaseEffectModelForm.Meta): model = ProductDiscountPercentage exclude = COMMON_EXCLUDES
def test_percentage_field(): field = PercentageField(min_value=0, max_value=Decimal("0.7")) assert Decimal(str(field.widget.attrs["max"])) == 70 assert field.to_python("50") == Decimal("0.5") assert field.to_python(50) == Decimal("0.5") assert field.to_python(500) == Decimal(5) assert field.to_python("") is None assert field.prepare_value(Decimal("0.50")) == 50 with pytest.raises(ValidationError): field.clean(-7) # --> -0.07 (< min_value) with pytest.raises(ValidationError): field.clean("700") # -> 7 (> max_value) frm = forms.Form(data={"x": "15"}) frm.fields["x"] = field frm.full_clean() assert frm.cleaned_data["x"] == Decimal("0.15")
class DiscountFromCategoryProductsForm(BaseEffectModelForm): discount_percentage = PercentageField( max_digits=6, decimal_places=5, label=_("discount percentage"), required=False, help_text=_("The discount percentage for this campaign.")) class Meta(BaseEffectModelForm.Meta): model = DiscountFromCategoryProducts def __init__(self, **kwargs): super(DiscountFromCategoryProductsForm, self).__init__(**kwargs) self.fields["category"].queryset = Category.objects.all_except_deleted() def clean(self): data = self.cleaned_data if data["discount_amount"] and data["discount_percentage"]: msg = _("Only amount or percentage can be set.") self.add_error("discount_amount", msg) self.add_error("discount_percentage", msg) return data