コード例 #1
0
    def test_decimalfield_changed(self):
        f = DecimalField(max_digits=2, decimal_places=2)
        d = decimal.Decimal("0.1")
        self.assertFalse(f.has_changed(d, '0.10'))
        self.assertTrue(f.has_changed(d, '0.101'))

        with translation.override('fr'), self.settings(USE_L10N=True):
            f = DecimalField(max_digits=2, decimal_places=2, localize=True)
            localized_d = formats.localize_input(d)  # -> '0,1' in French
            self.assertFalse(f.has_changed(d, localized_d))
コード例 #2
0
    def test_decimalfield_changed(self):
        f = DecimalField(max_digits=2, decimal_places=2)
        d = decimal.Decimal("0.1")
        self.assertFalse(f.has_changed(d, '0.10'))
        self.assertTrue(f.has_changed(d, '0.101'))

        with translation.override('fr'), self.settings(USE_L10N=True):
            f = DecimalField(max_digits=2, decimal_places=2, localize=True)
            localized_d = formats.localize_input(d)  # -> '0,1' in French
            self.assertFalse(f.has_changed(d, localized_d))
コード例 #3
0
ファイル: fields.py プロジェクト: Suitcake/django-money
    def __init__(self, currency_widget=None, currency_choices=CURRENCY_CHOICES,
                 choices=CURRENCY_CHOICES, max_value=None, min_value=None,
                 max_digits=None, decimal_places=None, *args, **kwargs):

        # choices does not make sense in this context, it would mean we had
        # to replace two widgets with one widget dynamically... which is a
        # mess. Instead, we let currency_choices be the same as choices and
        # raise a warning.
        if currency_choices != CURRENCY_CHOICES:
            warn('currency_choices will be deprecated in favor of choices', PendingDeprecationWarning)
            choices = currency_choices

        amount_field = DecimalField(max_value, min_value, max_digits, decimal_places, *args, **kwargs)
        currency_field = ChoiceField(choices=choices)

        if VERSION < (1, 10) and hasattr(amount_field, '_has_changed') and hasattr(currency_field, '_has_changed'):
            amount_field.has_changed = amount_field._has_changed
            currency_field.has_changed = currency_field._has_changed

        # TODO: No idea what currency_widget is supposed to do since it doesn't
        # even receive currency choices as input. Somehow it's supposed to be
        # instantiated from outside. Hard to tell.
        if currency_widget:
            self.widget = currency_widget
        else:
            self.widget = MoneyWidget(
                amount_widget=amount_field.widget,
                currency_widget=currency_field.widget
            )

        # The two fields that this widget comprises
        fields = (amount_field, currency_field)
        super(MoneyField, self).__init__(fields, *args, **kwargs)
コード例 #4
0
    def __init__(self,
                 currency_widget=None,
                 currency_choices=CURRENCY_CHOICES,
                 choices=CURRENCY_CHOICES,
                 max_value=None,
                 min_value=None,
                 max_digits=None,
                 decimal_places=None,
                 *args,
                 **kwargs):

        # choices does not make sense in this context, it would mean we had
        # to replace two widgets with one widget dynamically... which is a
        # mess. Instead, we let currency_choices be the same as choices and
        # raise a warning.
        if currency_choices != CURRENCY_CHOICES:
            warn('currency_choices will be deprecated in favor of choices',
                 PendingDeprecationWarning)
            choices = currency_choices

        # get the default currency if one was specified
        default_currency = kwargs.pop('default_currency', None)

        validators = []
        if min_value:
            validators.append(MinValueValidator(min_value))
        if max_value:
            validators.append(MaxValueValidator(max_value))

        amount_field = DecimalField(max_digits=max_digits,
                                    decimal_places=decimal_places,
                                    validators=validators,
                                    *args,
                                    **kwargs)
        currency_field = ChoiceField(choices=choices)

        if VERSION < (1, 8) and hasattr(amount_field,
                                        '_has_changed') and hasattr(
                                            currency_field, '_has_changed'):
            amount_field.has_changed = amount_field._has_changed
            currency_field.has_changed = currency_field._has_changed

        # TODO: No idea what currency_widget is supposed to do since it doesn't
        # even receive currency choices as input. Somehow it's supposed to be
        # instantiated from outside. Hard to tell.
        if currency_widget:
            self.widget = currency_widget
        else:
            self.widget = MoneyWidget(amount_widget=amount_field.widget,
                                      currency_widget=currency_field.widget,
                                      default_currency=default_currency)
        # The two fields that this widget comprises
        fields = (amount_field, currency_field)
        super(MoneyField, self).__init__(fields, *args, **kwargs)

        # set the initial value to the default currency so that the
        # default currency appears as the selected menu item
        self.initial = [None, default_currency]
コード例 #5
0
    def __init__(self,
                 currency_widget=None,
                 currency_choices=CURRENCY_CHOICES,
                 choices=CURRENCY_CHOICES,
                 max_value=None,
                 min_value=None,
                 max_digits=None,
                 decimal_places=None,
                 *args,
                 **kwargs):

        # choices does not make sense in this context, it would mean we had
        # to replace two widgets with one widget dynamically... which is a
        # mess. Instead, we let currency_choices be the same as choices and
        # raise a warning.
        if currency_choices != CURRENCY_CHOICES:
            warn('currency_choices will be deprecated in favor of choices',
                 PendingDeprecationWarning)
            choices = currency_choices

        amount_field = DecimalField(max_value, min_value, max_digits,
                                    decimal_places, *args, **kwargs)
        currency_field = ChoiceField(choices=choices)

        if VERSION < (1, 10) and hasattr(amount_field,
                                         '_has_changed') and hasattr(
                                             currency_field, '_has_changed'):
            amount_field.has_changed = amount_field._has_changed
            currency_field.has_changed = currency_field._has_changed

        # TODO: No idea what currency_widget is supposed to do since it doesn't
        # even receive currency choices as input. Somehow it's supposed to be
        # instantiated from outside. Hard to tell.
        if currency_widget:
            self.widget = currency_widget
        else:
            self.widget = MoneyWidget(amount_widget=amount_field.widget,
                                      currency_widget=currency_field.widget)

        # The two fields that this widget comprises
        fields = (amount_field, currency_field)
        super(MoneyField, self).__init__(fields, *args, **kwargs)