From 00ae91dace48f5feda2979c30b42f7c084fd350a Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Tue, 8 May 2018 08:21:42 -0400 Subject: [PATCH] slight cleanup and refactor of Schedule.timezone property --- awx/api/views.py | 6 +++++- awx/main/models/schedules.py | 35 +++++++++++++++-------------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index f1098ffce0..3304c2ca43 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -745,7 +745,11 @@ class ScheduleZoneInfo(APIView): swagger_topic = 'System Configuration' def get(self, request): - return Response(Schedule.get_zoneinfo()) + zones = [ + {'name': zone} + for zone in Schedule.get_zoneinfo() + ] + return Response(zones) class LaunchConfigCredentialsBase(SubListAttachDetachAPIView): diff --git a/awx/main/models/schedules.py b/awx/main/models/schedules.py index 565ad2de12..787e69da0b 100644 --- a/awx/main/models/schedules.py +++ b/awx/main/models/schedules.py @@ -6,9 +6,9 @@ import logging import re import dateutil.rrule -from operator import itemgetter import dateutil.parser from dateutil.tz import datetime_exists, tzutc +from dateutil.zoneinfo import get_zonefile_instance # Django from django.db import models @@ -100,28 +100,22 @@ class Schedule(CommonModel, LaunchTimeConfig): @classmethod def get_zoneinfo(self): - from dateutil.zoneinfo import get_zonefile_instance - return [ - {'name': zone} - for zone in sorted(get_zonefile_instance().zones) - ] + return 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 = Schedule.get_zoneinfo() all_zones.sort(key = lambda x: -len(x)) - for zone in all_zones: - if fname.endswith(zone): - return zone + for r in Schedule.rrulestr(self.rrule)._rrule: + if r._dtstart: + tzinfo = r._dtstart.tzinfo + if tzinfo is utc: + return 'UTC' + fname = tzinfo._filename + for zone in all_zones: + if fname.endswith(zone): + return zone logger.warn('Could not detect valid zoneinfo for {}'.format(self.rrule)) return '' @@ -130,6 +124,7 @@ class Schedule(CommonModel, LaunchTimeConfig): """ Apply our own custom rrule parsing requirements """ + tzinfos = {x: tzutc() for x in dateutil.parser.parserinfo().UTCZONE} kwargs['forceset'] = True # @@ -163,7 +158,7 @@ class Schedule(CommonModel, LaunchTimeConfig): # local_tz = tzfile('/usr/share/zoneinfo/America/New_York') local_tz = dateutil.rrule.rrulestr( rrule.replace(naive_until, naive_until + 'Z'), - tzinfos={x: tzutc() for x in dateutil.parser.parserinfo().UTCZONE} + tzinfos=tzinfos )._dtstart.tzinfo # Make a datetime object with tzinfo= @@ -181,7 +176,7 @@ class Schedule(CommonModel, LaunchTimeConfig): # rrule is now: DTSTART;TZID=America/New_York:20200601T120000 RRULE:...;UNTIL=20200601T220000Z rrule = rrule.replace(naive_until, utc_until) - x = dateutil.rrule.rrulestr(rrule, **kwargs) + x = dateutil.rrule.rrulestr(rrule, tzinfos=tzinfos, **kwargs) for r in x._rrule: if r._dtstart and r._dtstart.tzinfo is None: