Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

fields.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. self.verify_exists = verify_exists
  23. if verify_exists is not None:
  24. validator = validators.ExtendedURLValidator(
  25. verify_exists=verify_exists)
  26. else:
  27. validator = validators.ExtendedURLValidator()
  28. self.validators.append(validator)
  29. def formfield(self, **kwargs):
  30. # As with CharField, this will cause URL validation to be performed
  31. # twice.
  32. defaults = {
  33. 'form_class': fields.ExtendedURLField,
  34. 'verify_exists': self.verify_exists
  35. }
  36. defaults.update(kwargs)
  37. return super(ExtendedURLField, self).formfield(**defaults)
  38. class PositiveDecimalField(DecimalField):
  39. def formfield(self, **kwargs):
  40. return super(PositiveDecimalField, self).formfield(min_value=0)