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("choices", self.CHOICES)
        kwargs.setdefault("max_length", self.MAX_LENGTH)
        kwargs.setdefault("display_GMT_offset", False)

        # 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.
        choices = kwargs["choices"]
        if isinstance(choices[0][0], (str, bytes)):
            kwargs["choices"] = [(pytz.timezone(n1), n2) for n1, n2 in choices]

        if kwargs["display_GMT_offset"]:
            kwargs["choices"] = add_gmt_offset_to_choices(kwargs["choices"])
        kwargs.pop("display_GMT_offset", None)

        super(TimeZoneField, self).__init__(*args, **kwargs)
Example #2
0
 def test_add_gmt_offset_to_choices(self):
     result = add_gmt_offset_to_choices(self.timezones)
     expected = [
         "GMT-11:00 Pacific/Apia",
         "GMT-08:00 US/Pacific",
         "GMT-05:00 US/Eastern",
         "GMT+00:00 Europe/London",
         "GMT+03:00 Asia/Qatar",
         "GMT+13:00 Pacific/Fiji",
     ]
     for i in range(len(expected)):
         self.assertEqual(expected[i], result[i][1])
Example #3
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)