mirror of
https://github.com/ansible/awx.git
synced 2026-03-11 06:29:31 -02:30
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:
committed by
Jared Tabor
parent
e5dd3a9626
commit
fbe2391b86
@@ -4511,9 +4511,14 @@ class SchedulePreviewSerializer(BaseSerializer):
|
|||||||
class ScheduleSerializer(LaunchConfigurationBaseSerializer, SchedulePreviewSerializer):
|
class ScheduleSerializer(LaunchConfigurationBaseSerializer, SchedulePreviewSerializer):
|
||||||
show_capabilities = ['edit', 'delete']
|
show_capabilities = ['edit', 'delete']
|
||||||
|
|
||||||
|
timezone = serializers.SerializerMethodField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Schedule
|
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):
|
def get_related(self, obj):
|
||||||
res = super(ScheduleSerializer, self).get_related(obj)
|
res = super(ScheduleSerializer, self).get_related(obj)
|
||||||
|
|||||||
@@ -745,11 +745,7 @@ class ScheduleZoneInfo(APIView):
|
|||||||
swagger_topic = 'System Configuration'
|
swagger_topic = 'System Configuration'
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
from dateutil.zoneinfo import get_zonefile_instance
|
return Response(Schedule.get_zoneinfo())
|
||||||
return Response([
|
|
||||||
{'name': zone}
|
|
||||||
for zone in sorted(get_zonefile_instance().zones)
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
class LaunchConfigCredentialsBase(SubListAttachDetachAPIView):
|
class LaunchConfigCredentialsBase(SubListAttachDetachAPIView):
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
import logging
|
import logging
|
||||||
import datetime
|
import datetime
|
||||||
import dateutil.rrule
|
import dateutil.rrule
|
||||||
from dateutil.tz import datetime_exists
|
from operator import itemgetter
|
||||||
|
import dateutil.parser
|
||||||
|
from dateutil.tz import datetime_exists, tzutc
|
||||||
|
|
||||||
# Django
|
# Django
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@@ -94,6 +96,33 @@ class Schedule(CommonModel, LaunchTimeConfig):
|
|||||||
help_text=_("The next time that the scheduled action will run.")
|
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
|
@classmethod
|
||||||
def rrulestr(cls, rrule, **kwargs):
|
def rrulestr(cls, rrule, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -203,3 +203,18 @@ def test_beginning_of_time(job_template):
|
|||||||
)
|
)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
s.save()
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user