Permalink
Browse files

Major refactor, allowing even more functionality to be integrated int…

…o the User model.
  • Loading branch information...
1 parent 58284ec commit dd16f1fa419a76288eee0f589f79469bf27eb162 @rcrowley committed May 24, 2009
Showing with 75 additions and 89 deletions.
  1. +8 −8 decorators.py
  2. +11 −13 models.py
  3. +51 −60 utils.py
  4. +5 −8 views.py
View
@@ -3,17 +3,17 @@
from models import User
def wants_user(f):
- def new(*args, **kw):
+ def decorated(*args, **kwargs):
try: args[0].user = User.objects.get(pk=args[0].session['user_id'])
except: args[0].user = None
- return f(*args, **kw)
- return new
+ return f(*args, **kwargs)
+ return decorated
def needs_user(url):
- def decorator(f):
+ def decorated1(f):
@wants_user
- def new(*args, **kw):
+ def decorated2(*args, **kwargs):
if not args[0].user: return HttpResponseRedirect(reverse(url))
- else: return f(*args, **kw)
- return new
- return decorator
+ else: return f(*args, **kwargs)
+ return decorated2
+ return decorated1
View
@@ -25,17 +25,15 @@ def validate(self):
# is completely broken but easy to work around
def get_and_delete_messages(self): pass
- def tweet(self, status):
- consumer, connection = consumer_connection()
- try:
- obj = json.loads(fetch_response(request_oauth_resource(
- consumer, 'https://twitter.com/statuses/update.json',
- oauth.OAuthToken(self.oauth_token, self.oauth_token_secret),
- http_method='POST', parameters={'status': status}),
- connection))
- if 'id' in obj: return obj
- except: pass
- return False
+ def token(self):
+ return oauth.OAuthToken(self.oauth_token, self.oauth_token_secret)
+
+ def is_authorized(self): return is_authorized(self.token())
- def _oauth(self):
- return None # TODO Token
+ def tweet(self, status):
+ return api(
+ 'https://twitter.com/statuses/update.json',
+ self.token(),
+ http_method='POST',
+ status=status
+ )
View
111 utils.py
@@ -11,76 +11,67 @@
TWITTERAUTH_KEY = getattr(settings, 'TWITTERAUTH_KEY', 'OH HAI')
TWITTERAUTH_SECRET = getattr(settings, 'TWITTERAUTH_SECRET', 'OH NOES')
-def consumer_connection():
- return oauth.OAuthConsumer(TWITTERAUTH_KEY, TWITTERAUTH_SECRET), \
- httplib.HTTPSConnection('twitter.com')
+def consumer():
+ try: return consumer._consumer
+ except AttributeError:
+ consumer._consumer = oauth.OAuthConsumer(TWITTERAUTH_KEY, TWITTERAUTH_SECRET)
+ return consumer._consumer
-def request_oauth_resource(
- consumer,
+def connection():
+ try: return connection._connection
+ except AttributeError:
+ connection._connection = httplib.HTTPSConnection('twitter.com')
+ return connection._connection
+
+def oauth_request(
url,
- access_token,
+ token,
parameters=None,
signature_method=signature_method,
http_method='GET'
):
- oauth_request = oauth.OAuthRequest.from_consumer_and_token(
- consumer, token=access_token, http_url=url, parameters=parameters,
- http_method=http_method
+ req = oauth.OAuthRequest.from_consumer_and_token(
+ consumer(), token=token, http_url=url,
+ parameters=parameters, http_method=http_method
)
- oauth_request.sign_request(signature_method, consumer, access_token)
- return oauth_request
-
-
-def fetch_response(oauth_request, connection):
- url = oauth_request.to_url()
- connection.request(oauth_request.http_method, url)
- response = connection.getresponse()
- return response.read()
-
-def get_unauthorised_request_token(
- consumer,
- connection,
- signature_method=signature_method
-):
- oauth_request = oauth.OAuthRequest.from_consumer_and_token(
- consumer, http_url='https://twitter.com/oauth/request_token'
+ req.sign_request(signature_method, consumer(), token)
+ return req
+
+def oauth_response(req):
+ connection().request(req.http_method, req.to_url())
+ return connection().getresponse().read()
+
+def get_unauthorized_token(signature_method=signature_method):
+ req = oauth.OAuthRequest.from_consumer_and_token(
+ consumer(), http_url='https://twitter.com/oauth/request_token'
)
- oauth_request.sign_request(signature_method, consumer, None)
- resp = fetch_response(oauth_request, connection)
- token = oauth.OAuthToken.from_string(resp)
- return token
-
-
-def get_authorisation_url(
- consumer,
- token,
- signature_method=signature_method
-):
- oauth_request = oauth.OAuthRequest.from_consumer_and_token(
- consumer, token=token, http_url='http://twitter.com/oauth/authorize'
+ req.sign_request(signature_method, consumer(), None)
+ return oauth.OAuthToken.from_string(oauth_response(req))
+
+def get_authorization_url(token, signature_method=signature_method):
+ req = oauth.OAuthRequest.from_consumer_and_token(
+ consumer(), token=token,
+ http_url='http://twitter.com/oauth/authorize'
)
- oauth_request.sign_request(signature_method, consumer, token)
- return oauth_request.to_url()
-
-def exchange_request_token_for_access_token(
- consumer,
- connection,
- request_token,
- signature_method=signature_method
-):
- oauth_request = oauth.OAuthRequest.from_consumer_and_token(
- consumer, token=request_token,
+ req.sign_request(signature_method, consumer(), token)
+ return req.to_url()
+
+def get_authorized_token(token, signature_method=signature_method):
+ req = oauth.OAuthRequest.from_consumer_and_token(
+ consumer(), token=token,
http_url='https://twitter.com/oauth/access_token'
)
- oauth_request.sign_request(signature_method, consumer, request_token)
- resp = fetch_response(oauth_request, connection)
- return oauth.OAuthToken.from_string(resp)
-
-def is_authenticated(consumer, connection, token):
+ req.sign_request(signature_method, consumer(), token)
+ return oauth.OAuthToken.from_string(oauth_response(req))
+
+def api(url, token, http_method='GET', **kwargs):
try:
- obj = json.loads(fetch_response(request_oauth_resource(consumer,
- 'https://twitter.com/account/verify_credentials.json',
- token), connection))
- if 'screen_name' in obj: return obj
+ return json.loads(oauth_response(oauth_request(
+ url, token, http_method=http_method, parameters=kwargs
+ )))
except: pass
- return False
+ return None
+
+def is_authorized(token):
+ return api('https://twitter.com/account/verify_credentials.json',
+ token)
View
@@ -6,8 +6,6 @@
from models import User
from decorators import wants_user, needs_user
-CONSUMER, CONNECTION = consumer_connection()
-
@needs_user('auth_login')
def info(req):
if 'POST' == req.method:
@@ -23,9 +21,9 @@ def info(req):
@wants_user
def login(req):
if req.user: return HttpResponseRedirect('auth_info')
- token = get_unauthorised_request_token(CONSUMER, CONNECTION)
+ token = get_unauthorized_token()
req.session['token'] = token.to_string()
- return HttpResponseRedirect(get_authorisation_url(CONSUMER, token))
+ return HttpResponseRedirect(get_authorization_url(token))
def callback(req):
token = req.session.get('token', None)
@@ -38,12 +36,11 @@ def callback(req):
return render_to_response('callback.html', {
'mismatch': True
})
- token = exchange_request_token_for_access_token(CONSUMER,
- CONNECTION, token)
+ token = get_authorized_token(token)
# Actually login
- obj = is_authenticated(CONSUMER, CONNECTION, token)
- if obj is False:
+ obj = is_authorized(token)
+ if obj is None:
return render_to_response('callback.html', {
'username': True
})

0 comments on commit dd16f1f

Please sign in to comment.