PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mysite/dojango/forms/fields.py

https://bitbucket.org/tskarthik/clienttracker
Python | 158 lines | 134 code | 19 blank | 5 comment | 3 complexity | 59b2e5881ea60389c4dd82c46cd67365 MD5 | raw file
  1. from django.forms import *
  2. from django.conf import settings as dj_settings
  3. from django.utils import formats
  4. from dojango.forms import widgets
  5. from dojango.util import json_encode
  6. __all__ = (
  7. 'Field', 'DEFAULT_DATE_INPUT_FORMATS', 'DEFAULT_TIME_INPUT_FORMATS', # original django classes
  8. 'DEFAULT_DATETIME_INPUT_FORMATS', 'MultiValueField', 'ComboField', # original django classes
  9. 'DojoFieldMixin', 'CharField', 'ChoiceField', 'TypedChoiceField',
  10. 'IntegerField', 'BooleanField', 'FileField', 'ImageField',
  11. 'DateField', 'TimeField', 'DateTimeField', 'SplitDateTimeField',
  12. 'RegexField', 'DecimalField', 'FloatField', 'FilePathField',
  13. 'MultipleChoiceField', 'NullBooleanField', 'EmailField',
  14. 'IPAddressField', 'URLField', 'SlugField',
  15. )
  16. class DojoFieldMixin(object):
  17. """
  18. A general mixin for all custom django/dojo form fields.
  19. It passes the field attributes in 'passed_attrs' to the form widget, so
  20. they can be used there. The widget itself then evaluates which of these
  21. fiels will be used.
  22. """
  23. passed_attrs = [ # forwarded field->widget attributes
  24. 'required',
  25. 'help_text',
  26. 'min_value',
  27. 'max_value',
  28. 'max_length',
  29. 'max_digits',
  30. 'decimal_places',
  31. 'js_regex', # special key for some dojo widgets
  32. ]
  33. def widget_attrs(self, widget):
  34. """Called, when the field is instanitating the widget. Here we collect
  35. all field attributes and pass it to the attributes of the widgets using
  36. the 'extra_field_attrs' key. These additional attributes will be
  37. evaluated by the widget and deleted within the 'DojoWidgetMixin'.
  38. """
  39. ret = {'extra_field_attrs': {}}
  40. for field_attr in self.passed_attrs:
  41. field_val = getattr(self, field_attr, None)
  42. #print field_attr, widget, field_val
  43. if field_val is not None:
  44. ret['extra_field_attrs'][field_attr] = field_val
  45. return ret
  46. ###############################################
  47. # IMPLEMENTATION OF ALL EXISTING DJANGO FIELDS
  48. ###############################################
  49. class CharField(DojoFieldMixin, fields.CharField):
  50. widget = widgets.ValidationTextInput
  51. class ChoiceField(DojoFieldMixin, fields.ChoiceField):
  52. widget = widgets.Select
  53. class TypedChoiceField(DojoFieldMixin, fields.TypedChoiceField):
  54. widget = widgets.Select
  55. class IntegerField(DojoFieldMixin, fields.IntegerField):
  56. widget = widgets.NumberTextInput
  57. decimal_places = 0
  58. class BooleanField(DojoFieldMixin, fields.BooleanField):
  59. widget = widgets.CheckboxInput
  60. class FileField(DojoFieldMixin, fields.FileField):
  61. widget = widgets.FileInput
  62. class ImageField(DojoFieldMixin, fields.ImageField):
  63. widget = widgets.FileInput
  64. class DateField(DojoFieldMixin, fields.DateField):
  65. widget = widgets.DateInput
  66. def __init__(self, input_formats=None, min_value=None, max_value=None, *args, **kwargs):
  67. kwargs['input_formats'] = input_formats or \
  68. tuple(list(formats.get_format('DATE_INPUT_FORMATS')) + [
  69. '%Y-%m-%dT%H:%M', '%Y-%m-%dT%H:%M:%S' # also support dojo's default date-strings
  70. ])
  71. self.max_value = max_value
  72. self.min_value = min_value
  73. super(DateField, self).__init__(*args, **kwargs)
  74. class TimeField(DojoFieldMixin, fields.TimeField):
  75. widget = widgets.TimeInput
  76. def __init__(self, input_formats=None, min_value=None, max_value=None, *args, **kwargs):
  77. kwargs['input_formats'] = input_formats or \
  78. tuple(list(formats.get_format('TIME_INPUT_FORMATS')) + [
  79. '%Y-%m-%dT%H:%M', '%Y-%m-%dT%H:%M:%S', 'T%H:%M:%S', 'T%H:%M' # also support dojo's default time-strings
  80. ])
  81. self.max_value = max_value
  82. self.min_value = min_value
  83. super(TimeField, self).__init__(*args, **kwargs)
  84. class SplitDateTimeField(DojoFieldMixin, fields.SplitDateTimeField):
  85. widget = widgets.DateTimeInput
  86. def __init__(self, min_value=None, max_value=None, *args, **kwargs):
  87. self.max_value = max_value
  88. self.min_value = min_value
  89. super(SplitDateTimeField, self).__init__(*args, **kwargs)
  90. # Overwrite the SplitDateTimeField
  91. # copied from original SplitDateTimeField of django
  92. errors = self.default_error_messages.copy()
  93. if 'error_messages' in kwargs:
  94. errors.update(kwargs['error_messages'])
  95. fields = (
  96. DateField(error_messages={'invalid': errors['invalid_date']}),
  97. TimeField(error_messages={'invalid': errors['invalid_time']}),
  98. )
  99. # copied from original MultiValueField of django
  100. for f in fields:
  101. f.required = False
  102. self.fields = fields
  103. DateTimeField = SplitDateTimeField # datetime-field is always splitted
  104. class RegexField(DojoFieldMixin, fields.RegexField):
  105. widget = widgets.ValidationTextInput
  106. js_regex = None # we additionally have to define a custom javascript regexp, because the python one is not compatible to javascript
  107. def __init__(self, js_regex=None, *args, **kwargs):
  108. self.js_regex = js_regex
  109. super(RegexField, self).__init__(*args, **kwargs)
  110. class DecimalField(DojoFieldMixin, fields.DecimalField):
  111. widget = widgets.NumberTextInput
  112. class FloatField(DojoFieldMixin, fields.FloatField):
  113. widget = widgets.ValidationTextInput
  114. class FilePathField(DojoFieldMixin, fields.FilePathField):
  115. widget = widgets.Select
  116. class MultipleChoiceField(DojoFieldMixin, fields.MultipleChoiceField):
  117. widget = widgets.SelectMultiple
  118. class NullBooleanField(DojoFieldMixin, fields.NullBooleanField):
  119. widget = widgets.NullBooleanSelect
  120. class EmailField(DojoFieldMixin, fields.EmailField):
  121. widget = widgets.EmailTextInput
  122. class IPAddressField(DojoFieldMixin, fields.IPAddressField):
  123. widget = widgets.IPAddressTextInput
  124. class URLField(DojoFieldMixin, fields.URLField):
  125. widget = widgets.URLTextInput
  126. class SlugField(DojoFieldMixin, fields.SlugField):
  127. widget = widgets.ValidationTextInput
  128. js_regex = '^[-\w]+$' # we cannot extract the original regex input from the python regex