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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from django.core.exceptions import ImproperlyConfigured
  2. from django.db.models.fields import CharField, DecimalField, Field
  3. from django.db.models import SubfieldBase
  4. from django.utils import six
  5. from django.utils.translation import ugettext_lazy as _
  6. from django.core import validators as django_validators
  7. from oscar.core import validators
  8. from oscar.forms import fields
  9. import oscar.core.phonenumber as phonenumber
  10. try:
  11. from south.modelsinspector import add_introspection_rules
  12. except ImportError:
  13. pass
  14. else:
  15. add_introspection_rules([], ["^oscar\.models\.fields\.ExtendedURLField$"])
  16. add_introspection_rules([], [
  17. "^oscar\.models\.fields\.PositiveDecimalField$"])
  18. add_introspection_rules([], [
  19. "^oscar\.models\.fields\.UppercaseCharField$"])
  20. add_introspection_rules([], [
  21. "^oscar\.models\.fields\.PhoneNumberField$"])
  22. class ExtendedURLField(CharField):
  23. description = _("URL")
  24. def __init__(self, verbose_name=None, name=None,
  25. verify_exists=None, **kwargs):
  26. kwargs['max_length'] = kwargs.get('max_length', 200)
  27. CharField.__init__(self, verbose_name, name, **kwargs)
  28. # 'verify_exists' was deprecated in Django 1.4. To ensure backwards
  29. # compatibility, it is still accepted here, but only passed
  30. # on to the parent class if it was specified.
  31. self.verify_exists = verify_exists
  32. if verify_exists is not None:
  33. validator = validators.ExtendedURLValidator(
  34. verify_exists=verify_exists)
  35. else:
  36. validator = validators.ExtendedURLValidator()
  37. self.validators.append(validator)
  38. def formfield(self, **kwargs):
  39. # As with CharField, this will cause URL validation to be performed
  40. # twice.
  41. defaults = {
  42. 'form_class': fields.ExtendedURLField,
  43. 'verify_exists': self.verify_exists
  44. }
  45. defaults.update(kwargs)
  46. return super(ExtendedURLField, self).formfield(**defaults)
  47. class PositiveDecimalField(DecimalField):
  48. def formfield(self, **kwargs):
  49. return super(PositiveDecimalField, self).formfield(min_value=0)
  50. class UppercaseCharField(CharField):
  51. # necessary for to_python to be called
  52. __metaclass__ = SubfieldBase
  53. def to_python(self, value):
  54. val = super(UppercaseCharField, self).to_python(value)
  55. if isinstance(val, six.string_types):
  56. return val.upper()
  57. else:
  58. return val
  59. class PhoneNumberField(Field):
  60. """
  61. Copyright (c) 2011 Stefan Foulis and contributors.
  62. https://github.com/stefanfoulis/django-phonenumber-field
  63. Taken from fork https://github.com/maikhoepfel/django-phonenumber-field/
  64. A international phone number field for django that uses
  65. http://pypi.python.org/pypi/phonenumbers for validation.
  66. * Validates a wide range of phone number formats
  67. * Displays it nicely formatted
  68. * Can be given a hint for the country, so that it can accept local numbers,
  69. that are not in an international format
  70. """
  71. attr_class = phonenumber.PhoneNumber
  72. descriptor_class = phonenumber.PhoneNumberDescriptor
  73. default_validators = [phonenumber.validate_international_phonenumber]
  74. description = _("Phone number")
  75. def __init__(self, *args, **kwargs):
  76. if kwargs.get('null', False):
  77. raise ImproperlyConfigured(
  78. "null=True is not supported on PhoneNumberField")
  79. kwargs['max_length'] = kwargs.get('max_length', 128)
  80. super(PhoneNumberField, self).__init__(*args, **kwargs)
  81. self.validators.append(django_validators.MaxLengthValidator(self.max_length))
  82. def get_internal_type(self):
  83. return "CharField"
  84. def get_prep_value(self, value):
  85. """
  86. Returns field's value prepared for saving into a database.
  87. """
  88. value = phonenumber.to_python(value)
  89. if value is None:
  90. return ''
  91. return value.as_e164 if value.is_valid() else value.raw_input
  92. def contribute_to_class(self, cls, name):
  93. super(PhoneNumberField, self).contribute_to_class(cls, name)
  94. setattr(cls, self.name, self.descriptor_class(self))