From 7fa1747b38ee72128e058cf4c586a7fd988c45e8 Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sun, 13 Nov 2011 13:52:28 +0000 Subject: [PATCH 1/5] largely made standalone, should now be installable as a python package and should not need to be made part of your application. You should also no longer need to modify the source to get it working by adding your keys in. Also added parameters and settings for the callback urls. --- README.markdown | 23 ++++++++++++-------- setup.py | 38 ++++++++++++++++++++++++++++++++++ twython_django_oauth/models.py | 4 ++-- twython_django_oauth/urls.py | 4 ++-- twython_django_oauth/views.py | 35 +++++++++++++++---------------- 5 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 setup.py diff --git a/README.markdown b/README.markdown index 961f340..cd20b48 100644 --- a/README.markdown +++ b/README.markdown @@ -34,20 +34,25 @@ Twython also requires the (most excellent) OAuth2 library for handling OAuth tok Installation ----------------------------------------------------------------------------------------------------- -Copy the "twython_django_oauth" app into your project, and add it to your "INSTALLED_APPS" in your -settings.py file. If you wish to use the example template, feel free to copy that over as well. + +pip install git+http://github.com/zandeez/twython-django.git + +Add "twython_django_oauth" to your "INSTALLED_APPS" in your settings.py file. If you wish to use the example +template, feel free to copy that over as well. After you've done that, specify the following urlconf in your root urls.py: - (r'^your_url_extension/', include('your_app.twython_django_oauth.urls')), + (r'^your_url_extension/', include('twython_django_oauth.urls')), + +Add the following settings to settings.py: -If you're using this app bare-bones (i.e, just starting out), add the following to your settings.py: +TWITTER_KEY = 'your_key' +TWITTER_SECRET = 'your_secret' - AUTH_PROFILE_MODULE = 'twython_django_oauth.Profile' - -(Obviously, if you've got your own custom User/Profile setup going, this should all be pretty self explanatory -to integrate into your application. The OAuth handshake flow is well documented here, as well as how to use it -with Twython calls.) +LOGIN_URL='/your_url_extension/login' +LOGIN_REDIRECT_URL='/' +LOGOUT_URL='/your_url_extension/logout' +LOGOUT_REDIRECT_URL='/' Restart your Django app, and it should all work auto-magically. Build onwards from here if you're just starting out, or integrate this into your existing model setup if you're already Django-savvy. diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..684391e --- /dev/null +++ b/setup.py @@ -0,0 +1,38 @@ +#!/usr/bin/python + +import sys, os +from setuptools import setup +from setuptools import find_packages + +__author__ = 'Ryan McGrath ' +__version__ = '1.4.5' + +setup( + # Basic package information. + name = 'twython-django', + version = __version__, + packages = find_packages(), + + # Packaging options. + include_package_data = True, + + # Package dependencies. + install_requires = ['simplejson', 'oauth2', 'httplib2', 'twython'], + + # Metadata for PyPI. + author = 'Ryan McGrath', + author_email = 'ryan@venodesigns.net', + license = 'MIT License', + url = 'http://github.com/ryanmcgrath/twython-django/tree/master', + keywords = 'Django integration for twython', + description = 'An easy (and up to date) way to access Twitter data with Python.', + long_description = open('README.markdown').read(), + classifiers = [ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Topic :: Software Development :: Libraries :: Python Modules', + 'Topic :: Communications :: Chat', + 'Topic :: Internet' + ] +) \ No newline at end of file diff --git a/twython_django_oauth/models.py b/twython_django_oauth/models.py index bb3f5a1..0e51bc3 100644 --- a/twython_django_oauth/models.py +++ b/twython_django_oauth/models.py @@ -1,12 +1,12 @@ from django.db import models from django.contrib.auth.models import User -class Profile(models.Model): +class TwitterProfile(models.Model): """ An example Profile model that handles storing the oauth_token and oauth_secret in relation to a user. Adapt this if you have a current setup, there's really nothing special going on here. """ - user = models.ForeignKey(User) + user = models.OneToOneField(User) oauth_token = models.CharField(max_length = 200) oauth_secret = models.CharField(max_length = 200) diff --git a/twython_django_oauth/urls.py b/twython_django_oauth/urls.py index a8c8782..d331601 100644 --- a/twython_django_oauth/urls.py +++ b/twython_django_oauth/urls.py @@ -1,13 +1,13 @@ from django.conf.urls.defaults import * # your_app = name of your root djang app. -urlpatterns = patterns('your_app.twython_django_oauth.views', +urlpatterns = patterns('twython_django_oauth.views', # First leg of the authentication journey... (r'^login/?$', "begin_auth"), # Logout, if need be - (r'^/logout?$', "logout"), # Calling logout and what not + (r'^logout/?$', "logout"), # Calling logout and what not # This is where they're redirected to after authorizing - we'll # further (silently) redirect them again here after storing tokens and such. diff --git a/twython_django_oauth/views.py b/twython_django_oauth/views.py index 7b44d43..155c16f 100644 --- a/twython_django_oauth/views.py +++ b/twython_django_oauth/views.py @@ -3,24 +3,22 @@ from django.contrib.auth.models import User from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response from django.contrib.auth.decorators import login_required +from django.conf import settings +from django.core.urlresolvers import reverse from twython import Twython # If you've got your own Profile setup, see the note in the models file # about adapting this to your own setup. -from your_app.twython_django_oauth.models import Profile +from twython_django_oauth.models import TwitterProfile -# Move these into your settings.py if you're feeling adventurous. -CONSUMER_KEY = "YOUR CONSUMER KEY HERE" -CONSUMER_SECRET = "YOUR CONSUMER SECRET HERE" - -def logout(request): +def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL): """ Nothing hilariously hidden here, logs a user out. Strip this out if your application already has hooks to handle this. """ logout(request) - return HttpResponseRedirect('/') + return HttpResponseRedirect(request.build_absolute_uri(reverse(redirect_url))) def begin_auth(request): """ @@ -29,8 +27,9 @@ def begin_auth(request): """ # Instantiate Twython with the first leg of our trip. twitter = Twython( - twitter_token = CONSUMER_KEY, - twitter_secret = CONSUMER_SECRET + twitter_token = settings.TWITTER_KEY, + twitter_secret = settings.TWITTER_SECRET, + callback_url = request.build_absolute_uri(reverse('twython_django_oauth.views.thanks')) ) # Request an authorization url to send the user to... @@ -40,7 +39,7 @@ def begin_auth(request): request.session['request_token'] = auth_props return HttpResponseRedirect(auth_props['auth_url']) -def thanks(request): +def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL): """ A user gets redirected here after hitting Twitter and authorizing your app to use their data. @@ -53,10 +52,10 @@ def thanks(request): # Now that we've got the magic tokens back from Twitter, we need to exchange # for permanent ones and store them... twitter = Twython( - twitter_token = CONSUMER_KEY, - twitter_secret = CONSUMER_SECRET, + twitter_token = settings.TWITTER_KEY, + twitter_secret = settings.TWITTER_SECRET, oauth_token = request.session['request_token']['oauth_token'], - oauth_token_secret = request.session['request_token']['oauth_token_secret'] + oauth_token_secret = request.session['request_token']['oauth_token_secret'], ) # Retrieve the tokens we want... @@ -68,7 +67,7 @@ def thanks(request): except User.DoesNotExist: # We mock a creation here; no email, password is just the token, etc. user = User.objects.create_user(authorized_tokens['screen_name'], "fjdsfn@jfndjfn.com", authorized_tokens['oauth_token_secret']) - profile = Profile() + profile = TwitterProfile() profile.user = user profile.oauth_token = authorized_tokens['oauth_token'] profile.oauth_secret = authorized_tokens['oauth_token_secret'] @@ -79,17 +78,17 @@ def thanks(request): password = authorized_tokens['oauth_token_secret'] ) login(request, user) - return HttpResponseRedirect('/timeline') + return HttpResponseRedirect(redirect_url) def user_timeline(request): """ An example view with Twython/OAuth hooks/calls to fetch data about the user in question. Pretty self explanatory if you read through it... """ - user = request.user.get_profile() + user = request.user.twitterprofile twitter = Twython( - twitter_token = CONSUMER_KEY, - twitter_secret = CONSUMER_SECRET, + twitter_token = settings.TWITTER_KEY, + twitter_secret = settings.TWITTER_SECRET, oauth_token = user.oauth_token, oauth_token_secret = user.oauth_secret ) -- 2.39.5 From f418139ae176a74f4d2d018bfe01e0c47454642d Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sun, 13 Nov 2011 13:54:22 +0000 Subject: [PATCH 2/5] helps if you press the 'save' button --- README.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index cd20b48..9bb63ff 100644 --- a/README.markdown +++ b/README.markdown @@ -50,8 +50,9 @@ TWITTER_KEY = 'your_key' TWITTER_SECRET = 'your_secret' LOGIN_URL='/your_url_extension/login' -LOGIN_REDIRECT_URL='/' LOGOUT_URL='/your_url_extension/logout' + +LOGIN_REDIRECT_URL='/' LOGOUT_REDIRECT_URL='/' Restart your Django app, and it should all work auto-magically. Build onwards from here if you're -- 2.39.5 From 38081bee02979be1855f636f4cb5ad15c0f982e7 Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sun, 13 Nov 2011 13:56:51 +0000 Subject: [PATCH 3/5] corrections to README.markdown --- README.markdown | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.markdown b/README.markdown index 9bb63ff..bd179e0 100644 --- a/README.markdown +++ b/README.markdown @@ -47,12 +47,15 @@ After you've done that, specify the following urlconf in your root urls.py: Add the following settings to settings.py: TWITTER_KEY = 'your_key' + TWITTER_SECRET = 'your_secret' LOGIN_URL='/your_url_extension/login' + LOGOUT_URL='/your_url_extension/logout' LOGIN_REDIRECT_URL='/' + LOGOUT_REDIRECT_URL='/' Restart your Django app, and it should all work auto-magically. Build onwards from here if you're -- 2.39.5 From 50ac5263b7c6df3af55fe8ea9a631c4d8404c2f0 Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sun, 13 Nov 2011 14:48:46 +0000 Subject: [PATCH 4/5] added url names for easier external lookup --- twython_django_oauth/urls.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/twython_django_oauth/urls.py b/twython_django_oauth/urls.py index d331601..a017d81 100644 --- a/twython_django_oauth/urls.py +++ b/twython_django_oauth/urls.py @@ -4,16 +4,16 @@ from django.conf.urls.defaults import * urlpatterns = patterns('twython_django_oauth.views', # First leg of the authentication journey... - (r'^login/?$', "begin_auth"), + (r'^login/?$', "begin_auth", name="twitter_login"), # Logout, if need be - (r'^logout/?$', "logout"), # Calling logout and what not + (r'^logout/?$', "logout", name="twitter_logout"), # Calling logout and what not # This is where they're redirected to after authorizing - we'll # further (silently) redirect them again here after storing tokens and such. - (r'^thanks/?$', "thanks"), + (r'^thanks/?$', "thanks", name="twitter_callback"), # An example view using a Twython method with proper OAuth credentials. Clone # this view and url definition to get the rest of your desired pages/functionality. - (r'^user_timeline/?$', "user_timeline"), + (r'^user_timeline/?$', "user_timeline", name="twitter_timeline"), ) -- 2.39.5 From 278d5c3db0144350cf9ebfa15e5c7d54d291932b Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sun, 13 Nov 2011 15:16:16 +0000 Subject: [PATCH 5/5] fixed the url names, and the logout function that was recursively calling itself for shits and giggles. --- setup.py | 3 ++- twython_django_oauth/urls.py | 8 ++++---- twython_django_oauth/views.py | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 684391e..2614124 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,8 @@ setup( include_package_data = True, # Package dependencies. - install_requires = ['simplejson', 'oauth2', 'httplib2', 'twython'], + install_requires = ['simplejson', 'oauth2', 'httplib2', 'twython', 'django'], + provides = ['twython_django_oauth'], # Metadata for PyPI. author = 'Ryan McGrath', diff --git a/twython_django_oauth/urls.py b/twython_django_oauth/urls.py index a017d81..27a4164 100644 --- a/twython_django_oauth/urls.py +++ b/twython_django_oauth/urls.py @@ -4,16 +4,16 @@ from django.conf.urls.defaults import * urlpatterns = patterns('twython_django_oauth.views', # First leg of the authentication journey... - (r'^login/?$', "begin_auth", name="twitter_login"), + url(r'^login/?$', "begin_auth", name="twitter_login"), # Logout, if need be - (r'^logout/?$', "logout", name="twitter_logout"), # Calling logout and what not + url(r'^logout/?$', "logout", name="twitter_logout"), # Calling logout and what not # This is where they're redirected to after authorizing - we'll # further (silently) redirect them again here after storing tokens and such. - (r'^thanks/?$', "thanks", name="twitter_callback"), + url(r'^thanks/?$', "thanks", name="twitter_callback"), # An example view using a Twython method with proper OAuth credentials. Clone # this view and url definition to get the rest of your desired pages/functionality. - (r'^user_timeline/?$', "user_timeline", name="twitter_timeline"), + url(r'^user_timeline/?$', "user_timeline", name="twitter_timeline"), ) diff --git a/twython_django_oauth/views.py b/twython_django_oauth/views.py index 155c16f..4f0548a 100644 --- a/twython_django_oauth/views.py +++ b/twython_django_oauth/views.py @@ -1,4 +1,4 @@ -from django.contrib.auth import authenticate, login, logout +from django.contrib.auth import authenticate, login, logout as django_logout from django.contrib.auth.models import User from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response @@ -17,8 +17,8 @@ def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL): Nothing hilariously hidden here, logs a user out. Strip this out if your application already has hooks to handle this. """ - logout(request) - return HttpResponseRedirect(request.build_absolute_uri(reverse(redirect_url))) + django_logout(request) + return HttpResponseRedirect(request.build_absolute_uri(redirect_url)) def begin_auth(request): """ -- 2.39.5