annotate gpp/gcalendar/admin.py @ 477:737a0c261a77

Add a link to John Blair's 50 years of surf music article on the banner.
author Brian Neal <bgneal@gmail.com>
date Fri, 09 Sep 2011 22:16:40 +0000
parents 9a4bffdf37c3
children ddd69a8e07c7
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@457 5 import datetime
bgneal@457 6
bgneal@451 7 from django.conf import settings
bgneal@451 8 from django.conf.urls.defaults import *
gremmie@1 9 from django.contrib import admin
bgneal@451 10 from django.contrib import messages
bgneal@454 11 from django.contrib.sites.models import Site
bgneal@451 12 from django.core.urlresolvers import reverse
gremmie@1 13 from django.http import HttpResponse
bgneal@451 14 from django.http import HttpResponseRedirect
bgneal@458 15 from django.shortcuts import render
bgneal@451 16 from django.template import RequestContext
bgneal@451 17
bgneal@451 18 import gdata.client
gremmie@1 19
bgneal@456 20 from gcalendar.models import Event, AccessToken
bgneal@458 21 from gcalendar.calendar import Calendar, 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@458 84 """
bgneal@458 85 View to synchronize approved event changes with Google calendar.
bgneal@458 86
bgneal@458 87 """
bgneal@458 88 # Get pending events
bgneal@451 89 events = Event.pending_events.all()
bgneal@458 90
bgneal@458 91 # Attempt to get saved access token to the Google calendar
bgneal@458 92 access_token = AccessToken.objects.get_token().access_token()
bgneal@458 93
bgneal@451 94 messages = []
bgneal@451 95 err_msg = ''
bgneal@451 96 if request.method == 'POST':
bgneal@458 97 if access_token:
bgneal@451 98 try:
bgneal@458 99 cal = Calendar(source=oauth.USER_AGENT,
bgneal@458 100 calendar_id=settings.GCAL_CALENDAR_ID,
bgneal@458 101 access_token=access_token)
bgneal@451 102 cal.sync_events(events)
bgneal@451 103 except CalendarError, e:
bgneal@451 104 err_msg = e.msg
bgneal@451 105 events = Event.pending_events.all()
bgneal@451 106 else:
bgneal@451 107 messages.append('All events processed successfully.')
bgneal@451 108 events = Event.objects.none()
bgneal@451 109
bgneal@458 110 return render(request, 'gcalendar/google_sync.html', {
bgneal@451 111 'current_app': self.admin_site.name,
bgneal@458 112 'access_token': access_token,
bgneal@451 113 'messages': messages,
bgneal@451 114 'err_msg': err_msg,
bgneal@451 115 'events': events,
bgneal@458 116 })
bgneal@451 117
bgneal@451 118 def fetch_auth(self, request):
bgneal@451 119 """
bgneal@451 120 This view fetches a request token and then redirects the user to
bgneal@451 121 authorize it.
bgneal@451 122
bgneal@451 123 """
bgneal@454 124 site = Site.objects.get_current()
bgneal@454 125 callback_url = 'http://%s%s' % (site.domain,
bgneal@454 126 reverse('admin:gcalendar-get_access_token'))
bgneal@451 127 try:
bgneal@451 128 auth_url = oauth.fetch_auth(request, SCOPES, callback_url)
bgneal@451 129 except gdata.client.Error, e:
bgneal@451 130 messages.error(request, str(e))
bgneal@451 131 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
bgneal@451 132 else:
bgneal@451 133 return HttpResponseRedirect(auth_url)
bgneal@451 134
bgneal@451 135 def get_access_token(self, request):
bgneal@451 136 """
bgneal@451 137 This view is called by Google after the user has authorized us access to
bgneal@456 138 their data. We call into the oauth module to upgrade the oauth token to
bgneal@451 139 an access token. We then save the access token in the database and
bgneal@451 140 redirect back to our admin Google sync view.
bgneal@451 141
bgneal@451 142 """
bgneal@451 143 try:
bgneal@451 144 access_token = oauth.get_access_token(request)
bgneal@451 145 except gdata.client.Error, e:
bgneal@451 146 messages.error(request, str(e))
bgneal@451 147 else:
bgneal@458 148 token = AccessToken.objects.get_token()
bgneal@458 149 token.update(access_token)
bgneal@456 150 token.save()
bgneal@451 151
bgneal@451 152 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
bgneal@451 153
gremmie@1 154
gremmie@1 155 admin.site.register(Event, EventAdmin)
bgneal@456 156 admin.site.register(AccessToken)