def __init__(self, **kwargs): """Overwrite default values and validators.""" # detect if creating migration if 'migrate' in sys.argv or 'makemigrations' in sys.argv: # remove currency information for a clean migration kwargs['default_currency'] = '' kwargs['currency_choices'] = [] else: # set defaults kwargs.update(money_kwargs()) # Set a minimum value validator validators = kwargs.get('validators', []) allow_negative = kwargs.pop('allow_negative', False) # If no validators are provided, add some "standard" ones if len(validators) == 0: if not allow_negative: validators.append(MinMoneyValidator(0), ) kwargs['validators'] = validators super().__init__(**kwargs)
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]
def __init__(self, **kwargs): # detect if creating migration if 'migrate' in sys.argv or 'makemigrations' in sys.argv: # remove currency information for a clean migration kwargs['default_currency'] = '' kwargs['currency_choices'] = [] else: # set defaults kwargs.update(money_kwargs()) # Set a minimum value validator validators = kwargs.get('validators', []) if len(validators) == 0: validators.append(MinMoneyValidator(0), ) kwargs['validators'] = validators super().__init__(**kwargs)
def get_js_validators_for_field(self, bound_field): field = bound_field.field validators = [] # "required" validation logic is not in a validator so we need a # custom solution if field.required: validators.append(self.required_validator(field)) if field.__class__ in JS_VALIDATORS: js_validator = JS_VALIDATORS[field.__class__] validators.append(js_validator(field)) for validator in field.validators: js_validator = self.get_js_for_validator(validator, field) validators.append(js_validator) return validators