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 2.2KB

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