provide the timezone so that the UI doesn't have to

this will also ensure that UI doesn't use a different front end library
that will yield different results than the underlying Python code
This commit is contained in:
Ryan Petrello 2018-05-02 15:23:57 -04:00 committed by Jared Tabor
parent e5dd3a9626
commit fbe2391b86
4 changed files with 52 additions and 7 deletions

View File

@ -4511,9 +4511,14 @@ class SchedulePreviewSerializer(BaseSerializer):
class ScheduleSerializer(LaunchConfigurationBaseSerializer, SchedulePreviewSerializer):
show_capabilities = ['edit', 'delete']
timezone = serializers.SerializerMethodField()
class Meta:
model = Schedule
fields = ('*', 'unified_job_template', 'enabled', 'dtstart', 'dtend', 'rrule', 'next_run',)
fields = ('*', 'unified_job_template', 'enabled', 'dtstart', 'dtend', 'rrule', 'next_run', 'timezone',)
def get_timezone(self, obj):
return obj.timezone
def get_related(self, obj):
res = super(ScheduleSerializer, self).get_related(obj)

View File

@ -745,11 +745,7 @@ class ScheduleZoneInfo(APIView):
swagger_topic = 'System Configuration'
def get(self, request):
from dateutil.zoneinfo import get_zonefile_instance
return Response([
{'name': zone}
for zone in sorted(get_zonefile_instance().zones)
])
return Response(Schedule.get_zoneinfo())
class LaunchConfigCredentialsBase(SubListAttachDetachAPIView):

View File

@ -4,7 +4,9 @@
import logging
import datetime
import dateutil.rrule
from dateutil.tz import datetime_exists
from operator import itemgetter
import dateutil.parser
from dateutil.tz import datetime_exists, tzutc
# Django
from django.db import models
@ -94,6 +96,33 @@ class Schedule(CommonModel, LaunchTimeConfig):
help_text=_("The next time that the scheduled action will run.")
)
@classmethod
def get_zoneinfo(self):
from dateutil.zoneinfo import get_zonefile_instance
return [
{'name': zone}
for zone in sorted(get_zonefile_instance().zones)
]
@property
def timezone(self):
utc = tzutc()
_rrule = dateutil.rrule.rrulestr(
self.rrule,
tzinfos={x: utc for x in dateutil.parser.parserinfo().UTCZONE}
)
tzinfo = _rrule._dtstart.tzinfo
if tzinfo == utc:
return 'UTC'
fname = tzinfo._filename
all_zones = map(itemgetter('name'), Schedule.get_zoneinfo())
all_zones.sort(key = lambda x: -len(x))
for zone in all_zones:
if fname.endswith(zone):
return zone
logger.warn('Could not detect valid zoneinfo for {}'.format(self.rrule))
return ''
@classmethod
def rrulestr(cls, rrule, **kwargs):
"""

View File

@ -203,3 +203,18 @@ def test_beginning_of_time(job_template):
)
with pytest.raises(ValueError):
s.save()
@pytest.mark.django_db
@pytest.mark.parametrize('rrule, tz', [
['DTSTART:20300112T210000Z RRULE:FREQ=DAILY;INTERVAL=1', 'UTC'],
['DTSTART;TZID=America/New_York:20300112T210000 RRULE:FREQ=DAILY;INTERVAL=1', 'America/New_York']
])
def test_timezone_property(job_template, rrule, tz):
# ensure that really large generators don't have performance issues
s = Schedule(
name='Some Schedule',
rrule=rrule,
unified_job_template=job_template
)
assert s.timezone == tz