comparison gcalendar/admin.py @ 855:8743c566f712

WIP commit for converting to Google Calendar v3 API. This code should be enough to receive tokens from Google. See issue #80.
author Brian Neal <bgneal@gmail.com>
date Tue, 18 Nov 2014 21:09:24 -0600
parents ee87ea74d46b
children 9165edfb1709
comparison
equal deleted inserted replaced
854:1583a41511cc 855:8743c566f712
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 import gdata.client
15
16 from gcalendar.models import Event, AccessToken 14 from gcalendar.models import Event, AccessToken
17 from gcalendar.calendar import Calendar, CalendarError 15 from gcalendar.calendar import Calendar, CalendarError
18 from gcalendar import oauth 16 from gcalendar import oauth
19 17
20 import bio.badges 18 import bio.badges
21
22
23 SCOPES = ['https://www.google.com/calendar/feeds/']
24 19
25 20
26 class EventAdmin(admin.ModelAdmin): 21 class EventAdmin(admin.ModelAdmin):
27 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted', 22 list_display = ('what', 'user', 'start_date', 'where', 'date_submitted',
28 'status', 'is_approved', 'google_html') 23 'status', 'is_approved', 'google_html')
44 urls = super(EventAdmin, self).get_urls() 39 urls = super(EventAdmin, self).get_urls()
45 my_urls = patterns('', 40 my_urls = patterns('',
46 url(r'^google_sync/$', 41 url(r'^google_sync/$',
47 self.admin_site.admin_view(self.google_sync), 42 self.admin_site.admin_view(self.google_sync),
48 name="gcalendar-google_sync"), 43 name="gcalendar-google_sync"),
49 url(r'^fetch_auth/$', 44 url(r'^authorize/$',
50 self.admin_site.admin_view(self.fetch_auth), 45 self.admin_site.admin_view(self.authorize),
51 name="gcalendar-fetch_auth"), 46 name="gcalendar-authorize"),
52 url(r'^get_access_token/$', 47 url(r'^auth_return/$',
53 self.admin_site.admin_view(self.get_access_token), 48 self.admin_site.admin_view(self.auth_return),
54 name="gcalendar-get_access_token"), 49 name="gcalendar-auth_return"),
55 ) 50 )
56 return my_urls + urls 51 return my_urls + urls
57 52
58 def approve_events(self, request, qs): 53 def approve_events(self, request, qs):
59 """ 54 """
82 77
83 """ 78 """
84 # Get pending events 79 # Get pending events
85 events = Event.pending_events.all() 80 events = Event.pending_events.all()
86 81
87 # Attempt to get saved access token to the Google calendar 82 # Check status of credentials file
88 access_token = AccessToken.objects.get_token().access_token() 83 cred_status = oauth.check_credentials_status()
89 84
90 messages = [] 85 msgs = []
91 err_msg = '' 86 err_msg = ''
92 if request.method == 'POST': 87 if request.method == 'POST':
93 if access_token: 88 if access_token:
94 try: 89 try:
95 cal = Calendar(source=oauth.USER_AGENT, 90 cal = Calendar(source=oauth.USER_AGENT,
98 cal.sync_events(events) 93 cal.sync_events(events)
99 except CalendarError, e: 94 except CalendarError, e:
100 err_msg = e.msg 95 err_msg = e.msg
101 events = Event.pending_events.all() 96 events = Event.pending_events.all()
102 else: 97 else:
103 messages.append('All events processed successfully.') 98 msgs.append('All events processed successfully.')
104 events = Event.objects.none() 99 events = Event.objects.none()
105 100
106 return render(request, 'gcalendar/google_sync.html', { 101 return render(request, 'gcalendar/google_sync.html', {
107 'current_app': self.admin_site.name, 102 'current_app': self.admin_site.name,
108 'access_token': access_token, 103 'cred_status': cred_status,
109 'messages': messages, 104 'messages': msgs,
110 'err_msg': err_msg, 105 'err_msg': err_msg,
111 'events': events, 106 'events': events,
112 }) 107 })
113 108
114 def fetch_auth(self, request): 109 def authorize(self, request):
115 """ 110 """
116 This view fetches a request token and then redirects the user to 111 This view generates the authorization URL and redirects the user to it.
117 authorize it.
118
119 """ 112 """
120 site = Site.objects.get_current() 113 site = Site.objects.get_current()
121 callback_url = 'http://%s%s' % (site.domain, 114 callback_url = 'http://%s%s' % (site.domain,
122 reverse('admin:gcalendar-get_access_token')) 115 reverse('admin:gcalendar-auth_return'))
123 try: 116 auth_url = oauth.get_auth_url(request, callback_url)
124 auth_url = oauth.fetch_auth(request, SCOPES, callback_url) 117 return HttpResponseRedirect(auth_url)
125 except gdata.client.Error, e:
126 messages.error(request, str(e))
127 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
128 else:
129 return HttpResponseRedirect(auth_url)
130 118
131 def get_access_token(self, request): 119 def auth_return(self, request):
132 """ 120 """
133 This view is called by Google after the user has authorized us access to 121 This view is called by Google after the user has authorized us access to
134 their data. We call into the oauth module to upgrade the oauth token to 122 their data. We call into the oauth module to process the authorization
135 an access token. We then save the access token in the database and 123 code and exchange it for tokens.
136 redirect back to our admin Google sync view.
137
138 """ 124 """
139 try: 125 try:
140 access_token = oauth.get_access_token(request) 126 oauth.auth_return(request)
141 except gdata.client.Error, e: 127 except oauth.OAuthError as e:
142 messages.error(request, str(e)) 128 self.message_user(request, str(e), level=messages.ERROR)
143 else:
144 token = AccessToken.objects.get_token()
145 token.update(access_token)
146 token.save()
147 129
148 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync')) 130 return HttpResponseRedirect(reverse('admin:gcalendar-google_sync'))
149 131
150 132
151 admin.site.register(Event, EventAdmin) 133 admin.site.register(Event, EventAdmin)