Skip to content

Commit

Permalink
updated do_token_dance to take an optional args hash and do the no ne…
Browse files Browse the repository at this point in the history
…w users dance; FlickrApp exceptions
  • Loading branch information
thisisaaronland committed Oct 27, 2009
1 parent 66c0851 commit c39bd58
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 18 deletions.
79 changes: 64 additions & 15 deletions FlickrApp/__init__.py
Expand Up @@ -2,8 +2,7 @@
from google.appengine.api import memcache

from django.utils import simplejson

import FlickrApp.User as User
import User

import os
import binascii
Expand All @@ -27,6 +26,27 @@
#
#

class FlickrAppException (Exception) :
def __init__(self, value):
self.value = value

def __str__(self):
return repr(self.value)

class FlickrAppAPIException (FlickrAppException) :
def __init__(self, value):
self.value = value

class FlickrAppCrumbException (FlickrAppException) :
def __init__(self, value):
self.value = value

class FlickrAppNewUserException (FlickrAppException) :
def __init__(self, value=''):
self.value = value

#

class FlickrApp (webapp.RequestHandler) :

"""FlickrApp is a simple base class to use with Google App Engine (GAE)
Expand Down Expand Up @@ -174,7 +194,7 @@ def do_flickr_auth (self, min_perms=None, redir=None) :
API Auth endpoint where the user will be prompted to approve
your request for a Flickr token (with 'min_perms' permissions).
"""

args = {'api_key' : self._api_key}

extra = []
Expand Down Expand Up @@ -204,7 +224,7 @@ def do_flickr_auth (self, min_perms=None, redir=None) :
def flickr_sign_args (self, args) :
return flickr.sign_args(self._api_secret, args)

def do_token_dance (self, perms=None) :
def do_token_dance (self, **args) :

"""
This is the method you should call from your 'auth' handler; that
Expand All @@ -224,16 +244,40 @@ class TokenFrobHandler (FlickrApp) :
# Assume __init__ here
def get (self):
if not self.do_token_dance() :
self.response.out.write("OH NOES! SOMETHING WENT WRONG!")
def get (self):
try :
new_users = True
self.do_token_dance(allow_new_users=new_users)
except FlickrApp.FlickrAppNewUserException, e :
self.assign('error', 'no_new_users')
except FlickrApp.FlickrAppAPIException, e :
self.assign('error', 'api_error')
except FlickrApp.FlickrAppException, e :
self.assign('error', 'app_error')
self.assign('error_message', e)
except Exception, e:
self.assign('error', 'unknown')
self.assign('error_message', e)
self.display("token_dance.html")
return
"""

frob = self.request.get('frob')

if not frob or frob == '' :
return False
raise FlickrAppException('Missing frob!')

extra = self.request.get('extra')
e_params = {}
Expand All @@ -247,15 +291,16 @@ def get (self):
crumb = urllib.unquote(e_params['crumb'])

if not self.validate_crumb(None, 'flickrauth', crumb) :
return False
raise FlickrAppCrumbException('Invalid crumb')

#

args = {'frob': frob, 'check_response' : True}
rsp = self.api_call('flickr.auth.getToken', args)
api_args = {'frob': frob, 'check_response' : True}

rsp = self.api_call('flickr.auth.getToken', api_args)

if not rsp :
return False
raise FlickrAppAPIException('Failed to get token')

token = rsp['auth']['token']['_content']
name = rsp['auth']['user']['username']
Expand All @@ -264,7 +309,11 @@ def get (self):
user_perms = self.perms_map[perms]

user = User.get_user_by_nsid(nsid)


if not user :
if args.has_key('allow_new_users') and not args['allow_new_users'] :
raise FlickrAppNewUserException()

if not user :

args = {
Expand Down Expand Up @@ -342,7 +391,7 @@ def proxy_api_call (self, method, args, ttl=0) :

if cache :
return cache

rsp = self.api_call(method, args)

if not rsp :
Expand Down
23 changes: 20 additions & 3 deletions example/HelloWorld/__init__.py
Expand Up @@ -56,10 +56,27 @@ def get (self) :

class TokenDance (HelloWorldApp) :

def get (self):
if not self.do_token_dance() :
self.response.out.write('Ack! Flickr Auth Token dance failed.')
def get (self):

try :

new_users = True
self.do_token_dance(allow_new_users=new_users)

except FlickrApp.FlickrAppNewUserException, e :
self.response.out.write('New user signups are currently disabled.')

except FlickrApp.FlickrAppAPIException, e :
self.response.out.write('The Flickr API is being cranky.')

except FlickrApp.FlickrAppException, e :
self.response.out.write('Application error: %s' % e)

except Exception, e:
self.response.out.write('Unknown error: %s' % e)

return

# This is where you send a user to sign in. If they are not
# already authed then the application will take care generating
# Flickr Auth frobs and other details.
Expand Down

0 comments on commit c39bd58

Please sign in to comment.