aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Leis <evan.leis@wearetopsecret.com>2011-12-06 13:29:05 -0700
committerEvan Leis <evan.leis@wearetopsecret.com>2011-12-06 13:29:05 -0700
commit8d8a4d76e3e66c9f616c3f6d8ba45f4858b29c76 (patch)
tree4bd7106f25620736249bdc49024ba1ae09110f05
parent256d2085f9f46c3695ea5a46c6af728d8115129b (diff)
downloadezfacebook-8d8a4d76e3e66c9f616c3f6d8ba45f4858b29c76.zip
ezfacebook-8d8a4d76e3e66c9f616c3f6d8ba45f4858b29c76.tar.gz
Added user middleware. FacebookRequestMiddlewaredevelop
-rw-r--r--README.md50
-rw-r--r--ezfacebook/example_settings.py3
-rw-r--r--ezfacebook/user/decorators.py16
-rw-r--r--ezfacebook/user/middleware.py58
-rw-r--r--setup.py2
5 files changed, 119 insertions, 10 deletions
diff --git a/README.md b/README.md
index 21c61fe..821cea4 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,8 @@ This package makes facebook integration easy for all kinds of web sites:
Included in the suite are:
- Easy to use facebook settings with debug settings
-- View decorators that extract information from requests, like signed_request, and facebook graph api.
+- View decorators that extract information from requests, like signed_request, and facebook graph api.
+- NEW: Middleware to apply those decorators to all functions for all defined facebook apps.
- Facebook script template tags
- Facebook Channel URL context variable and view
- Facebook Settings context processors
@@ -31,7 +32,8 @@ Included in the suite are:
)
MIDDLEWARE_CLASSES = (
- 'ezfacebook.helpers.middleware.IEIFrameApplicationMiddleware' # ezfacebook.helpers: Optional
+ 'ezfacebook.helpers.middleware.IEIFrameApplicationMiddleware', # ezfacebook.helpers: Optional
+ 'ezfacebook.user.middleware.FacebookRequestMiddleware', # ezfacebook.user: Optional
)
INSTALLED_APPS = (
@@ -257,6 +259,7 @@ Example:
from django.conf import settings
from django.core.urlresolvers import reverse
+ from django.views.generic.simple import direct_to_template
from ezfacebook.context.templatetags import absurl
from ezfacebook.user import decorators
@@ -294,6 +297,7 @@ Example:
from django.conf import settings
from django.core.urlresolvers import reverse
+ from django.views.generic.simple import direct_to_template
from ezfacebook.context.templatetags import absurl
from ezfacebook.user import decorators
@@ -308,4 +312,46 @@ Example:
return direct_to_template(request, 'success.html')
+## Middleware :: `ezfacebook.user.middleware`
+
+This middleware is used to apply the above decorators to all view functions.
+
+### FacebookRequestMiddleware :: `ezfacebook.user.middleware.FacebookRequestMiddleware`
+
+Put signed_request and graph in the request for each facebook app.
+Like this: `request.ezfb.my_app_name.graph` or `request.ezfb.my_app_name.signed_request`
+
+This cleans up your code so that decorators are not everywhere. However, it does add a little bit of inefficiency when
+your apps are not always being checked against. Decorators are ugly, but recommended.
+
+Example:
+
+ from django.views.generic.simple import direct_to_template
+
+ from myapp import models
+
+ def my_view(request):
+ """
+ An example demonstrating some possibilities of FacebookRequestMiddleware.
+ """
+
+ # Get or create a FacebookUser from the graph, if present.
+
+ graph = request.ezfb.my_first_fb_app.graph
+ if graph:
+ fbuser = models.FacebookUser.objects.get_or_create(facebook_guid=graph.facebook_guid)
+ else:
+ fbuser = None
+ return direct_to_template(request, 'my_first_fb_app/index-liked.html', {'fbuser': fbuser})
+
+ # OR!
+ # Show the index page, a different version if they like my_first_fb_app
+
+ signed_request = request.ezfb.my_second_fb_app.signed_request
+ if signed_request:
+ page_data = signed_request.get('page', None)
+ if page_data and page_data.get('liked', False):
+ return direct_to_template(request, 'my_second_fb_app/index-liked.html')
+ return direct_to_template(request, 'my_second_fb_app/index-unliked.html')
+
\ No newline at end of file
diff --git a/ezfacebook/example_settings.py b/ezfacebook/example_settings.py
index 5f4fb34..acc78bf 100644
--- a/ezfacebook/example_settings.py
+++ b/ezfacebook/example_settings.py
@@ -4,7 +4,8 @@ TEMPLATE_CONTEXT_PROCESSORS = (
)
MIDDLEWARE_CLASSES = (
- 'ezfacebook.helpers.middleware.IEIFrameApplicationMiddleware' # ezfacebook.helpers: Optional
+ 'ezfacebook.helpers.middleware.IEIFrameApplicationMiddleware', # ezfacebook.helpers: Optional
+ 'ezfacebook.user.middleware.FacebookRequestMiddleware', # ezfacebook.user: Optional
)
INSTALLED_APPS = (
diff --git a/ezfacebook/user/decorators.py b/ezfacebook/user/decorators.py
index 2b0f9e6..3052619 100644
--- a/ezfacebook/user/decorators.py
+++ b/ezfacebook/user/decorators.py
@@ -36,13 +36,14 @@ def graph_from_cookies(app_name):
return direct_to_template(request, 'success.html')
"""
app_settings = settings.app_settings(app_name)
+ injector_method = _graph_from_cookies_method(app_settings)
+ return _inject_arg_decorator(app_settings, injector_method)
+def _graph_from_cookies_method(app_settings):
if app_settings.debug_guid != False and app_settings.debug_token != False:
- # Use debug mode
- return _inject_arg_decorator(app_settings, _debug_graph_from_cookies)
+ return _debug_graph_from_cookies
else:
- # Use regular mode
- return _inject_arg_decorator(app_settings, _graph_from_cookies)
+ return _graph_from_cookies
def _debug_graph_from_cookies(request, app_settings):
graph = adapter._getFacebookGraphAPIClass()(app_settings.debug_guid, app_settings.debug_token)
@@ -88,11 +89,14 @@ def parse_signed_request(app_name):
return direct_to_template(request, 'unliked.html')
"""
app_settings = settings.app_settings(app_name)
+ injector_method = _parse_signed_request_method(app_settings)
+ return _inject_arg_decorator(app_settings, injector_method)
+def _parse_signed_request_method(app_settings):
if app_settings.debug_signed_request != False:
- return _inject_arg_decorator(app_settings, _debug_parse_signed_request)
+ return _debug_parse_signed_request
else:
- return _inject_arg_decorator(app_settings, _parse_signed_request)
+ return _parse_signed_request
def _debug_parse_signed_request(request, app_settings):
signed_request_data = app_settings.debug_signed_request
diff --git a/ezfacebook/user/middleware.py b/ezfacebook/user/middleware.py
new file mode 100644
index 0000000..9611d42
--- /dev/null
+++ b/ezfacebook/user/middleware.py
@@ -0,0 +1,58 @@
+from django.conf import settings
+
+from . import decorators
+
+class FacebookRequestMiddleware(object):
+ """
+ Put signed_request and graph in the request for each facebook app.
+
+ Example:
+
+ def my_view(request):
+ if request.ezfb.my_first_app.graph:
+ ''' do something with graph, or graph.facebook_guid '''
+ if request.ezfb.my_first_app.signed_request and request.ezfb.my_first_app.signed_request.get('page', {}).get('liked', False):
+ ''' The user likes this page, do something special! (Note signed_request is only available on the index page. '''
+
+ """
+ def process_request(self, request):
+ """
+ Add 'ezfb' to the request.
+ """
+ request.ezfb = self._clone_settings(request)
+
+ def _clone_settings(self, request):
+ """
+ For each FACEBOOK_SETTINGS, append signed_request and graph to a class with the settings name holding the information
+ """
+ class ezfb:
+ pass
+
+ for settings_class in self._iter_facebook_settings():
+ class settings:
+ signed_request = self._get_signed_request(request, settings_class)
+ graph = self._get_graph_from_cookies(request, settings_class)
+ setattr(ezfb, settings_class.__name__, settings)
+ return ezfb
+
+ def _iter_facebook_settings(self):
+ """
+ Generator that iterates through each FACEBOOK_SETTINGS class
+ """
+ fbsettings = settings.FACEBOOK_SETTINGS
+ for prop_name in dir(fbsettings):
+ settings_class = getattr(fbsettings, prop_name)
+ if hasattr(settings_class, 'app_id'):
+ yield settings_class
+
+ def _get_signed_request(self, request, settings_class):
+ """
+ Get the signed request from the request using the given Facebook settings class.
+ """
+ return decorators._parse_signed_request_method(settings_class)(request, settings_class)
+
+ def _get_graph_from_cookies(self, request, settings_class):
+ """
+ Get the graph api from the request using the given Facebook settings class.
+ """
+ return decorators._graph_from_cookies_method(settings_class)(request, settings_class)
diff --git a/setup.py b/setup.py
index f69d538..604443e 100644
--- a/setup.py
+++ b/setup.py
@@ -12,7 +12,7 @@ with open('README.md') as readme:
setup(
name='ezfacebook',
- version='0.75.2',
+ version='0.75.9',
description="Django Tools to use facebook seamlessly without having to build around it.",
long_description=long_description,
author='Evan Leis',