1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- from datetime import datetime, timedelta
- from mptt.forms import * # noqa
- from django.forms import * # noqa
- from django.utils import timezone
- from django.utils.encoding import force_text
- from django.utils.translation import ugettext_lazy as _
- from .utils import parse_iso8601_string
- class YesNoSwitchBase(TypedChoiceField):
- def prepare_value(self, value):
- """normalize bools to binary 1/0 so field works on them too"""
- if value in (True, 'True', 'true', 1, '1'):
- return 1
- else:
- return 0
- def clean(self, value):
- return self.prepare_value(value)
- def YesNoSwitch(**kwargs):
- yes_label = kwargs.pop('yes_label', _("Yes"))
- no_label = kwargs.pop('no_label', _("No"))
- return YesNoSwitchBase(
- coerce=int,
- choices=((1, yes_label), (0, no_label)),
- widget=RadioSelect(attrs={'class': 'yesno-switch'}),
- **kwargs)
- class IsoDateTimeField(DateTimeField):
- input_formats = ['iso8601']
- def prepare_value(self, value):
- try:
- return value.isoformat()
- except AttributeError:
- return value
- def to_python(self, value):
- """
- Validates that the input can be converted to a datetime. Returns a
- Python datetime.datetime object.
- """
- if value in self.empty_values:
- return None
- try:
- return parse_iso8601_string(value)
- except ValueError:
- raise ValidationError(self.error_messages['invalid'], code='invalid')
|