Skip to content

Commit

Permalink
Social registration application for django
Browse files Browse the repository at this point in the history
  • Loading branch information
flashingpumpkin committed Oct 5, 2009
0 parents commit 5eba5c7
Show file tree
Hide file tree
Showing 28 changed files with 1,052 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
@@ -0,0 +1 @@
* Alen Mujezinovic
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2009 Caffeinehit Ltd, Alen Mujezinovic and Contributors

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,3 @@
include AUTHORS
include LICENSE
include README
63 changes: 63 additions & 0 deletions README
@@ -0,0 +1,63 @@
==========================
Django Social Registration
==========================

Django Social Registration enables developers to add alternative registration
methods based on third party sites.

Requirements
============
django
oauth
python-openid

Installation
============

#. Add the *socialregistration* directory to your *PYTHON_PATH*.
#. Add *socialregistration* to your *INSTALLED_APPS* settings of Django.
#. Add *socialregistration.urls* to your *urls.py* file.

Configuration
=============

Facebook Connect
----------------
#. Add *FACEBOOK_API_KEY* and *FACEBOOK_SECRET_KEY* to your settings file
representing the keys you were given by Facebook.

#. Add tags to your template file:

{% load facebook_tags %}
{% facebook_button %}
{% facebook_js %}

Twitter
-------
#. Add the following variables to your *settings.py* file with the values you
were given by Twitter:

TWITTER_CONSUMER_KEY
TWITTER_CONSUMER_SECRET_KEY
TWITTER_REQUEST_TOKEN_URL
TWITTER_ACCESS_TOKEN_URL
TWITTER_AUTHORIZATION_URL

#. Add tags to your template file:
{% load twitter_tags %}
{% twitter_button %}

Other OAuth Services
--------------------
There is an example of how FriendFeed integration could work.
*socialregistration.models* provides a *FriendFeedProfile* model to save account
data, *socialregistration.auth* provides examples for different auth backends for
different service providers, *socialregistration.utils* provides a Twitter
and FriendFeed interface and *socialregistration.urls* provides examples based
on Twitter and FriendFeed how to hook in more OAuth based services.

OpenID
------
#. Add tags to your template file:
{% load openid_tags %}
{% openid_form %}
36 changes: 36 additions & 0 deletions setup.py
@@ -0,0 +1,36 @@
#!/usr/bin/env python

METADATA = dict(
name='django-socialregistration',
version='0.1',
author='Alen Mujezinovic',
author_email='alen@caffeinehit.com',
description='Django application enabling registration through a variety of APIs',
long_description=open('README').read(),
url='http://code.google.com/p/django-socialregistration',
keywords='django facebook twitter oauth openid registration',
)
SETUPTOOLS_METADATA = dict(
install_requires=['setuptools', 'django', 'oauth', 'python-openid'],
include_package_data=True,
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Software Development :: Libraries :: Python Modules',
'Environment :: Web Environment',
'Topic :: Internet',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Framework :: Django',
],
packages=['socialregistration']
)

if __name__ == '__main__':
try:
import setuptools
METADATA.update(SETUPTOOLS_METADATA)
setuptools.setup(**METADATA)
except ImportError:
import distutils.core
distutils.core.setup(**METADATA)
Empty file added socialregistration/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions socialregistration/admin.py
@@ -0,0 +1,12 @@
"""
Created on 22.09.2009
@author: alen
"""
from django.contrib import admin
from socialregistration.models import (FacebookProfile, TwitterProfile,
OpenIDProfile, OpenIDStore, OpenIDNonce)

admin.site.register([FacebookProfile, TwitterProfile, OpenIDProfile, OpenIDStore, OpenIDNonce])


47 changes: 47 additions & 0 deletions socialregistration/auth.py
@@ -0,0 +1,47 @@
"""
Created on 22.09.2009
@author: alen
"""
from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from socialregistration.models import (FacebookProfile, TwitterProfile,
FriendFeedProfile, OpenIDProfile)

class Auth(object):
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

class FacebookAuth(Auth):
def authenticate(self, uid=None):
try:
return FacebookProfile.objects.get(
uid=uid,
site=Site.objects.get_current()
).user
except:
return None

class TwitterAuth(Auth):
def authenticate(self, twitter_id=None):
try:
return TwitterProfile.objects.get(
twitter_id=twitter_id,
site=Site.objects.get_current()
).user
except:
return None

class OpenIDAuth(Auth):
def authenticate(self, identity=None):
try:
return OpenIDProfile.objects.get(
identity=identity,
site=Site.objects.get_current()
).user
except:
return None
35 changes: 35 additions & 0 deletions socialregistration/forms.py
@@ -0,0 +1,35 @@
"""
Created on 22.09.2009
@author: alen
"""
from django import forms
from django.utils.translation import gettext as _

from django.contrib.auth.models import User

class UserForm(forms.Form):
username = forms.RegexField(r'\w+', max_length=255,)
email = forms.EmailField(required=False)

def __init__(self, user, profile, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
self.user = user
self.profile = profile

def clean_username(self):
username = self.cleaned_data.get('username')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return username
else:
raise forms.ValidationError(_('This username is already in use.'))

def save(self):
self.user.username = self.cleaned_data.get('username')
self.user.email = self.cleaned_data.get('email')
self.user.save()
self.profile.user = self.user
self.profile.save()
return self.user
63 changes: 63 additions & 0 deletions socialregistration/models.py
@@ -0,0 +1,63 @@
"""
Created on 22.09.2009
@author: alen
"""

from django.db import models

from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
# Create your models here.


class FacebookProfile(models.Model):
user = models.ForeignKey(User)
site = models.ForeignKey(Site, default=Site.objects.get_current)
uid = models.CharField(max_length=255, blank=False, null=False)

def __unicode__(self):
return '%s: %s' % (self.user, self.uid)

def authenticate(self):
return authenticate(uid=self.uid)

class TwitterProfile(models.Model):
user = models.ForeignKey(User)
site = models.ForeignKey(Site, default=Site.objects.get_current)
twitter_id = models.PositiveIntegerField()

def __unicode__(self):
return '%s: %s' % (self.user, self.twitter_id)

def authenticate(self):
return authenticate(twitter_id=self.twitter_id)

class FriendFeedProfile(models.Model):
user = models.ForeignKey(User)
site = models.ForeignKey(Site, default=Site.objects.get_current)

class OpenIDProfile(models.Model):
user = models.ForeignKey(User)
site = models.ForeignKey(Site, default=Site.objects.get_current)
identity = models.TextField()

def authenticate(self):
return authenticate(identity=self.identity)

class OpenIDStore(models.Model):
site = models.ForeignKey(Site, default=Site.objects.get_current)
server_url = models.CharField(max_length=255)
handle = models.CharField(max_length=255)
secret = models.TextField()
issued = models.IntegerField()
lifetime = models.IntegerField()
assoc_type = models.TextField()

class OpenIDNonce(models.Model):
server_url = models.CharField(max_length=255)
timestamp = models.IntegerField()
salt = models.CharField(max_length=255)
date_created = models.DateTimeField(auto_now_add=True)

Empty file.
@@ -0,0 +1,7 @@

<form class="connect-button" name="login" method="post" action="{% if logged_in %}{% url facebook_connect %}{% else %}{% url facebook_login %}{% endif %}">
{% if next %}
<input type="hidden" name="next" value="{{ next }}" />
{% endif %}
<input type="image" onclick="facebookConnect(this.form);return false;" src="http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_large_long.gif" />
</form>
16 changes: 16 additions & 0 deletions socialregistration/templates/socialregistration/facebook_js.html
@@ -0,0 +1,16 @@
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>
<script type="text/javascript">
FB_RequireFeatures(["XFBML"],
function() {
FB.Facebook.init("{{ facebook_api_key }}", "{% url facebook_xd_receiver %}")
}
);
function facebookConnect(form){
FB.Connect.requireSession();
FB.Facebook.get_sessionState().waitUntilReady(
function(){
form.submit();
}
);
}
</script>
Empty file.
Empty file.
@@ -0,0 +1,4 @@
<form action="{% url openid_redirect %}" method="GET">
<input type="text" name="openid_provider" />
<input type="submit" value="Connect with OpenID" />
</form>
6 changes: 6 additions & 0 deletions socialregistration/templates/socialregistration/setup.html
@@ -0,0 +1,6 @@
<form action="." method="post">
<table>
{{ form }}
</table>
<input type="submit" value="save" />
</form>
@@ -0,0 +1,7 @@
{% load twitter_tags %}
<form class="connect-button" name="login" method="post" action="{% url twitter_redirect %}">
{% if next %}
<input type="hidden" name="next" value="{{ next }}" />
{% endif %}
<input type="image" onclick="this.form.submit();" src="http://apiwiki.twitter.com/f/1242697608/Sign-in-with-Twitter-lighter.png" />
</form>
10 changes: 10 additions & 0 deletions socialregistration/templates/socialregistration/xd_receiver.html
@@ -0,0 +1,10 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.debug.js" type="text/javascript">
</script>
</body>
</html>
5 changes: 5 additions & 0 deletions socialregistration/templatetags/__init__.py
@@ -0,0 +1,5 @@
"""
Created on 22.09.2009
@author: alen
"""
21 changes: 21 additions & 0 deletions socialregistration/templatetags/facebook_tags.py
@@ -0,0 +1,21 @@
"""
Created on 22.09.2009
@author: alen
"""
from django import template
from django.conf import settings

register = template.Library()

@register.inclusion_tag('socialregistration/facebook_js.html')
def facebook_js():
return {'facebook_api_key' : settings.FACEBOOK_API_KEY}

@register.inclusion_tag('socialregistration/facebook_button.html', takes_context=True)
def facebook_button(context):
if not 'request' in context:
raise AttributeError, 'Please add the ``django.core.context_processors.request`` context processors to your settings.CONTEXT_PROCESSORS set'
logged_in = context['request'].user.is_authenticated()
next = context['next'] if 'next' in context else None
return dict(next=next, logged_in=logged_in)

0 comments on commit 5eba5c7

Please sign in to comment.