You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fields.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from django.db.models.fields import CharField, DecimalField
  2. from django.utils.translation import ugettext_lazy as _
  3. from oscar.core import validators
  4. from oscar.forms import fields
  5. try:
  6. from south.modelsinspector import add_introspection_rules
  7. except ImportError:
  8. pass
  9. else:
  10. add_introspection_rules([], ["^oscar\.models\.fields\.ExtendedURLField$"])
  11. add_introspection_rules([], [
  12. "^oscar\.models\.fields\.PositiveDecimalField$"])
  13. class ExtendedURLField(CharField):
  14. description = _("URL")
  15. def __init__(self, verbose_name=None, name=None,
  16. verify_exists=None, **kwargs):
  17. kwargs['max_length'] = kwargs.get('max_length', 200)
  18. CharField.__init__(self, verbose_name, name, **kwargs)
  19. # 'verify_exists' was deprecated in Django 1.4. To ensure backwards
  20. # compatibility, it is still accepted here, but only passed
  21. # on to the parent class if it was specified.
  22. if verify_exists is not None:
  23. validator = validators.ExtendedURLValidator(
  24. verify_exists=verify_exists)
  25. else:
  26. validator = validators.ExtendedURLValidator()
  27. self.validators.append(validator)
  28. def formfield(self, **kwargs):
  29. # As with CharField, this will cause URL validation to be performed
  30. # twice.
  31. defaults = {
  32. 'form_class': fields.ExtendedURLField,
  33. }
  34. defaults.update(kwargs)
  35. return super(ExtendedURLField, self).formfield(**defaults)
  36. class PositiveDecimalField(DecimalField):
  37. def formfield(self, **kwargs):
  38. return super(PositiveDecimalField, self).formfield(min_value=0)