From ff4dd46c8790dbe3c867f06089e170bc3024ddac Mon Sep 17 00:00:00 2001 From: Tom Price Date: Mon, 18 May 2015 23:41:17 +0100 Subject: [PATCH] Re-enable timezones and add iCal converstion to UTC. This should work on Google Cal but won't be able to test till in production. --- PyRIGS/settings.py | 2 +- RIGS/ical.py | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/PyRIGS/settings.py b/PyRIGS/settings.py index 90b88c16..297ef8d8 100644 --- a/PyRIGS/settings.py +++ b/PyRIGS/settings.py @@ -180,7 +180,7 @@ USE_I18N = True USE_L10N = True -USE_TZ = False +USE_TZ = True DATETIME_INPUT_FORMATS = ('%Y-%m-%dT%H:%M','%Y-%m-%dT%H:%M:%S') diff --git a/RIGS/ical.py b/RIGS/ical.py index 1eb4ebe3..ecabce62 100644 --- a/RIGS/ical.py +++ b/RIGS/ical.py @@ -2,19 +2,26 @@ from RIGS import models, forms from django_ical.views import ICalFeed from django.db.models import Q from django.core.urlresolvers import reverse_lazy, reverse, NoReverseMatch +from django.utils import timezone +from django.conf import settings -import datetime +import datetime, pytz class CalendarICS(ICalFeed): """ A simple event calender """ #Metadata which is passed on to clients - product_id = 'PyRIGS' - title = 'PyRIGS Calendar' - timezone = 'Europe/London' + product_id = 'RIGS' + title = 'RIGS Calendar' + timezone = 'UTC' file_name = "rigs.ics" + def get(self, *args, **kwargs): + timezone.activate(timezone.UTC) + return super(CalendarICS, self).get(*args, **kwargs) + + def items(self): #include events from up to 1 year ago start = datetime.datetime.now() - datetime.timedelta(days=365) @@ -51,6 +58,8 @@ class CalendarICS(ICalFeed): startDateTime = item.access_at elif item.has_start_time: startDateTime = datetime.datetime.combine(item.start_date,item.start_time) + tz = pytz.timezone(settings.TIME_ZONE) + startDateTime = tz.normalize(tz.localize(startDateTime)).astimezone(pytz.timezone(self.timezone)) else: startDateTime = item.start_date @@ -66,6 +75,8 @@ class CalendarICS(ICalFeed): if item.has_start_time and item.has_end_time: # don't allow an event with specific end but no specific start endDateTime = datetime.datetime.combine(endDateTime,item.end_time) + tz = pytz.timezone(settings.TIME_ZONE) + endDateTime = tz.normalize(tz.localize(endDateTime)).astimezone(pytz.timezone(self.timezone)) elif item.has_end_time: # if there's a start time specified then an end time should also be specified endDateTime = datetime.datetime.combine(endDateTime+datetime.timedelta(days=1),datetime.time(00, 00)) #elif item.end_time: # end time but no start time - this is weird - don't think ICS will like it so ignoring