Permalink
Browse files

Added tumblr support

  • Loading branch information...
1 parent 18f23a7 commit 83546097334f651156cec5850b83aefe8ce6d5d0 @flashingpumpkin flashingpumpkin committed Oct 4, 2011
@@ -6,6 +6,7 @@
from socialregistration.contrib.github.models import GithubProfile
from socialregistration.contrib.linkedin.models import LinkedInProfile
from socialregistration.contrib.openid.models import OpenIDProfile
+from socialregistration.contrib.tumblr.models import TumblrProfile
from socialregistration.contrib.twitter.models import TwitterProfile
def index(request):
@@ -17,4 +18,5 @@ def index(request):
linkedin=LinkedInProfile.objects.all(),
github=GithubProfile.objects.all(),
foursquare=FoursquareProfile.objects.all(),
+ tumblr=TumblrProfile.objects.all(),
), context_instance=RequestContext(request))
View
@@ -118,6 +118,7 @@
'socialregistration.contrib.github',
'socialregistration.contrib.facebook',
'socialregistration.contrib.foursquare',
+ 'socialregistration.contrib.tumblr',
'example.app',
)
@@ -155,6 +156,7 @@
'socialregistration.contrib.github.auth.GithubAuth',
'socialregistration.contrib.facebook.auth.FacebookAuth',
'socialregistration.contrib.foursquare.auth.FoursquareAuth',
+ 'socialregistration.contrib.tumblr.auth.TumblrAuth',
)
MIDDLEWARE_CLASSES = (
@@ -199,6 +201,9 @@
FOURSQUARE_CLIENT_SECRET = ''
FOURSQUARE_REQUEST_PERMISSIONS = ''
+# Add your tumblr API keys here
+TUMBLR_CONSUMER_KEY = ''
+TUMBLR_CONSUMER_SECRET_KEY = ''
SOCIALREGISTRATION_USE_HTTPS = False
SOCIALREGISTRATION_GENERATE_USERNAME = False
@@ -7,13 +7,14 @@
<h1>Hi, {{ request.user }}</h1>
{% endif %}
-{% load facebook twitter openid linkedin github foursquare %}
+{% load facebook twitter openid linkedin github foursquare tumblr %}
<ol>
<li>{% facebook_button %}</li>
<li>{% twitter_button %}</li>
<li>{% linkedin_button %}</li>
<li>{% github_button %}</li>
<li>{% foursquare_button %}</li>
+ <li>{% tumblr_button %}</li>
<li>{% openid_form %}</li>
<li>
<form action="{% url socialregistration:openid:redirect %}" method="post">
@@ -32,6 +33,7 @@
<li>LinkedIn: {{ linkedin.count }}</li>
<li>Github: {{ github.count }}</li>
<li>Foursquare: {{ foursquare.count }}</li>
+ <li>Tumblr: {{ tumblr.count }}</li>
<li>OpenID: {{ openid.count }}</li>
</ol>
@@ -20,6 +20,7 @@ class OAuth(Client):
_request_token = None
_access_token = None
_access_token_dict = None
+ _user_info = None
def __init__(self, access_token=None, access_token_secret=None):
self.consumer = oauth.Consumer(self.api_key, self.secret_key)
@@ -119,6 +120,7 @@ class OAuth2(Client):
scope = None
_access_token = None
+ _user_info = None
def __init__(self, access_token=None):
self._access_token = access_token
@@ -0,0 +1,14 @@
+from django.contrib.auth.backends import ModelBackend
+from django.contrib.sites.models import Site
+from socialregistration.contrib.tumblr.models import TumblrProfile
+
+
+class TumblrAuth(ModelBackend):
+ def authenticate(self, tumblr=None):
+ try:
+ return TumblrProfile.objects.get(
+ tumblr=tumblr,
+ site=Site.objects.get_current()
+ ).user
+ except TumblrProfile.DoesNotExist:
+ return None
@@ -0,0 +1,35 @@
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.core.urlresolvers import reverse
+from socialregistration.clients.oauth import OAuth
+import json
+import urlparse
+
+class Tumblr(OAuth):
+ api_key = getattr(settings, 'TUMBLR_CONSUMER_KEY', '')
+ secret_key = getattr(settings, 'TUMBLR_CONSUMER_SECRET_KEY', '')
+
+ request_token_url = 'http://www.tumblr.com/oauth/request_token'
+ access_token_url = 'http://www.tumblr.com/oauth/access_token'
+ auth_url = 'http://www.tumblr.com/oauth/authorize'
+
+ def get_callback_url(self):
+ if self.is_https():
+ return urlparse.urljoin(
+ 'https://%s' % Site.objects.get_current().domain,
+ reverse('socialregistration:tumblr:callback'))
+ return urlparse.urljoin(
+ 'http://%s' % Site.objects.get_current().domain,
+ reverse('socialregistration:tumblr:callback'))
+
+ def get_user_info(self):
+ if self._user_info is None:
+ self._user_info = json.loads(
+ self.request('http://api.tumblr.com/v2/user/info'))['response']['user']
+ return self._user_info
+
+ @staticmethod
+ def get_session_key():
+ return 'socialreg:tumblr'
+
+
@@ -0,0 +1,50 @@
+from django.contrib.auth import authenticate
+from django.contrib.auth.models import User
+from django.contrib.sites.models import Site
+from django.db import models
+from socialregistration.signals import connect
+
+class TumblrProfile(models.Model):
+ user = models.ForeignKey(User, unique=True)
+ site = models.ForeignKey(Site, default=Site.objects.get_current)
+ tumblr = models.CharField(max_length=100)
+
+ def __unicode__(self):
+ try:
+ return u'%s: %s' % (self.user, self.tumblr_name)
+ except User.DoesNotExist:
+ return u'None'
+
+ def authenticate(self):
+ return authenticate(tumblr=self.tumblr)
+
+class TumblrRequestToken(models.Model):
+ profile = models.OneToOneField(TumblrProfile, related_name='request_token')
+ oauth_token = models.CharField(max_length=80)
+ oauth_token_secret = models.CharField(max_length=80)
+
+class TumblrAccessToken(models.Model):
+ profile = models.OneToOneField(TumblrProfile, related_name='access_token')
+ oauth_token = models.CharField(max_length=80)
+ oauth_token_secret = models.CharField(max_length=80)
+
+def save_tumblr_token(sender, user, profile, client, **kwargs):
+ try:
+ TumblrRequestToken.objects.get(profile=profile).delete()
+ except TumblrRequestToken.DoesNotExist:
+ pass
+ try:
+ TumblrAccessToken.objects.get(profile=profile).delete()
+ except TumblrAccessToken.DoesNotExist:
+ pass
+
+ TumblrRequestToken.objects.create(profile=profile,
+ oauth_token=client.get_request_token().key,
+ oauth_token_secret=client.get_request_token().secret)
+
+ TumblrAccessToken.objects.create(profile=profile,
+ oauth_token=client.get_access_token().key,
+ oauth_token_secret=client.get_access_token().secret)
+
+connect.connect(save_tumblr_token, sender=TumblrProfile,
+ dispatch_uid='socialregistration_tumblr_token')
@@ -0,0 +1,3 @@
+{% if error %}
+ <p>{{ error }}</p>
+{% endif %}
@@ -0,0 +1,7 @@
+<form class="connect-button" name="login" method="post" action="{% url socialregistration:tumblr:redirect %}">
+{% csrf_token %}
+{% if next %}
+ <input type="hidden" name="next" value="{{ next }}" />
+{% endif %}
+<input type="image" onclick="this.form.submit();" src="{% if button %}{{ button }}{% else %}http://platform.tumblr.com/v1/share_2.png{% endif %}" />
+</form>
@@ -0,0 +1,6 @@
+from django import template
+from socialregistration.templatetags import button
+
+register = template.Library()
+
+register.tag('tumblr_button', button('socialregistration/tumblr/tumblr_button.html'))
@@ -0,0 +1 @@
+
@@ -0,0 +1,11 @@
+from django.conf import settings
+from django.conf.urls.defaults import *
+from socialregistration.contrib.tumblr.views import TumblrRedirect, \
+ TumblrCallback, TumblrSetup
+
+
+urlpatterns = patterns('',
+ url('^redirect/$', TumblrRedirect.as_view(), name='redirect'),
+ url('^callback/$', TumblrCallback.as_view(), name='callback'),
+ url('^setup/$', TumblrSetup.as_view(), name='setup'),
+)
@@ -0,0 +1,24 @@
+from django.core.urlresolvers import reverse
+from socialregistration.contrib.tumblr.client import Tumblr
+from socialregistration.contrib.tumblr.models import TumblrProfile
+from socialregistration.views import OAuthRedirect, OAuthCallback, SetupCallback
+
+class TumblrRedirect(OAuthRedirect):
+ client = Tumblr
+ template_name = 'socialregistration/tumblr/tumblr.html'
+
+class TumblrCallback(OAuthCallback):
+ client = Tumblr
+ template_name = 'socialregistration/tumblr/tumblr.html'
+
+ def get_redirect(self):
+ return reverse('socialregistration:tumblr:setup')
+
+class TumblrSetup(SetupCallback):
+ client = Tumblr
+ profile = TumblrProfile
+ template_name = 'socialregistration/tumblr/tumblr.html'
+
+ def get_lookup_kwargs(self, request, client):
+ return {'tumblr': client.get_user_info()['name']}
+
@@ -34,6 +34,11 @@
url(r'^foursquare/', include('socialregistration.contrib.foursquare.urls',
namespace='foursquare')))
+if 'socialregistration.contrib.tumblr' in settings.INSTALLED_APPS:
+ urlpatterns = urlpatterns + patterns('',
+ url(r'^tumblr/', include('socialregistration.contrib.tumblr.urls',
+ namespace='tumblr')))
+
urlpatterns = urlpatterns + patterns('',
url(r'^setup/$', Setup.as_view(), name='setup'),
url(r'^logout/$', Logout.as_view(), name='logout'),

0 comments on commit 8354609

Please sign in to comment.