コード例 #1
0
 def validate(self, value, model_instance):
     # since our choices are of the form [<str>, <str>], convert the
     # incoming value to a string for validation
     if not is_pytz_instance(value):
         raise ValidationError("'%s' is not a pytz timezone object" % value)
     tz_as_str = value.zone
     super(TimeZoneFieldBase, self).validate(tz_as_str, model_instance)
コード例 #2
0
 def _get_python_and_db_repr(self, value):
     "Returns a tuple of (python representation, db representation)"
     if value is None or value == "":
         return (None, "")
     if is_pytz_instance(value):
         return (value, value.zone)
     try:
         return (pytz.timezone(force_str(value)), force_str(value))
     except pytz.UnknownTimeZoneError:
         pass
     raise ValidationError("Invalid timezone '%s'" % value)
コード例 #3
0
ファイル: fields.py プロジェクト: cloudera/hue
 def _get_python_and_db_repr(self, value):
     "Returns a tuple of (python representation, db representation)"
     if value is None or value == '':
         return (None, '')
     if is_pytz_instance(value):
         return (value, value.zone)
     try:
         return (pytz.timezone(force_text(value)), force_text(value))
     except pytz.UnknownTimeZoneError:
         pass
     raise ValidationError("Invalid timezone '%s'" % value)
コード例 #4
0
 def _get_python_and_db_repr(self, value):
     "Returns a tuple of (python representation, db representation)"
     if value is None or value == '':
         return (None, '')
     if is_pytz_instance(value):
         return (value, value.zone)
     if isinstance(value, six.string_types):
         try:
             return (pytz.timezone(value), value)
         except pytz.UnknownTimeZoneError:
             pass
     raise ValidationError("Invalid timezone '%s'" % value)
コード例 #5
0
ファイル: fields.py プロジェクト: gbts/django-timezone-field
 def _get_python_and_db_repr(self, value):
     "Returns a tuple of (python representation, db representation)"
     if value is None or value == '':
         return (None, None)
     if is_pytz_instance(value):
         return (value, value.zone)
     if isinstance(value, six.string_types):
         try:
             return (pytz.timezone(value), value)
         except pytz.UnknownTimeZoneError:
             pass
     raise ValidationError("Invalid timezone '%s'" % value)
コード例 #6
0
    def to_internal_value(self, value):
        if is_pytz_instance(value):
            return value

        if isinstance(value, six.string_types):
            if not self.strict:
                value = normalize_tz_name(value)
            try:
                return pytz.timezone(value)
            except pytz.UnknownTimeZoneError:
                pass

        self.fail('unknown_tz')
コード例 #7
0
    def __init__(self, **kwargs):
        parent_kwargs = {
            'max_length': self.MAX_LENGTH,
            'choices': TimeZoneField.CHOICES,
        }
        parent_kwargs.update(kwargs)
        super(TimeZoneFieldBase, self).__init__(**parent_kwargs)

        # We expect choices in form [<str>, <str>], but we
        # also support [<pytz.timezone>, <str>], for backwards compatability
        # Our parent saved those in self._choices.
        if self._choices:
            if is_pytz_instance(self._choices[0][0]):
                self._choices = [(tz.zone, name) for tz, name in self._choices]
コード例 #8
0
    def __init__(self, *args, **kwargs):
        # allow some use of positional args up until the args we customize
        # https://github.com/mfogel/django-timezone-field/issues/42
        # https://github.com/django/django/blob/1.11.11/django/db/models/fields/__init__.py#L145
        if len(args) > 3:
            raise ValueError('Cannot specify max_length by positional arg')
        kwargs.setdefault('max_length', self.default_max_length)

        if 'choices' in kwargs:
            values, displays = zip(*kwargs['choices'])
            # Choices can be specified in two forms: either
            # [<pytz.timezone>, <str>] or [<str>, <str>]
            #
            # The [<pytz.timezone>, <str>] format is the one we actually
            # store the choices in memory because of
            # https://github.com/mfogel/django-timezone-field/issues/24
            #
            # The [<str>, <str>] format is supported because since django
            # can't deconstruct pytz.timezone objects, migration files must
            # use an alternate format. Representing the timezones as strings
            # is the obvious choice.
            if not is_pytz_instance(values[0]):
                values = [pytz.timezone(v) for v in values]
        else:
            values = self.default_tzs
            displays = None

        choices_display = kwargs.pop('choices_display', None)
        if choices_display == 'WITH_GMT_OFFSET':
            choices = with_gmt_offset(values)
        elif choices_display == 'STANDARD':
            choices = standard(values)
        elif choices_display is None:
            choices = zip(values, displays) if displays else standard(values)
        else:
            raise ValueError(
                "Unrecognized value for kwarg 'choices_display' of '" +
                choices_display + "'")

        # 'display_GMT_offset' is deprecated, use 'choices_display' instead
        if kwargs.pop('display_GMT_offset', False):
            choices = add_gmt_offset_to_choices(choices)

        kwargs['choices'] = choices
        super(TimeZoneField, self).__init__(*args, **kwargs)
コード例 #9
0
 def to_representation(self, value):
     if value is None or value == '':
         return None
     if is_pytz_instance(value):
         return value.zone
     raise ValidationError("Invalid timezone value '%s'" % value)
コード例 #10
0
 def validate(self, value, model_instance):
     if not is_pytz_instance(value):
         raise ValidationError("'%s' is not a pytz timezone object" % value)
     super(TimeZoneField, self).validate(value, model_instance)
コード例 #11
0
ファイル: fields.py プロジェクト: cloudera/hue
 def validate(self, value, model_instance):
     if not is_pytz_instance(value):
         raise ValidationError("'%s' is not a pytz timezone object" % value)
     super(TimeZoneField, self).validate(value, model_instance)