/usr/lib/python2.7/dist-packages/facebook/webappfb.py is in python-facebook 0.svn20100209-3.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #
# webappfb - Facebook tools for Google's AppEngine "webapp" Framework
#
# Copyright (c) 2009, Max Battcher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author nor the names of its contributors may
# be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from google.appengine.api import memcache
from google.appengine.ext.webapp import RequestHandler
from facebook import Facebook
import yaml
"""
Facebook tools for Google AppEngine's object-oriented "webapp" framework.
"""
# This global configuration dictionary is for configuration variables
# for Facebook requests such as the application's API key and secret
# key. Defaults to loading a 'facebook.yaml' YAML file. This should be
# useful and familiar for most AppEngine development.
FACEBOOK_CONFIG = yaml.load(file('facebook.yaml', 'r'))
class FacebookRequestHandler(RequestHandler):
"""
Base class for request handlers for Facebook apps, providing useful
Facebook-related tools: a local
"""
def _fbconfig_value(self, name, default=None):
"""
Checks the global config dictionary and then for a class/instance
variable, using a provided default if no value is found.
"""
if name in FACEBOOK_CONFIG:
default = FACEBOOK_CONFIG[name]
return getattr(self, name, default)
def initialize(self, request, response):
"""
Initialize's this request's Facebook client.
"""
super(FacebookRequestHandler, self).initialize(request, response)
app_name = self._fbconfig_value('app_name', '')
api_key = self._fbconfig_value('api_key', None)
secret_key = self._fbconfig_value('secret_key', None)
self.facebook = Facebook(api_key, secret_key,
app_name=app_name)
require_app = self._fbconfig_value('require_app', False)
require_login = self._fbconfig_value('require_login', False)
need_session = self._fbconfig_value('need_session', False)
check_session = self._fbconfig_value('check_session', True)
self._messages = None
self.redirecting = False
if require_app or require_login:
if not self.facebook.check_session(request):
self.redirect(self.facebook.get_login_url(next=request.path))
self.redirecting = True
return
elif check_session:
self.facebook.check_session(request) # ignore response
# NOTE: require_app is deprecated according to modern Facebook login
# policies. Included for completeness, but unnecessary.
if require_app and not self.facebook.added:
self.redirect(self.facebook.get_add_url(next=request.path))
self.redirecting = True
return
if not (require_app or require_login) and need_session:
self.facebook.auth.getSession()
def redirect(self, url, **kwargs):
"""
For Facebook canvas pages we should use <fb:redirect /> instead of
a normal redirect.
"""
if self.facebook.in_canvas:
self.response.clear()
self.response.out.write('<fb:redirect url="%s" />' % (url, ))
else:
super(FacebookRequestHandler, self).redirect(url, **kwargs)
def add_user_message(self, kind, msg, detail='', time=15 * 60):
"""
Add a message to the current user to memcache.
"""
if self.facebook.uid:
key = 'messages:%s' % self.facebook.uid
self._messages = memcache.get(key)
message = {
'kind': kind,
'message': msg,
'detail': detail,
}
if self._messages is not None:
self._messages.append(message)
else:
self._messages = [message]
memcache.set(key, self._messages, time=time)
def get_and_delete_user_messages(self):
"""
Get all of the messages for the current user; removing them.
"""
if self.facebook.uid:
key = 'messages:%s' % self.facebook.uid
if not hasattr(self, '_messages') or self._messages is None:
self._messages = memcache.get(key)
memcache.delete(key)
return self._messages
return None
class FacebookCanvasHandler(FacebookRequestHandler):
"""
Request handler for Facebook canvas (FBML application) requests.
"""
def canvas(self, *args, **kwargs):
"""
This will be your handler to deal with Canvas requests.
"""
raise NotImplementedError()
def get(self, *args):
"""
All valid canvas views are POSTS.
"""
# TODO: Attempt to auto-redirect to Facebook canvas?
self.error(404)
def post(self, *args, **kwargs):
"""
Check a couple of simple safety checks and then call the canvas
handler.
"""
if self.redirecting: return
if not self.facebook.in_canvas:
self.error(404)
return
self.canvas(*args, **kwargs)
# vim: ai et ts=4 sts=4 sw=4
|