gremmie@1
|
1 """
|
gremmie@1
|
2 This file contains the automatic admin site definitions for the gcalendar application.
|
bgneal@451
|
3
|
gremmie@1
|
4 """
|
bgneal@451
|
5 from django.conf import settings
|
bgneal@451
|
6 from django.conf.urls.defaults import *
|
gremmie@1
|
7 from django.contrib import admin
|
bgneal@451
|
8 from django.contrib import messages
|
bgneal@454
|
9 from django.contrib.sites.models import Site
|
bgneal@451
|
10 from django.core.urlresolvers import reverse
|
gremmie@1
|
11 from django.http import HttpResponse
|
bgneal@451
|
12 from django.http import HttpResponseRedirect
|
bgneal@451
|
13 from django.shortcuts import render_to_response
|
bgneal@451
|
14 from django.template import RequestContext
|
bgneal@451
|
15
|
bgneal@451
|
16 import gdata.client
|
gremmie@1
|
17
|
gremmie@1
|
18 from gcalendar.models import Event
|
bgneal@451
|
19 from gcalendar.forms import PasswordForm
|
bgneal@451
|
20 from gcalendar.calendar import Calendar
|
bgneal@451
|
21 from gcalendar.calendar import CalendarError
|
bgneal@451
|
22 from gcalendar import oauth
|
bgneal@451
|
23
|
bgneal@204
|
24 import bio.badges
|
gremmie@1
|
25
|
gremmie@1
|
26
|
bgneal@451
|
27 SCOPES = ['https://www.google.com/calendar/feeds/']
|
bgneal@451
|
28
|
bgneal@451
|
29
|
gremmie@1
|
30 class EventAdmin(admin.ModelAdmin):
|
gremmie@1
|
31 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted',
|
bgneal@228
|
32 'status', 'is_approved', 'google_html')
|
gremmie@1
|
33 list_filter = ('start_date', 'status')
|
bgneal@152
|
34 date_hierarchy = 'start_date'
|
gremmie@1
|
35 search_fields = ('what', 'where', 'description')
|
gremmie@1
|
36 raw_id_fields = ('user', )
|
bgneal@228
|
37 exclude = ('html', 'google_id', 'google_url')
|
gremmie@1
|
38 save_on_top = True
|
bgneal@152
|
39 actions = ('approve_events', )
|
bgneal@152
|
40
|
bgneal@152
|
41 pending_states = {
|
bgneal@152
|
42 Event.NEW: Event.NEW_APRV,
|
bgneal@152
|
43 Event.EDIT_REQ: Event.EDIT_APRV,
|
bgneal@152
|
44 Event.DEL_REQ: Event.DEL_APRV,
|
bgneal@152
|
45 }
|
gremmie@1
|
46
|
gremmie@1
|
47 def get_urls(self):
|
gremmie@1
|
48 urls = super(EventAdmin, self).get_urls()
|
gremmie@1
|
49 my_urls = patterns('',
|
bgneal@451
|
50 url(r'^google_sync/$',
|
bgneal@451
|
51 self.admin_site.admin_view(self.google_sync),
|
bgneal@451
|
52 name="gcalendar-google_sync"),
|
bgneal@451
|
53 url(r'^fetch_auth/$',
|
bgneal@451
|
54 self.admin_site.admin_view(self.fetch_auth),
|
bgneal@451
|
55 name="gcalendar-fetch_auth"),
|
bgneal@451
|
56 url(r'^get_access_token/$',
|
bgneal@451
|
57 self.admin_site.admin_view(self.get_access_token),
|
bgneal@451
|
58 name="gcalendar-get_access_token"),
|
gremmie@1
|
59 )
|
gremmie@1
|
60 return my_urls + urls
|
gremmie@1
|
61
|
bgneal@152
|
62 def approve_events(self, request, qs):
|
bgneal@152
|
63 """
|
bgneal@152
|
64 Ratchets the selected events forward to the approved state.
|
bgneal@152
|
65 Ignores events that aren't in the proper state.
|
bgneal@152
|
66 """
|
bgneal@389
|
67 count = 0
|
bgneal@152
|
68 for event in qs:
|
bgneal@152
|
69 if event.status in self.pending_states:
|
bgneal@152
|
70 event.status = self.pending_states[event.status]
|
bgneal@152
|
71 event.save()
|
bgneal@152
|
72 count += 1
|
bgneal@152
|
73
|
bgneal@204
|
74 if event.status == Event.NEW_APRV:
|
bgneal@204
|
75 bio.badges.award_badge(bio.badges.CALENDAR_PIN, event.user)
|
bgneal@204
|
76
|
bgneal@152
|
77 msg = "1 event was" if count == 1 else "%d events were" % count
|
bgneal@152
|
78 msg += " approved."
|
bgneal@152
|
79 self.message_user(request, msg)
|
bgneal@152
|
80
|
bgneal@152
|
81 approve_events.short_description = "Approve selected events"
|
bgneal@152
|
82
|
bgneal@451
|
83 def google_sync(self, request):
|
bgneal@451
|
84 """View to synchronize approved event changes with Google calendar."""
|
bgneal@451
|
85 events = Event.pending_events.all()
|
bgneal@451
|
86 messages = []
|
bgneal@451
|
87 err_msg = ''
|
bgneal@451
|
88 if request.method == 'POST':
|
bgneal@451
|
89 form = PasswordForm(request.POST)
|
bgneal@451
|
90 if form.is_valid():
|
bgneal@451
|
91 try:
|
bgneal@451
|
92 cal = Calendar(settings.GCAL_EMAIL,
|
bgneal@451
|
93 form.cleaned_data['password'],
|
bgneal@451
|
94 settings.GCAL_CALENDAR_ID)
|
bgneal@451
|
95 cal.sync_events(events)
|
bgneal@451
|
96 except CalendarError, e:
|
bgneal@451
|
97 err_msg = e.msg
|
bgneal@451
|
98 events = Event.pending_events.all()
|
bgneal@451
|
99 form = PasswordForm()
|
bgneal@451
|
100 else:
|
bgneal@451
|
101 messages.append('All events processed successfully.')
|
bgneal@451
|
102 events = Event.objects.none()
|
bgneal@451
|
103 form = PasswordForm()
|
bgneal@451
|
104
|
bgneal@451
|
105 else:
|
bgneal@451
|
106 form = PasswordForm()
|
bgneal@451
|
107
|
bgneal@451
|
108 return render_to_response('gcalendar/google_sync.html', {
|
bgneal@451
|
109 'current_app': self.admin_site.name,
|
bgneal@451
|
110 'messages': messages,
|
bgneal@451
|
111 'err_msg': err_msg,
|
bgneal@451
|
112 'events': events,
|
bgneal@451
|
113 'form': form,
|
bgneal@451
|
114 },
|
bgneal@451
|
115 context_instance=RequestContext(request))
|
bgneal@451
|
116
|
bgneal@451
|
117 def fetch_auth(self, request):
|
bgneal@451
|
118 """
|
bgneal@451
|
119 This view fetches a request token and then redirects the user to
|
bgneal@451
|
120 authorize it.
|
bgneal@451
|
121
|
bgneal@451
|
122 """
|
bgneal@454
|
123 site = Site.objects.get_current()
|
bgneal@454
|
124 callback_url = 'http://%s%s' % (site.domain,
|
bgneal@454
|
125 reverse('admin:gcalendar-get_access_token'))
|
bgneal@451
|
126 try:
|
bgneal@451
|
127 auth_url = oauth.fetch_auth(request, SCOPES, callback_url)
|
bgneal@451
|
128 except gdata.client.Error, e:
|
bgneal@451
|
129 messages.error(request, str(e))
|
bgneal@451
|
130 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
|
bgneal@451
|
131 else:
|
bgneal@451
|
132 return HttpResponseRedirect(auth_url)
|
bgneal@451
|
133
|
bgneal@451
|
134 def get_access_token(self, request):
|
bgneal@451
|
135 """
|
bgneal@451
|
136 This view is called by Google after the user has authorized us access to
|
bgneal@451
|
137 their data. We call into the oauth module to upgrade the oauth token to
|
bgneal@451
|
138 an access token. We then save the access token in the database and
|
bgneal@451
|
139 redirect back to our admin Google sync view.
|
bgneal@451
|
140
|
bgneal@451
|
141 """
|
bgneal@451
|
142 try:
|
bgneal@451
|
143 access_token = oauth.get_access_token(request)
|
bgneal@451
|
144 except gdata.client.Error, e:
|
bgneal@451
|
145 messages.error(request, str(e))
|
bgneal@451
|
146 else:
|
bgneal@451
|
147 # TODO: save access token
|
bgneal@451
|
148 pass
|
bgneal@451
|
149
|
bgneal@451
|
150 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
|
bgneal@451
|
151
|
gremmie@1
|
152
|
gremmie@1
|
153 admin.site.register(Event, EventAdmin)
|