annotate gcalendar/admin.py @ 787:7e17b9e45356

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