annotate gpp/gcalendar/admin.py @ 452:c77359d0d951

Working on #220. Can't test locally, so committing in increments.
author Brian Neal <bgneal@gmail.com>
date Thu, 30 Jun 2011 02:13:36 +0000
parents 345825e6dcae
children 5b2114cec3e3
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@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@451 9 from django.core.urlresolvers import reverse
gremmie@1 10 from django.http import HttpResponse
bgneal@451 11 from django.http import HttpResponseRedirect
bgneal@451 12 from django.shortcuts import render_to_response
bgneal@451 13 from django.template import RequestContext
bgneal@451 14
bgneal@451 15 import gdata.client
gremmie@1 16
gremmie@1 17 from gcalendar.models import Event
bgneal@451 18 from gcalendar.forms import PasswordForm
bgneal@451 19 from gcalendar.calendar import Calendar
bgneal@451 20 from gcalendar.calendar import CalendarError
bgneal@451 21 from gcalendar import oauth
bgneal@451 22
bgneal@204 23 import bio.badges
gremmie@1 24
gremmie@1 25
bgneal@451 26 SCOPES = ['https://www.google.com/calendar/feeds/']
bgneal@451 27
bgneal@451 28
gremmie@1 29 class EventAdmin(admin.ModelAdmin):
gremmie@1 30 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted',
bgneal@228 31 'status', 'is_approved', 'google_html')
gremmie@1 32 list_filter = ('start_date', 'status')
bgneal@152 33 date_hierarchy = 'start_date'
gremmie@1 34 search_fields = ('what', 'where', 'description')
gremmie@1 35 raw_id_fields = ('user', )
bgneal@228 36 exclude = ('html', 'google_id', 'google_url')
gremmie@1 37 save_on_top = True
bgneal@152 38 actions = ('approve_events', )
bgneal@152 39
bgneal@152 40 pending_states = {
bgneal@152 41 Event.NEW: Event.NEW_APRV,
bgneal@152 42 Event.EDIT_REQ: Event.EDIT_APRV,
bgneal@152 43 Event.DEL_REQ: Event.DEL_APRV,
bgneal@152 44 }
gremmie@1 45
gremmie@1 46 def get_urls(self):
gremmie@1 47 urls = super(EventAdmin, self).get_urls()
gremmie@1 48 my_urls = patterns('',
bgneal@451 49 url(r'^google_sync/$',
bgneal@451 50 self.admin_site.admin_view(self.google_sync),
bgneal@451 51 name="gcalendar-google_sync"),
bgneal@451 52 url(r'^fetch_auth/$',
bgneal@451 53 self.admin_site.admin_view(self.fetch_auth),
bgneal@451 54 name="gcalendar-fetch_auth"),
bgneal@451 55 url(r'^get_access_token/$',
bgneal@451 56 self.admin_site.admin_view(self.get_access_token),
bgneal@451 57 name="gcalendar-get_access_token"),
gremmie@1 58 )
gremmie@1 59 return my_urls + urls
gremmie@1 60
bgneal@152 61 def approve_events(self, request, qs):
bgneal@152 62 """
bgneal@152 63 Ratchets the selected events forward to the approved state.
bgneal@152 64 Ignores events that aren't in the proper state.
bgneal@152 65 """
bgneal@389 66 count = 0
bgneal@152 67 for event in qs:
bgneal@152 68 if event.status in self.pending_states:
bgneal@152 69 event.status = self.pending_states[event.status]
bgneal@152 70 event.save()
bgneal@152 71 count += 1
bgneal@152 72
bgneal@204 73 if event.status == Event.NEW_APRV:
bgneal@204 74 bio.badges.award_badge(bio.badges.CALENDAR_PIN, event.user)
bgneal@204 75
bgneal@152 76 msg = "1 event was" if count == 1 else "%d events were" % count
bgneal@152 77 msg += " approved."
bgneal@152 78 self.message_user(request, msg)
bgneal@152 79
bgneal@152 80 approve_events.short_description = "Approve selected events"
bgneal@152 81
bgneal@451 82 def google_sync(self, request):
bgneal@451 83 """View to synchronize approved event changes with Google calendar."""
bgneal@451 84 events = Event.pending_events.all()
bgneal@451 85 messages = []
bgneal@451 86 err_msg = ''
bgneal@451 87 if request.method == 'POST':
bgneal@451 88 form = PasswordForm(request.POST)
bgneal@451 89 if form.is_valid():
bgneal@451 90 try:
bgneal@451 91 cal = Calendar(settings.GCAL_EMAIL,
bgneal@451 92 form.cleaned_data['password'],
bgneal@451 93 settings.GCAL_CALENDAR_ID)
bgneal@451 94 cal.sync_events(events)
bgneal@451 95 except CalendarError, e:
bgneal@451 96 err_msg = e.msg
bgneal@451 97 events = Event.pending_events.all()
bgneal@451 98 form = PasswordForm()
bgneal@451 99 else:
bgneal@451 100 messages.append('All events processed successfully.')
bgneal@451 101 events = Event.objects.none()
bgneal@451 102 form = PasswordForm()
bgneal@451 103
bgneal@451 104 else:
bgneal@451 105 form = PasswordForm()
bgneal@451 106
bgneal@451 107 return render_to_response('gcalendar/google_sync.html', {
bgneal@451 108 'current_app': self.admin_site.name,
bgneal@451 109 'messages': messages,
bgneal@451 110 'err_msg': err_msg,
bgneal@451 111 'events': events,
bgneal@451 112 'form': form,
bgneal@451 113 },
bgneal@451 114 context_instance=RequestContext(request))
bgneal@451 115
bgneal@451 116 def fetch_auth(self, request):
bgneal@451 117 """
bgneal@451 118 This view fetches a request token and then redirects the user to
bgneal@451 119 authorize it.
bgneal@451 120
bgneal@451 121 """
bgneal@451 122 callback_url = 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@451 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@451 144 # TODO: save access token
bgneal@451 145 pass
bgneal@451 146
bgneal@451 147 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
bgneal@451 148
gremmie@1 149
gremmie@1 150 admin.site.register(Event, EventAdmin)