Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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