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@574
|
6 from django.conf.urls import patterns, url
|
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
|
bgneal@451
|
11 from django.http import HttpResponseRedirect
|
bgneal@458
|
12 from django.shortcuts import render
|
bgneal@451
|
13
|
bgneal@456
|
14 from gcalendar.models import Event, AccessToken
|
bgneal@458
|
15 from gcalendar.calendar import Calendar, CalendarError
|
bgneal@451
|
16 from gcalendar import oauth
|
bgneal@451
|
17
|
bgneal@204
|
18 import bio.badges
|
gremmie@1
|
19
|
gremmie@1
|
20
|
gremmie@1
|
21 class EventAdmin(admin.ModelAdmin):
|
gremmie@1
|
22 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted',
|
bgneal@228
|
23 'status', 'is_approved', 'google_html')
|
gremmie@1
|
24 list_filter = ('start_date', 'status')
|
bgneal@152
|
25 date_hierarchy = 'start_date'
|
gremmie@1
|
26 search_fields = ('what', 'where', 'description')
|
gremmie@1
|
27 raw_id_fields = ('user', )
|
bgneal@228
|
28 exclude = ('html', 'google_id', 'google_url')
|
gremmie@1
|
29 save_on_top = True
|
bgneal@152
|
30 actions = ('approve_events', )
|
bgneal@152
|
31
|
bgneal@152
|
32 pending_states = {
|
bgneal@152
|
33 Event.NEW: Event.NEW_APRV,
|
bgneal@152
|
34 Event.EDIT_REQ: Event.EDIT_APRV,
|
bgneal@152
|
35 Event.DEL_REQ: Event.DEL_APRV,
|
bgneal@152
|
36 }
|
gremmie@1
|
37
|
gremmie@1
|
38 def get_urls(self):
|
gremmie@1
|
39 urls = super(EventAdmin, self).get_urls()
|
gremmie@1
|
40 my_urls = patterns('',
|
bgneal@451
|
41 url(r'^google_sync/$',
|
bgneal@451
|
42 self.admin_site.admin_view(self.google_sync),
|
bgneal@451
|
43 name="gcalendar-google_sync"),
|
bgneal@855
|
44 url(r'^authorize/$',
|
bgneal@855
|
45 self.admin_site.admin_view(self.authorize),
|
bgneal@855
|
46 name="gcalendar-authorize"),
|
bgneal@855
|
47 url(r'^auth_return/$',
|
bgneal@855
|
48 self.admin_site.admin_view(self.auth_return),
|
bgneal@855
|
49 name="gcalendar-auth_return"),
|
gremmie@1
|
50 )
|
gremmie@1
|
51 return my_urls + urls
|
gremmie@1
|
52
|
bgneal@152
|
53 def approve_events(self, request, qs):
|
bgneal@152
|
54 """
|
bgneal@152
|
55 Ratchets the selected events forward to the approved state.
|
bgneal@152
|
56 Ignores events that aren't in the proper state.
|
bgneal@152
|
57 """
|
bgneal@389
|
58 count = 0
|
bgneal@152
|
59 for event in qs:
|
bgneal@152
|
60 if event.status in self.pending_states:
|
bgneal@152
|
61 event.status = self.pending_states[event.status]
|
bgneal@152
|
62 event.save()
|
bgneal@152
|
63 count += 1
|
bgneal@152
|
64
|
bgneal@204
|
65 if event.status == Event.NEW_APRV:
|
bgneal@204
|
66 bio.badges.award_badge(bio.badges.CALENDAR_PIN, event.user)
|
bgneal@204
|
67
|
bgneal@152
|
68 msg = "1 event was" if count == 1 else "%d events were" % count
|
bgneal@152
|
69 msg += " approved."
|
bgneal@152
|
70 self.message_user(request, msg)
|
bgneal@152
|
71
|
bgneal@152
|
72 approve_events.short_description = "Approve selected events"
|
bgneal@152
|
73
|
bgneal@451
|
74 def google_sync(self, request):
|
bgneal@458
|
75 """
|
bgneal@458
|
76 View to synchronize approved event changes with Google calendar.
|
bgneal@458
|
77
|
bgneal@458
|
78 """
|
bgneal@458
|
79 # Get pending events
|
bgneal@451
|
80 events = Event.pending_events.all()
|
bgneal@458
|
81
|
bgneal@855
|
82 # Check status of credentials file
|
bgneal@855
|
83 cred_status = oauth.check_credentials_status()
|
bgneal@458
|
84
|
bgneal@855
|
85 msgs = []
|
bgneal@451
|
86 err_msg = ''
|
bgneal@451
|
87 if request.method == 'POST':
|
bgneal@458
|
88 if access_token:
|
bgneal@451
|
89 try:
|
bgneal@458
|
90 cal = Calendar(source=oauth.USER_AGENT,
|
bgneal@458
|
91 calendar_id=settings.GCAL_CALENDAR_ID,
|
bgneal@458
|
92 access_token=access_token)
|
bgneal@451
|
93 cal.sync_events(events)
|
bgneal@451
|
94 except CalendarError, e:
|
bgneal@451
|
95 err_msg = e.msg
|
bgneal@451
|
96 events = Event.pending_events.all()
|
bgneal@451
|
97 else:
|
bgneal@855
|
98 msgs.append('All events processed successfully.')
|
bgneal@451
|
99 events = Event.objects.none()
|
bgneal@451
|
100
|
bgneal@458
|
101 return render(request, 'gcalendar/google_sync.html', {
|
bgneal@451
|
102 'current_app': self.admin_site.name,
|
bgneal@855
|
103 'cred_status': cred_status,
|
bgneal@855
|
104 'messages': msgs,
|
bgneal@451
|
105 'err_msg': err_msg,
|
bgneal@451
|
106 'events': events,
|
bgneal@458
|
107 })
|
bgneal@451
|
108
|
bgneal@855
|
109 def authorize(self, request):
|
bgneal@451
|
110 """
|
bgneal@855
|
111 This view generates the authorization URL and redirects the user to it.
|
bgneal@451
|
112 """
|
bgneal@454
|
113 site = Site.objects.get_current()
|
bgneal@454
|
114 callback_url = 'http://%s%s' % (site.domain,
|
bgneal@855
|
115 reverse('admin:gcalendar-auth_return'))
|
bgneal@855
|
116 auth_url = oauth.get_auth_url(request, callback_url)
|
bgneal@855
|
117 return HttpResponseRedirect(auth_url)
|
bgneal@451
|
118
|
bgneal@855
|
119 def auth_return(self, request):
|
bgneal@451
|
120 """
|
bgneal@451
|
121 This view is called by Google after the user has authorized us access to
|
bgneal@855
|
122 their data. We call into the oauth module to process the authorization
|
bgneal@855
|
123 code and exchange it for tokens.
|
bgneal@451
|
124 """
|
bgneal@451
|
125 try:
|
bgneal@855
|
126 oauth.auth_return(request)
|
bgneal@855
|
127 except oauth.OAuthError as e:
|
bgneal@855
|
128 self.message_user(request, str(e), level=messages.ERROR)
|
bgneal@451
|
129
|
bgneal@451
|
130 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
|
bgneal@451
|
131
|
gremmie@1
|
132
|
gremmie@1
|
133 admin.site.register(Event, EventAdmin)
|
bgneal@456
|
134 admin.site.register(AccessToken)
|