Mercurial > public > sg101
comparison gpp/gcalendar/calendar.py @ 458:9a4bffdf37c3
Finishing up #220. Updated to GData v2.0 and using the new OAuth access token.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 02 Jul 2011 03:52:43 +0000 |
parents | d77e0dc772ad |
children |
comparison
equal
deleted
inserted
replaced
457:7b7332037396 | 458:9a4bffdf37c3 |
---|---|
1 """ | 1 """ |
2 This file contains the calendar class wich abstracts the Google gdata API for working with | 2 This file contains the calendar class wich abstracts the Google gdata API for working with |
3 Google Calendars. | 3 Google Calendars. |
4 | |
4 """ | 5 """ |
5 import datetime | 6 import datetime |
6 import pytz | 7 import pytz |
7 | 8 |
8 from django.utils.tzinfo import FixedOffset | 9 from django.utils.tzinfo import FixedOffset |
9 from gdata.calendar.service import CalendarService | 10 from gdata.calendar.client import CalendarClient |
10 from gdata.calendar import CalendarEventFeed | 11 from gdata.calendar.data import (CalendarEventEntry, CalendarEventFeed, |
11 from gdata.calendar import CalendarEventEntry | 12 CalendarWhere, When, EventWho) |
12 from gdata.calendar import Who | 13 import atom.data |
13 from gdata.calendar import Where | |
14 from gdata.calendar import When | |
15 from gdata.service import BadAuthentication | |
16 import atom | |
17 | 14 |
18 from gcalendar.models import Event | 15 from gcalendar.models import Event |
19 | 16 |
20 | 17 |
21 class CalendarError(Exception): | 18 class CalendarError(Exception): |
29 class Calendar(object): | 26 class Calendar(object): |
30 DATE_FMT = '%Y-%m-%d' | 27 DATE_FMT = '%Y-%m-%d' |
31 DATE_TIME_FMT = DATE_FMT + 'T%H:%M:%S' | 28 DATE_TIME_FMT = DATE_FMT + 'T%H:%M:%S' |
32 DATE_TIME_TZ_FMT = DATE_TIME_FMT + '.000Z' | 29 DATE_TIME_TZ_FMT = DATE_TIME_FMT + '.000Z' |
33 | 30 |
34 def __init__(self, email, password, calendar_id='default'): | 31 def __init__(self, source=None, calendar_id='default', access_token=None): |
35 self.client = CalendarService() | 32 self.client = CalendarClient(source=source, auth_token=access_token) |
36 self.client.email = email | 33 |
37 self.client.password = password | 34 self.insert_feed = ('https://www.google.com/calendar/feeds/' |
38 self.client.source = 'Google-Calendar_Python_GCalendar' | 35 '%s/private/full' % calendar_id) |
39 self.insert_feed = '/calendar/feeds/%s/private/full' % calendar_id | |
40 self.batch_feed = '%s/batch' % self.insert_feed | 36 self.batch_feed = '%s/batch' % self.insert_feed |
41 try: | |
42 self.client.ProgrammaticLogin() | |
43 except BadAuthentication: | |
44 raise CalendarError('Incorrect password') | |
45 except Exception, e: | |
46 raise CalendarError(str(e)) | |
47 | 37 |
48 def sync_events(self, qs): | 38 def sync_events(self, qs): |
49 request_feed = CalendarEventFeed() | 39 request_feed = CalendarEventFeed() |
50 for model in qs: | 40 for model in qs: |
51 if model.status == Event.NEW_APRV: | 41 if model.status == Event.NEW_APRV: |
69 for entry in response_feed.entry: | 59 for entry in response_feed.entry: |
70 i = int(entry.batch_id.text) | 60 i = int(entry.batch_id.text) |
71 code = int(entry.batch_status.code) | 61 code = int(entry.batch_status.code) |
72 | 62 |
73 error = False | 63 error = False |
74 if qs[i].status == Event.NEW_APRV or qs[i].status == Event.EDIT_APRV: | 64 if qs[i].status == Event.NEW_APRV: |
75 if (code == 201 and qs[i].status == Event.NEW_APRV) or ( | 65 if code == 201: |
76 code == 200 and qs[i].status == Event.EDIT_APRV): | 66 qs[i].status = Event.ON_CAL |
77 qs[i].google_id = entry.id.text | 67 qs[i].google_id = entry.GetEditLink().href |
78 qs[i].google_url = entry.GetHtmlLink().href | 68 qs[i].google_url = entry.GetHtmlLink().href |
69 qs[i].save() | |
70 qs[i].notify_on_calendar() | |
71 else: | |
72 error = True | |
73 | |
74 elif qs[i].status == Event.EDIT_APRV: | |
75 if code == 200: | |
79 qs[i].status = Event.ON_CAL | 76 qs[i].status = Event.ON_CAL |
80 qs[i].save() | 77 qs[i].save() |
81 if code == 201: | |
82 qs[i].notify_on_calendar() | |
83 else: | 78 else: |
84 error = True | 79 error = True |
80 | |
85 elif qs[i].status == Event.DEL_APRV: | 81 elif qs[i].status == Event.DEL_APRV: |
86 if code == 200: | 82 if code == 200: |
87 qs[i].delete() | 83 qs[i].delete() |
88 else: | 84 else: |
89 error = True | 85 error = True |
95 if len(err_msgs) > 0: | 91 if len(err_msgs) > 0: |
96 raise CalendarError(', '.join(err_msgs)) | 92 raise CalendarError(', '.join(err_msgs)) |
97 | 93 |
98 def _retrieve_event(self, model): | 94 def _retrieve_event(self, model): |
99 try: | 95 try: |
100 event = self.client.GetCalendarEventEntry(model.google_id) | 96 event = self.client.GetEventEntry(model.google_id) |
101 except Exception, e: | 97 except Exception, e: |
102 raise CalendarError('Could not retrieve event from Google: %s, %s' \ | 98 raise CalendarError('Could not retrieve event from Google: %s, %s' \ |
103 % (model.what, e)) | 99 % (model.what, e)) |
104 return event | 100 return event |
105 | 101 |
106 def _populate_event(self, model, event): | 102 def _populate_event(self, model, event): |
107 """Populates a gdata event from an Event model object.""" | 103 """Populates a gdata event from an Event model object.""" |
108 event.title = atom.Title(text=model.what) | 104 event.title = atom.data.Title(text=model.what) |
109 event.content = atom.Content(text=model.html) | 105 event.content = atom.data.Content(text=model.html) |
110 event.where = [Where(value_string=model.where)] | 106 event.where = [CalendarWhere(value=model.where)] |
111 event.who = [Who(name=model.user.username, email=model.user.email)] | 107 event.who = [EventWho(email=model.user.email)] |
112 | 108 |
113 if model.all_day: | 109 if model.all_day: |
114 start_time = self._make_time(model.start_date) | 110 start_time = self._make_time(model.start_date) |
115 if model.start_date == model.end_date: | 111 if model.start_date == model.end_date: |
116 end_time = None | 112 end_time = None |
118 end_time = self._make_time(model.end_date) | 114 end_time = self._make_time(model.end_date) |
119 else: | 115 else: |
120 start_time = self._make_time(model.start_date, model.start_time, model.time_zone) | 116 start_time = self._make_time(model.start_date, model.start_time, model.time_zone) |
121 end_time = self._make_time(model.end_date, model.end_time, model.time_zone) | 117 end_time = self._make_time(model.end_date, model.end_time, model.time_zone) |
122 | 118 |
123 event.when = [When(start_time=start_time, end_time=end_time)] | 119 event.when = [When(start=start_time, end=end_time)] |
124 return event | 120 return event |
125 | 121 |
126 def _make_time(self, date, time=None, tz_name=None): | 122 def _make_time(self, date, time=None, tz_name=None): |
127 """ | 123 """ |
128 Returns the gdata formatted date/time string given a date, optional time, | 124 Returns the gdata formatted date/time string given a date, optional time, |
129 and optional time zone name (e.g. 'US/Pacific'). If the time zone name is None, | 125 and optional time zone name (e.g. 'US/Pacific'). If the time zone name is None, |
130 no time zone info will be added to the string. | 126 no time zone info will be added to the string. |