Mercurial > public > sg101
comparison gcalendar/admin.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/gcalendar/admin.py@ddd69a8e07c7 |
children | 8743c566f712 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 This file contains the automatic admin site definitions for the gcalendar application. | |
3 | |
4 """ | |
5 from django.conf import settings | |
6 from django.conf.urls import patterns, url | |
7 from django.contrib import admin | |
8 from django.contrib import messages | |
9 from django.contrib.sites.models import Site | |
10 from django.core.urlresolvers import reverse | |
11 from django.http import HttpResponseRedirect | |
12 from django.shortcuts import render | |
13 | |
14 import gdata.client | |
15 | |
16 from gcalendar.models import Event, AccessToken | |
17 from gcalendar.calendar import Calendar, CalendarError | |
18 from gcalendar import oauth | |
19 | |
20 import bio.badges | |
21 | |
22 | |
23 SCOPES = ['https://www.google.com/calendar/feeds/'] | |
24 | |
25 | |
26 class EventAdmin(admin.ModelAdmin): | |
27 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted', | |
28 'status', 'is_approved', 'google_html') | |
29 list_filter = ('start_date', 'status') | |
30 date_hierarchy = 'start_date' | |
31 search_fields = ('what', 'where', 'description') | |
32 raw_id_fields = ('user', ) | |
33 exclude = ('html', 'google_id', 'google_url') | |
34 save_on_top = True | |
35 actions = ('approve_events', ) | |
36 | |
37 pending_states = { | |
38 Event.NEW: Event.NEW_APRV, | |
39 Event.EDIT_REQ: Event.EDIT_APRV, | |
40 Event.DEL_REQ: Event.DEL_APRV, | |
41 } | |
42 | |
43 def get_urls(self): | |
44 urls = super(EventAdmin, self).get_urls() | |
45 my_urls = patterns('', | |
46 url(r'^google_sync/$', | |
47 self.admin_site.admin_view(self.google_sync), | |
48 name="gcalendar-google_sync"), | |
49 url(r'^fetch_auth/$', | |
50 self.admin_site.admin_view(self.fetch_auth), | |
51 name="gcalendar-fetch_auth"), | |
52 url(r'^get_access_token/$', | |
53 self.admin_site.admin_view(self.get_access_token), | |
54 name="gcalendar-get_access_token"), | |
55 ) | |
56 return my_urls + urls | |
57 | |
58 def approve_events(self, request, qs): | |
59 """ | |
60 Ratchets the selected events forward to the approved state. | |
61 Ignores events that aren't in the proper state. | |
62 """ | |
63 count = 0 | |
64 for event in qs: | |
65 if event.status in self.pending_states: | |
66 event.status = self.pending_states[event.status] | |
67 event.save() | |
68 count += 1 | |
69 | |
70 if event.status == Event.NEW_APRV: | |
71 bio.badges.award_badge(bio.badges.CALENDAR_PIN, event.user) | |
72 | |
73 msg = "1 event was" if count == 1 else "%d events were" % count | |
74 msg += " approved." | |
75 self.message_user(request, msg) | |
76 | |
77 approve_events.short_description = "Approve selected events" | |
78 | |
79 def google_sync(self, request): | |
80 """ | |
81 View to synchronize approved event changes with Google calendar. | |
82 | |
83 """ | |
84 # Get pending events | |
85 events = Event.pending_events.all() | |
86 | |
87 # Attempt to get saved access token to the Google calendar | |
88 access_token = AccessToken.objects.get_token().access_token() | |
89 | |
90 messages = [] | |
91 err_msg = '' | |
92 if request.method == 'POST': | |
93 if access_token: | |
94 try: | |
95 cal = Calendar(source=oauth.USER_AGENT, | |
96 calendar_id=settings.GCAL_CALENDAR_ID, | |
97 access_token=access_token) | |
98 cal.sync_events(events) | |
99 except CalendarError, e: | |
100 err_msg = e.msg | |
101 events = Event.pending_events.all() | |
102 else: | |
103 messages.append('All events processed successfully.') | |
104 events = Event.objects.none() | |
105 | |
106 return render(request, 'gcalendar/google_sync.html', { | |
107 'current_app': self.admin_site.name, | |
108 'access_token': access_token, | |
109 'messages': messages, | |
110 'err_msg': err_msg, | |
111 'events': events, | |
112 }) | |
113 | |
114 def fetch_auth(self, request): | |
115 """ | |
116 This view fetches a request token and then redirects the user to | |
117 authorize it. | |
118 | |
119 """ | |
120 site = Site.objects.get_current() | |
121 callback_url = 'http://%s%s' % (site.domain, | |
122 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 token = AccessToken.objects.get_token() | |
145 token.update(access_token) | |
146 token.save() | |
147 | |
148 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync')) | |
149 | |
150 | |
151 admin.site.register(Event, EventAdmin) | |
152 admin.site.register(AccessToken) |