mediapop / django-friendface

Getting Facebook to work for Django.
2 stars 0 forks source link

Django-Friendface

Django-friendface is a django application for interacting with Facebook. The goal is to have a complete implementation Facebook's API's in a django centric fashion and to support projects that have multiple Facebook Applications.

Setup

INSTALLED_APPS += ('friendface',)

Each application you want to use you need to setup in the django admin.
Once saved the application will request all of its details from Facebook.

Usage

{% load facebook %}

<a href="https://github.com/mediapop/django-friendface/blob/master/{% fburl 'my_app.views.index' %}">My view on Facebook!</a>

Authentication

AUTHENTICATION_BACKENDS += ('friendface.auth.backends.FacebookBackend',)

This will cause newly authenticated users to also create a regular Django user. Django will link to FacebookUser to the Users profile as facebook. This means that there needs to be a Profile on the User object.

class Profile(models.Model):
    user = models.OneToOneField('contrib.auth.User')
    facebook = models.OneToOneField('friendface.FacebookUser',
                                    null=True,
                                    blank=True)

Facebook Middlewares

MIDDLEWARE_CLASSES += (
    'friendface.middleware.FacebookApplicationMiddleware',
    'friendface.middleware.FacebookDecodingMiddleware',
    'friendface.middleware.DisableCsrfProtectionOnDecodedSignedRequest',
    'friendface.middleware.FacebookSignedRequestAuthenticationMiddleware',
)

FacebookApplicationMiddleware

Matches your request path with the Facebook applications paths and loads that application. If there's more than one application with the same path it will pick the most exact match (i.e. the shorter URL.) and set the facebook_application on the request.

def my_fancy_view(request):
    app = request.facebook_application
    response = app.request('http://www.mediapop.co')
    return HttpResponse(response['shares'])

FacebookDecodingMiddleware

Decodes the signed_request and makes it accessible via request.FACEBOOK

FacebookSignedRequestAuthenticationMiddleware

Reads the user_id from a decoded request and signs that user in.

To run test suite

friendface is prepared for testing both it's Javascript and Python components. Simply ensure that you got all Python requirements installed and that you got a recent version of node.js with npm installed. Then simply type:

$ make test

If necessary npm will install requirements for phantom.js.

Mobile detection

If the request has mobile set to True then the FacebookAppAuthMixin will act like MobileView and set the current session as Facebook mobile. This means that when the user access the app on http://apps.facebook.com then the user will be redirected to the bare domain of the app. The current normal workflow goes like:

To get detection install a mobile detection middleware and make sure it marks the as mobile = True.

Recommended mobile detection middleware

Install django-mobi and add it's middleware to our settings:

MIDDLEWARE_CLASSES += (
    'mobi.middleware.MobileDetectionMiddleware'
)