Mercurial > public > sg101
comparison gpp/gcalendar/admin.py @ 451:345825e6dcae
Working on #220. Can't test locally, so committing in increments.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 30 Jun 2011 01:57:17 +0000 |
parents | 3fa61786abf1 |
children | 5b2114cec3e3 |
comparison
equal
deleted
inserted
replaced
450:acc129bfd0d3 | 451:345825e6dcae |
---|---|
1 """ | 1 """ |
2 This file contains the automatic admin site definitions for the gcalendar application. | 2 This file contains the automatic admin site definitions for the gcalendar application. |
3 | |
3 """ | 4 """ |
5 from django.conf import settings | |
6 from django.conf.urls.defaults import * | |
4 from django.contrib import admin | 7 from django.contrib import admin |
8 from django.contrib import messages | |
9 from django.core.urlresolvers import reverse | |
5 from django.http import HttpResponse | 10 from django.http import HttpResponse |
6 from django.conf.urls.defaults import * | 11 from django.http import HttpResponseRedirect |
12 from django.shortcuts import render_to_response | |
13 from django.template import RequestContext | |
14 | |
15 import gdata.client | |
7 | 16 |
8 from gcalendar.models import Event | 17 from gcalendar.models import Event |
9 from gcalendar.admin_views import google_sync | 18 from gcalendar.forms import PasswordForm |
19 from gcalendar.calendar import Calendar | |
20 from gcalendar.calendar import CalendarError | |
21 from gcalendar import oauth | |
22 | |
10 import bio.badges | 23 import bio.badges |
24 | |
25 | |
26 SCOPES = ['https://www.google.com/calendar/feeds/'] | |
11 | 27 |
12 | 28 |
13 class EventAdmin(admin.ModelAdmin): | 29 class EventAdmin(admin.ModelAdmin): |
14 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted', | 30 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted', |
15 'status', 'is_approved', 'google_html') | 31 'status', 'is_approved', 'google_html') |
28 } | 44 } |
29 | 45 |
30 def get_urls(self): | 46 def get_urls(self): |
31 urls = super(EventAdmin, self).get_urls() | 47 urls = super(EventAdmin, self).get_urls() |
32 my_urls = patterns('', | 48 my_urls = patterns('', |
33 url(r'^google_sync/$', | 49 url(r'^google_sync/$', |
34 self.admin_site.admin_view(google_sync), | 50 self.admin_site.admin_view(self.google_sync), |
35 name="gcalendar-google_sync") | 51 name="gcalendar-google_sync"), |
52 url(r'^fetch_auth/$', | |
53 self.admin_site.admin_view(self.fetch_auth), | |
54 name="gcalendar-fetch_auth"), | |
55 url(r'^get_access_token/$', | |
56 self.admin_site.admin_view(self.get_access_token), | |
57 name="gcalendar-get_access_token"), | |
36 ) | 58 ) |
37 return my_urls + urls | 59 return my_urls + urls |
38 | 60 |
39 def approve_events(self, request, qs): | 61 def approve_events(self, request, qs): |
40 """ | 62 """ |
55 msg += " approved." | 77 msg += " approved." |
56 self.message_user(request, msg) | 78 self.message_user(request, msg) |
57 | 79 |
58 approve_events.short_description = "Approve selected events" | 80 approve_events.short_description = "Approve selected events" |
59 | 81 |
82 def google_sync(self, request): | |
83 """View to synchronize approved event changes with Google calendar.""" | |
84 events = Event.pending_events.all() | |
85 messages = [] | |
86 err_msg = '' | |
87 if request.method == 'POST': | |
88 form = PasswordForm(request.POST) | |
89 if form.is_valid(): | |
90 try: | |
91 cal = Calendar(settings.GCAL_EMAIL, | |
92 form.cleaned_data['password'], | |
93 settings.GCAL_CALENDAR_ID) | |
94 cal.sync_events(events) | |
95 except CalendarError, e: | |
96 err_msg = e.msg | |
97 events = Event.pending_events.all() | |
98 form = PasswordForm() | |
99 else: | |
100 messages.append('All events processed successfully.') | |
101 events = Event.objects.none() | |
102 form = PasswordForm() | |
103 | |
104 else: | |
105 form = PasswordForm() | |
106 | |
107 return render_to_response('gcalendar/google_sync.html', { | |
108 'current_app': self.admin_site.name, | |
109 'messages': messages, | |
110 'err_msg': err_msg, | |
111 'events': events, | |
112 'form': form, | |
113 }, | |
114 context_instance=RequestContext(request)) | |
115 | |
116 def fetch_auth(self, request): | |
117 """ | |
118 This view fetches a request token and then redirects the user to | |
119 authorize it. | |
120 | |
121 """ | |
122 callback_url = reverse('admin:gcalendar-get_access_token') | |
123 try: | |
124 auth_url = oauth.fetch_auth(request, SCOPES, callback_url) | |
125 except gdata.client.Error, e: | |
126 messages.error(request, str(e)) | |
127 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync')) | |
128 else: | |
129 return HttpResponseRedirect(auth_url) | |
130 | |
131 def get_access_token(self, request): | |
132 """ | |
133 This view is called by Google after the user has authorized us access to | |
134 their data. We call into the oauth module to upgrade the oauth token to | |
135 an access token. We then save the access token in the database and | |
136 redirect back to our admin Google sync view. | |
137 | |
138 """ | |
139 try: | |
140 access_token = oauth.get_access_token(request) | |
141 except gdata.client.Error, e: | |
142 messages.error(request, str(e)) | |
143 else: | |
144 # TODO: save access token | |
145 pass | |
146 | |
147 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync')) | |
148 | |
60 | 149 |
61 admin.site.register(Event, EventAdmin) | 150 admin.site.register(Event, EventAdmin) |
62 |