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@1028
|
6 from django.conf.urls import 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@857
|
14 from gcalendar.models import Event
|
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):
|
bgneal@857
|
22 list_display = ['what', 'user', 'start_date', 'where', 'date_submitted',
|
bgneal@857
|
23 'status', 'is_approved', 'google_html']
|
bgneal@857
|
24 list_filter = ['start_date', 'status']
|
bgneal@152
|
25 date_hierarchy = 'start_date'
|
bgneal@857
|
26 search_fields = ['what', 'where', 'description']
|
bgneal@857
|
27 raw_id_fields = ['user']
|
bgneal@857
|
28 exclude = ['html', 'google_id', 'google_url']
|
gremmie@1
|
29 save_on_top = True
|
bgneal@857
|
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()
|
bgneal@1028
|
40 my_urls = [
|
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"),
|
bgneal@1028
|
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@857
|
88 credentials = oauth.get_credentials()
|
bgneal@857
|
89 if credentials:
|
bgneal@451
|
90 try:
|
bgneal@857
|
91 cal = Calendar(calendar_id=settings.GCAL_CALENDAR_ID,
|
bgneal@857
|
92 credentials=credentials)
|
bgneal@451
|
93 cal.sync_events(events)
|
bgneal@451
|
94 except CalendarError, e:
|
bgneal@857
|
95 err_msg = str(e)
|
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@857
|
100 else:
|
bgneal@857
|
101 self.message_user(request, "Invalid or missing credentials",
|
bgneal@857
|
102 level=messages.ERROR)
|
bgneal@451
|
103
|
bgneal@458
|
104 return render(request, 'gcalendar/google_sync.html', {
|
bgneal@451
|
105 'current_app': self.admin_site.name,
|
bgneal@855
|
106 'cred_status': cred_status,
|
bgneal@855
|
107 'messages': msgs,
|
bgneal@451
|
108 'err_msg': err_msg,
|
bgneal@451
|
109 'events': events,
|
bgneal@458
|
110 })
|
bgneal@451
|
111
|
bgneal@855
|
112 def authorize(self, request):
|
bgneal@451
|
113 """
|
bgneal@855
|
114 This view generates the authorization URL and redirects the user to it.
|
bgneal@451
|
115 """
|
bgneal@454
|
116 site = Site.objects.get_current()
|
bgneal@991
|
117 callback_url = '%s://%s%s' % (
|
bgneal@991
|
118 settings.SITE_SCHEME,
|
bgneal@991
|
119 site.domain,
|
bgneal@855
|
120 reverse('admin:gcalendar-auth_return'))
|
bgneal@857
|
121 auth_url = oauth.get_auth_url(callback_url)
|
bgneal@855
|
122 return HttpResponseRedirect(auth_url)
|
bgneal@451
|
123
|
bgneal@855
|
124 def auth_return(self, request):
|
bgneal@451
|
125 """
|
bgneal@451
|
126 This view is called by Google after the user has authorized us access to
|
bgneal@855
|
127 their data. We call into the oauth module to process the authorization
|
bgneal@855
|
128 code and exchange it for tokens.
|
bgneal@451
|
129 """
|
bgneal@451
|
130 try:
|
bgneal@855
|
131 oauth.auth_return(request)
|
bgneal@855
|
132 except oauth.OAuthError as e:
|
bgneal@855
|
133 self.message_user(request, str(e), level=messages.ERROR)
|
bgneal@451
|
134
|
bgneal@451
|
135 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
|
bgneal@451
|
136
|
gremmie@1
|
137
|
gremmie@1
|
138 admin.site.register(Event, EventAdmin)
|