Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanlister committed Nov 12, 2010
0 parents commit bcff584
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.pyc
.DS_Store
1 change: 1 addition & 0 deletions README
@@ -0,0 +1 @@
This app aims to replace Django's Authentication with Facebook.
Empty file added __init__.py
Empty file.
20 changes: 20 additions & 0 deletions decorators.py
@@ -0,0 +1,20 @@
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponseRedirect, HttpResponse
from django.utils.decorators import available_attrs
from django.utils.http import urlquote

def facebook_required(login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
""" Check the user has a valid facebook connection. """
if not login_url:
from django.conf import settings
login_url = settings.LOGIN_URL

def decorator(view_func):
def _wrapped_view(request, *args, **kwargs):
if request.facebook.uid:
return view_func(request, *args, **kwargs)
path = urlquote(request.get_full_path())
tup = login_url, redirect_field_name, path
#return HttpResponseRedirect('%s?%s=%s' % tup)
return HttpResponse('You must be logged in with Facebook.')
return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
38 changes: 38 additions & 0 deletions middleware.py
@@ -0,0 +1,38 @@
from django.conf import settings

class Facebook(object):
def __init__(self, user=None):
if user is None:
self.uid = None
else:
self.uid = user['uid']
self.user = user
self.graph = facebook.GraphAPI(user['access_token'])



class FacebookDebugCookieMiddleware(object):
""" Sets an imaginary cookie to make it easy to work from a development environment. """
def process_request(self, request):
cookie_name = "fbs_" + settings.FACEBOOK_APP_ID
request.COOKIES[cookie_name] = settings.FACEBOOK_DEBUG_COOKIE
return None


class FacebookDebugTokenMiddleware(object):
""" Forces a specific access token to be used. """
def process_request(self, request):
fb_user = {
'uid':u'212900950',
'access_token':'2227470867|2.FkEuuS_4N9cqPqqjd_FaMg__.3600.1289257200-212900950|xK2pUhlDtAlO1jp_P4TCx8TeLog',
}
request.facebook = Facebook(fb_user)
return None


class FacebookMiddleware(object):
""" Transparently integrate Django accounts with Facebook. """


class TokenDebuggingMiddleware(object):
def process_request(self, request):
12 changes: 12 additions & 0 deletions templates/facebook_init.html
@@ -0,0 +1,12 @@
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: {{ app_id }}, status: true, cookie: true, xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
Empty file added templatetags/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions templatetags/facebook.py
@@ -0,0 +1,12 @@
from django import template
from django.conf import settings
register = template.Library()

@register.inclusion_tag('facebook_init.html')
def facebook_init():
try:
app_id = settings.FACEBOOK_APP_ID
except AttributeError:
raise template.TemplateSyntaxError, "%r tag requires FACEBOOK_APP_ID to be configured." \
% token.contents.split()[0]
return {'app_id': app_id}
Empty file added views.py
Empty file.

0 comments on commit bcff584

Please sign in to comment.