Esempio n. 1
0
def test_formatted_decimal_field_overridden_step():
    field = FormattedDecimalFormField(
        max_digits=10,
        decimal_places=10,
        widget=NumberInput(attrs={'step': '0.1'}))

    class TestForm(Form):
        f = field

    rendered_field = force_text(TestForm()['f'])
    rendered_step = re.search('step="(.*?)"', rendered_field).group(1)
    assert rendered_step == '0.1'
Esempio n. 2
0
    def __init__(self,
                 sales_unit: Optional[SalesUnit] = None,
                 *args,
                 **kwargs):
        super().__init__(*args, **kwargs)

        if not ShuupSettings.get_setting("SHUUP_ENABLE_MULTIPLE_SHOPS"):
            self.fields["purchase_price"].label = format_lazy(
                _("Purchase price per unit ({currency_name})"),
                currency_name=get_currency_name(Shop.objects.first().currency),
            )
        self.decimals = 0
        if sales_unit:
            self.decimals = sales_unit.decimals
            self.fields["delta"] = FormattedDecimalFormField(
                label=_("Quantity"), decimal_places=sales_unit.decimals)
Esempio n. 3
0
    def __init__(self, *args, **kwargs):
        from shuup.admin.modules.settings import consts
        from shuup.admin.modules.settings.enums import OrderReferenceNumberMethod

        shop = kwargs.pop("shop")
        kwargs["initial"] = {
            consts.ORDER_REFERENCE_NUMBER_LENGTH_FIELD:
            configuration.get(shop, consts.ORDER_REFERENCE_NUMBER_LENGTH_FIELD,
                              settings.SHUUP_REFERENCE_NUMBER_LENGTH),
            consts.ORDER_REFERENCE_NUMBER_PREFIX_FIELD:
            configuration.get(shop, consts.ORDER_REFERENCE_NUMBER_PREFIX_FIELD,
                              settings.SHUUP_REFERENCE_NUMBER_PREFIX),
        }
        super(ShopOrderConfigurationForm, self).__init__(*args, **kwargs)

        reference_method = configuration.get(
            shop, consts.ORDER_REFERENCE_NUMBER_METHOD_FIELD,
            settings.SHUUP_REFERENCE_NUMBER_METHOD)

        self.prefix_disabled = reference_method in [
            OrderReferenceNumberMethod.UNIQUE.value,
            OrderReferenceNumberMethod.SHOP_RUNNING.value,
        ]

        self.fields[
            consts.
            ORDER_REFERENCE_NUMBER_PREFIX_FIELD].disabled = self.prefix_disabled

        decimal_places = 2  # default
        if shop.currency in babel.core.get_global("currency_fractions"):
            decimal_places = babel.core.get_global("currency_fractions")[
                shop.currency][0]

        self.fields[ORDER_MIN_TOTAL_CONFIG_KEY] = FormattedDecimalFormField(
            label=_("Order minimum total"),
            decimal_places=decimal_places,
            max_digits=FORMATTED_DECIMAL_FIELD_MAX_DIGITS,
            min_value=0,
            required=False,
            initial=configuration.get(shop, ORDER_MIN_TOTAL_CONFIG_KEY,
                                      Decimal(0)),
            help_text=_(
                "The minimum sum that an order needs to reach to be created."),
        )
Esempio n. 4
0
class StockAdjustmentForm(forms.Form):
    purchase_price = forms.DecimalField(label=format_lazy(
        _("Purchase price per unit ({currency_name})"),
        currency_name=get_currency_name(settings.SHUUP_HOME_CURRENCY),
    ))
    delta = FormattedDecimalFormField(label=_("Quantity"), decimal_places=0)

    def __init__(self,
                 sales_unit: Optional[SalesUnit] = None,
                 *args,
                 **kwargs):
        super().__init__(*args, **kwargs)

        if not ShuupSettings.get_setting("SHUUP_ENABLE_MULTIPLE_SHOPS"):
            self.fields["purchase_price"].label = format_lazy(
                _("Purchase price per unit ({currency_name})"),
                currency_name=get_currency_name(Shop.objects.first().currency),
            )
        self.decimals = 0
        if sales_unit:
            self.decimals = sales_unit.decimals
            self.fields["delta"] = FormattedDecimalFormField(
                label=_("Quantity"), decimal_places=sales_unit.decimals)

    def clean_delta(self):
        delta = self.cleaned_data.get("delta")
        if delta == 0:
            raise ValidationError(
                _("Only non-zero values can be added to stock."),
                code="stock_delta_zero")

        if self.decimals:
            precision = Decimal("0.1")**self.decimals
        else:
            precision = Decimal("1")
        return Decimal(delta).quantize(precision)