comparison gcalendar/admin.py @ 857:9165edfb1709

For issue #80, use new Google Calendar v3 API.
author Brian Neal <bgneal@gmail.com>
date Sat, 22 Nov 2014 13:27:13 -0600
parents 8743c566f712
children 4aadaf3bc234
comparison
equal deleted inserted replaced
856:c2dfd1b1323e 857:9165edfb1709
9 from django.contrib.sites.models import Site 9 from django.contrib.sites.models import Site
10 from django.core.urlresolvers import reverse 10 from django.core.urlresolvers import reverse
11 from django.http import HttpResponseRedirect 11 from django.http import HttpResponseRedirect
12 from django.shortcuts import render 12 from django.shortcuts import render
13 13
14 from gcalendar.models import Event, AccessToken 14 from gcalendar.models import Event
15 from gcalendar.calendar import Calendar, CalendarError 15 from gcalendar.calendar import Calendar, CalendarError
16 from gcalendar import oauth 16 from gcalendar import oauth
17 17
18 import bio.badges 18 import bio.badges
19 19
20 20
21 class EventAdmin(admin.ModelAdmin): 21 class EventAdmin(admin.ModelAdmin):
22 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted', 22 list_display = ['what', 'user', 'start_date', 'where', 'date_submitted',
23 'status', 'is_approved', 'google_html') 23 'status', 'is_approved', 'google_html']
24 list_filter = ('start_date', 'status') 24 list_filter = ['start_date', 'status']
25 date_hierarchy = 'start_date' 25 date_hierarchy = 'start_date'
26 search_fields = ('what', 'where', 'description') 26 search_fields = ['what', 'where', 'description']
27 raw_id_fields = ('user', ) 27 raw_id_fields = ['user']
28 exclude = ('html', 'google_id', 'google_url') 28 exclude = ['html', 'google_id', 'google_url']
29 save_on_top = True 29 save_on_top = True
30 actions = ('approve_events', ) 30 actions = ['approve_events']
31 31
32 pending_states = { 32 pending_states = {
33 Event.NEW: Event.NEW_APRV, 33 Event.NEW: Event.NEW_APRV,
34 Event.EDIT_REQ: Event.EDIT_APRV, 34 Event.EDIT_REQ: Event.EDIT_APRV,
35 Event.DEL_REQ: Event.DEL_APRV, 35 Event.DEL_REQ: Event.DEL_APRV,
83 cred_status = oauth.check_credentials_status() 83 cred_status = oauth.check_credentials_status()
84 84
85 msgs = [] 85 msgs = []
86 err_msg = '' 86 err_msg = ''
87 if request.method == 'POST': 87 if request.method == 'POST':
88 if access_token: 88 credentials = oauth.get_credentials()
89 if credentials:
89 try: 90 try:
90 cal = Calendar(source=oauth.USER_AGENT, 91 cal = Calendar(calendar_id=settings.GCAL_CALENDAR_ID,
91 calendar_id=settings.GCAL_CALENDAR_ID, 92 credentials=credentials)
92 access_token=access_token)
93 cal.sync_events(events) 93 cal.sync_events(events)
94 except CalendarError, e: 94 except CalendarError, e:
95 err_msg = e.msg 95 err_msg = str(e)
96 events = Event.pending_events.all() 96 events = Event.pending_events.all()
97 else: 97 else:
98 msgs.append('All events processed successfully.') 98 msgs.append('All events processed successfully.')
99 events = Event.objects.none() 99 events = Event.objects.none()
100 else:
101 self.message_user(request, "Invalid or missing credentials",
102 level=messages.ERROR)
100 103
101 return render(request, 'gcalendar/google_sync.html', { 104 return render(request, 'gcalendar/google_sync.html', {
102 'current_app': self.admin_site.name, 105 'current_app': self.admin_site.name,
103 'cred_status': cred_status, 106 'cred_status': cred_status,
104 'messages': msgs, 107 'messages': msgs,
111 This view generates the authorization URL and redirects the user to it. 114 This view generates the authorization URL and redirects the user to it.
112 """ 115 """
113 site = Site.objects.get_current() 116 site = Site.objects.get_current()
114 callback_url = 'http://%s%s' % (site.domain, 117 callback_url = 'http://%s%s' % (site.domain,
115 reverse('admin:gcalendar-auth_return')) 118 reverse('admin:gcalendar-auth_return'))
116 auth_url = oauth.get_auth_url(request, callback_url) 119 auth_url = oauth.get_auth_url(callback_url)
117 return HttpResponseRedirect(auth_url) 120 return HttpResponseRedirect(auth_url)
118 121
119 def auth_return(self, request): 122 def auth_return(self, request):
120 """ 123 """
121 This view is called by Google after the user has authorized us access to 124 This view is called by Google after the user has authorized us access to
129 132
130 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync')) 133 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
131 134
132 135
133 admin.site.register(Event, EventAdmin) 136 admin.site.register(Event, EventAdmin)
134 admin.site.register(AccessToken)