Mercurial > public > sg101
comparison gpp/gcalendar/admin.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 | 7b7332037396 |
children | ddd69a8e07c7 |
comparison
equal
deleted
inserted
replaced
457:7b7332037396 | 458:9a4bffdf37c3 |
---|---|
10 from django.contrib import messages | 10 from django.contrib import messages |
11 from django.contrib.sites.models import Site | 11 from django.contrib.sites.models import Site |
12 from django.core.urlresolvers import reverse | 12 from django.core.urlresolvers import reverse |
13 from django.http import HttpResponse | 13 from django.http import HttpResponse |
14 from django.http import HttpResponseRedirect | 14 from django.http import HttpResponseRedirect |
15 from django.shortcuts import render_to_response | 15 from django.shortcuts import render |
16 from django.template import RequestContext | 16 from django.template import RequestContext |
17 | 17 |
18 import gdata.client | 18 import gdata.client |
19 | 19 |
20 from gcalendar.models import Event, AccessToken | 20 from gcalendar.models import Event, AccessToken |
21 from gcalendar.forms import PasswordForm | 21 from gcalendar.calendar import Calendar, CalendarError |
22 from gcalendar.calendar import Calendar | |
23 from gcalendar.calendar import CalendarError | |
24 from gcalendar import oauth | 22 from gcalendar import oauth |
25 | 23 |
26 import bio.badges | 24 import bio.badges |
27 | 25 |
28 | 26 |
81 self.message_user(request, msg) | 79 self.message_user(request, msg) |
82 | 80 |
83 approve_events.short_description = "Approve selected events" | 81 approve_events.short_description = "Approve selected events" |
84 | 82 |
85 def google_sync(self, request): | 83 def google_sync(self, request): |
86 """View to synchronize approved event changes with Google calendar.""" | 84 """ |
85 View to synchronize approved event changes with Google calendar. | |
86 | |
87 """ | |
88 # Get pending events | |
87 events = Event.pending_events.all() | 89 events = Event.pending_events.all() |
90 | |
91 # Attempt to get saved access token to the Google calendar | |
92 access_token = AccessToken.objects.get_token().access_token() | |
93 | |
88 messages = [] | 94 messages = [] |
89 err_msg = '' | 95 err_msg = '' |
90 if request.method == 'POST': | 96 if request.method == 'POST': |
91 form = PasswordForm(request.POST) | 97 if access_token: |
92 if form.is_valid(): | |
93 try: | 98 try: |
94 cal = Calendar(settings.GCAL_EMAIL, | 99 cal = Calendar(source=oauth.USER_AGENT, |
95 form.cleaned_data['password'], | 100 calendar_id=settings.GCAL_CALENDAR_ID, |
96 settings.GCAL_CALENDAR_ID) | 101 access_token=access_token) |
97 cal.sync_events(events) | 102 cal.sync_events(events) |
98 except CalendarError, e: | 103 except CalendarError, e: |
99 err_msg = e.msg | 104 err_msg = e.msg |
100 events = Event.pending_events.all() | 105 events = Event.pending_events.all() |
101 form = PasswordForm() | |
102 else: | 106 else: |
103 messages.append('All events processed successfully.') | 107 messages.append('All events processed successfully.') |
104 events = Event.objects.none() | 108 events = Event.objects.none() |
105 form = PasswordForm() | |
106 | 109 |
107 else: | 110 return render(request, 'gcalendar/google_sync.html', { |
108 form = PasswordForm() | |
109 | |
110 return render_to_response('gcalendar/google_sync.html', { | |
111 'current_app': self.admin_site.name, | 111 'current_app': self.admin_site.name, |
112 'access_token': access_token, | |
112 'messages': messages, | 113 'messages': messages, |
113 'err_msg': err_msg, | 114 'err_msg': err_msg, |
114 'events': events, | 115 'events': events, |
115 'form': form, | 116 }) |
116 }, | |
117 context_instance=RequestContext(request)) | |
118 | 117 |
119 def fetch_auth(self, request): | 118 def fetch_auth(self, request): |
120 """ | 119 """ |
121 This view fetches a request token and then redirects the user to | 120 This view fetches a request token and then redirects the user to |
122 authorize it. | 121 authorize it. |
144 try: | 143 try: |
145 access_token = oauth.get_access_token(request) | 144 access_token = oauth.get_access_token(request) |
146 except gdata.client.Error, e: | 145 except gdata.client.Error, e: |
147 messages.error(request, str(e)) | 146 messages.error(request, str(e)) |
148 else: | 147 else: |
149 try: | 148 token = AccessToken.objects.get_token() |
150 token = AccessToken.objects.get(id=1) | 149 token.update(access_token) |
151 except AccessToken.DoesNotExist: | |
152 token = AccessToken() | |
153 | |
154 token.token = gdata.gauth.TokenToBlob(access_token) | |
155 token.auth_date = datetime.datetime.now() | |
156 token.save() | 150 token.save() |
157 | 151 |
158 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync')) | 152 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync')) |
159 | 153 |
160 | 154 |