class ModelForm(forms.ModelForm): class Meta: model = models.Model fields = [] price_net = MoneyField(available_currencies=AVAILABLE_CURRENCIES) price_gross = MoneyField(available_currencies=AVAILABLE_CURRENCIES)
class ManagePaymentForm(forms.Form): amount = MoneyField( label=pgettext_lazy( 'Payment management form (capture, refund, release)', 'Amount'), max_digits=12, decimal_places=settings.DEFAULT_DECIMAL_PLACES, currency=settings.DEFAULT_CURRENCY) def __init__(self, *args, **kwargs): self.payment = kwargs.pop('payment') super().__init__(*args, **kwargs) def clean(self): if self.payment.status != self.clean_status: raise forms.ValidationError(self.clean_error) def payment_error(self, message): self.add_error( None, pgettext_lazy( 'Payment form error', 'Payment gateway error: %s') % message) def try_payment_action(self, action): money = self.cleaned_data['amount'] try: action(money.amount) except (PaymentError, ValueError) as e: self.payment_error(str(e)) return False return True
class ValidatedPriceForm(forms.Form): price = MoneyField(currency='USD', max_digits=9, decimal_places=2, validators=[ MinMoneyValidator(Money(5, currency='USD')), MaxMoneyValidator(Money(15, currency='USD')) ])
def make_money_field(): return MoneyField( available_currencies=settings.AVAILABLE_CURRENCIES, min_values=[zero_money()], max_digits=settings.DEFAULT_MAX_DIGITS, decimal_places=settings.DEFAULT_DECIMAL_PLACES, required=False, )
class MaxMinPriceForm(forms.Form): price = MoneyField( available_currencies=AVAILABLE_CURRENCIES, min_values=[Money(5, currency="USD"), Money(6, currency="BTC")], max_values=[Money(15, currency="USD"), Money(16, currency="BTC")], )
def set_initial_money(instance: models.Model, field_name: str, field: MoneyField): money_field = getattr(instance, field_name) # type: Optional[Money] if money_field is None: value = [None, settings.DEFAULT_CURRENCY] else: value = [money_field.amount, money_field.currency] if not money_field.currency: value[1] = settings.DEFAULT_CURRENCY field.initial = value
class ValidatedPriceForm(forms.Form): price = MoneyField( available_currencies=AVAILABLE_CURRENCIES, max_digits=9, decimal_places=2, validators=[ MinMoneyValidator(Money(5, "USD")), MaxMoneyValidator(Money(15, "USD")), ], )
class WeightShippingMethodForm(MoneyModelForm): minimum_order_weight = WeightField( required=False, label=pgettext_lazy("Minimum order weight to use this shipping method", "Minimum order weight"), ) maximum_order_weight = WeightField( required=False, label=pgettext_lazy("Maximum order weight to use this shipping method", "Maximum order weight"), ) price = MoneyField(required=False, available_currencies=settings.AVAILABLE_CURRENCIES) class Meta(ShippingMethodForm.Meta): fields = [ "name", "price", "minimum_order_weight", "maximum_order_weight" ] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["maximum_order_weight"].widget.attrs[ "placeholder"] = pgettext_lazy( "Placeholder for maximum order weight set to unlimited", "No limit") self.fields["minimum_order_weight"].widget.attrs["placeholder"] = "0" def clean_minimum_order_weight(self): return self.cleaned_data["minimum_order_weight"] or 0 def clean(self): data = super().clean() min_weight = data.get("minimum_order_weight") max_weight = data.get("maximum_order_weight") if min_weight and max_weight is not None and max_weight <= min_weight: self.add_error( "maximum_order_weight", pgettext_lazy( "Price shipping method form error", "Maximum order price should be larger" " than the minimum order price.", ), ) return data
class ValueVoucherForm(forms.ModelForm): limit = MoneyField( min_value=Money(0, currency=settings.DEFAULT_CURRENCY), required=False, currency=settings.DEFAULT_CURRENCY) class Meta: model = Voucher fields = ['limit'] labels = { 'limit': pgettext_lazy( 'Lowest value for order to be able to use the voucher', 'Only if purchase value is greater than or equal to')} def save(self, commit=True): self.instance.category = None self.instance.apply_to = None self.instance.product = None return super().save(commit)
class ShippingMethodForm(MoneyModelForm): price = MoneyField( required=False, label=pgettext_lazy("Currency amount", "Price"), available_currencies=settings.AVAILABLE_CURRENCIES, ) class Meta: model = ShippingMethod fields = ["name", "price"] labels = { "name": pgettext_lazy("Shipping Method name", "Name"), "price": pgettext_lazy("Currency amount", "Price"), } help_texts = { "name": pgettext_lazy( "Shipping method name help text", "Customers will see this at the checkout.", ) }
class ShippingVoucherForm(forms.ModelForm): limit = MoneyField(min_value=Money(0, settings.DEFAULT_CURRENCY), required=False, currency=settings.DEFAULT_CURRENCY) apply_to = forms.ChoiceField(choices=country_choices, required=False) class Meta: model = Voucher fields = ['apply_to', 'limit'] labels = { 'apply_to': pgettext_lazy('Country', 'Country'), 'limit': pgettext_lazy( 'Lowest value for order to be able to use the voucher', 'Only if order is over or equal to') } def save(self, commit=True): self.instance.category = None self.instance.product = None return super().save(commit)
from django.urls import reverse_lazy from django.utils.translation import pgettext_lazy from django_countries import countries from django_prices.forms import MoneyField from mptt.forms import TreeNodeMultipleChoiceField from ...core.utils.taxes import ZERO_MONEY from ...discount import DiscountValueType from ...discount.models import Sale, Voucher from ...discount.utils import generate_voucher_code from ...skill.models import Category, Skill from ..forms import AjaxSelect2MultipleChoiceField MinAmountSpent = MoneyField( min_value=ZERO_MONEY, required=False, currency=settings.DEFAULT_CURRENCY, label=pgettext_lazy( 'Lowest value for task to be able to use the voucher', 'Apply only if the purchase value is greater than or equal to')) class SaleForm(forms.ModelForm): skills = AjaxSelect2MultipleChoiceField( queryset=Skill.objects.all(), fetch_data_url=reverse_lazy('dashboard:ajax-skills'), required=False, label=pgettext_lazy('Discounted skills', 'Discounted skills')) class Meta: model = Sale exclude = [] labels = {
class FixedCurrencyOptionalPriceForm(forms.Form): price = MoneyField(available_currencies=["BTC"], required=False)
class FixedCurrencyRequiredPriceForm(forms.Form): price = MoneyField(available_currencies=["BTC"])
class OptionalPriceForm(forms.Form): price_net = MoneyField(available_currencies=AVAILABLE_CURRENCIES, required=False)
class RequiredPriceForm(forms.Form): price_net = MoneyField(available_currencies=AVAILABLE_CURRENCIES)
class PriceShippingMethodForm(MoneyModelForm): price = MoneyField( required=False, label=pgettext_lazy("Currency amount", "Price"), available_currencies=settings.AVAILABLE_CURRENCIES, ) minimum_order_price = MoneyField( required=False, label=pgettext_lazy("Minimum order price to use this shipping method", "Minimum order price"), available_currencies=settings.AVAILABLE_CURRENCIES, ) maximum_order_price = MoneyField( required=False, label=pgettext_lazy("Maximum order price to use this order", "Maximum order price"), available_currencies=settings.AVAILABLE_CURRENCIES, ) class Meta(ShippingMethodForm.Meta): labels = { "minimum_order_price": pgettext_lazy("Minimum order price to use this shipping method", "Minimum order price"), "maximum_order_price": pgettext_lazy("Maximum order price to use this order", "Maximum order price"), } labels.update(ShippingMethodForm.Meta.labels) fields = [ "name", "price", "minimum_order_price", "maximum_order_price" ] def __init__(self, *args, **kwargs): instance = kwargs["instance"] initial = kwargs.setdefault("initial", {}) initial.setdefault("price", instance.price) initial.setdefault("minimum_order_price", instance.minimum_order_price) initial.setdefault("maximum_order_price", instance.maximum_order_price) super().__init__(*args, **kwargs) self.fields["maximum_order_price"].widget.attrs[ "placeholder"] = pgettext_lazy( "Placeholder for maximum order price set to unlimited", "No limit") self.fields["minimum_order_price"].widget.attrs["placeholder"] = "0" def clean_minimum_order_price(self): return self.cleaned_data["minimum_order_price"] or zero_money( self.instance.currency) def clean(self): data = super().clean() min_price = data.get("minimum_order_price") max_price = data.get("maximum_order_price") if min_price and max_price is not None and max_price <= min_price: self.add_error( "maximum_order_price", pgettext_lazy( "Price shipping method form error", "Maximum order price should be larger" " than the minimum order price.", ), ) return data
from mptt.forms import TreeNodeMultipleChoiceField from ...core.taxes import zero_money from ...core.utils.promo_code import generate_promo_code from ...discount import DiscountValueType from ...discount.models import Sale, Voucher from ...product.models import Category, Product from ...product.tasks import update_products_minimal_variant_prices_of_discount_task from ..forms import AjaxSelect2MultipleChoiceField, MoneyModelForm MinAmountSpent = MoneyField( available_currencies=settings.AVAILABLE_CURRENCIES, min_values=[zero_money()], max_digits=settings.DEFAULT_MAX_DIGITS, decimal_places=settings.DEFAULT_DECIMAL_PLACES, required=False, label=pgettext_lazy( "Lowest value for order to be able to use the voucher", "Apply only if the purchase value is greater than or equal to", ), ) class SaleForm(forms.ModelForm): products = AjaxSelect2MultipleChoiceField( queryset=Product.objects.all(), fetch_data_url=reverse_lazy("dashboard:ajax-products"), required=False, label=pgettext_lazy("Discounted products", "Discounted products"), )
class OptionalPriceForm(forms.Form): price_net = MoneyField(currency='BTC', required=False)
class RequiredPriceForm(forms.Form): price_net = MoneyField(currency='BTC')
from django_countries import countries from django_prices.forms import MoneyField from mptt.forms import TreeNodeMultipleChoiceField from ...core.taxes import zero_money from ...core.utils.promo_code import generate_promo_code from ...discount import DiscountValueType from ...discount.models import Sale, Voucher from ...product.models import Category, Product from ..forms import AjaxSelect2MultipleChoiceField MinAmountSpent = MoneyField( min_value=zero_money(), required=False, currency=settings.DEFAULT_CURRENCY, label=pgettext_lazy( "Lowest value for order to be able to use the voucher", "Apply only if the purchase value is greater than or equal to", ), ) class SaleForm(forms.ModelForm): products = AjaxSelect2MultipleChoiceField( queryset=Product.objects.all(), fetch_data_url=reverse_lazy("dashboard:ajax-products"), required=False, label=pgettext_lazy("Discounted products", "Discounted products"), ) class Meta: