diff --git a/.project b/.project new file mode 100644 index 0000000..6d44593 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + pyfacebook + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + diff --git a/.pydevproject b/.pydevproject new file mode 100644 index 0000000..f47ecb1 --- /dev/null +++ b/.pydevproject @@ -0,0 +1,7 @@ + + + + +python 2.6 +Default + diff --git a/examples/examples.py b/examples/examples.py deleted file mode 100644 index 4a9e3cd..0000000 --- a/examples/examples.py +++ /dev/null @@ -1,107 +0,0 @@ -# ----------------------- -# Web application example -# ----------------------- - -def simple_web_app(request, api_key, secret_key): - fb = Facebook(api_key, secret_key, request.GET['auth_token']) - fb.auth.getSession() - - friend_ids = fb.friends.get() - info = fb.users.getInfo(friend_ids, ['name', 'pic']) - - print '' - for friend in info: - print '%(name)s' % friend - print '' - -def web_app(request): - """Get the user's friends and their pictures. This example uses - the Django web framework, but should be adaptable to others.""" - - # Get api_key and secret_key from a file - fb_file = open('facebook_keys.txt').readlines() - api_key = fb_file[0].strip() - secret_key = fb_file[1].strip() - fb = Facebook(api_key, secret_key) - - # Use the data from the cookie if present - if 'session_key' in request.session and 'uid' in request.session: - fb.session_key = request.session['session_key'] - fb.uid = request.session['uid'] - else: - - try: - fb.auth_token = request.GET['auth_token'] - except KeyError: - # Send user to the Facebook to login - return HttpResponseRedirect(fb.get_login_url()) - - # getSession sets the session_key and uid - # Store these in the cookie so we don't have to get them again - fb.auth.getSession() - request.session['session_key'] = fb.session_key - request.session['uid'] = fb.uid - - try: - friend_ids = fb.friends.get() - except FacebookError, e: - # Error 102 means the session has expired. - # Delete the cookie and send the user to Facebook to login - if e.info['code'] == u'102': - del request.session['session_key'] - del request.session['uid'] - return HttpResponseRedirect(fb.get_login_url()) - else: - # Other Facebook errors are possible too. Don't ignore them. - raise - - info = fb.users.getInfo(friend_ids, ['name', 'pic']) - # info is a list of dictionaries - - # you would never do this in an actual Django application, - # it's just an example of accessing the results. - links = [] - for friend in info: - html = '%(name)s' % friend - links.append(html) - - return render_to_response('template.html', {'links': links}) - -# --------------------------- -# Desktop application example -# --------------------------- - -def desktop_app(): - from facebook import Facebook - - # Get api_key and secret_key from a file - fbs = open(FB_SETTINGS).readlines() - facebook = Facebook(fbs[0].strip(), fbs[1].strip()) - - facebook.auth.createToken() - # Show login window - facebook.login() - - # Login to the window, then press enter - print 'After logging in, press enter...' - raw_input() - - facebook.auth.getSession() - info = facebook.users.getInfo([facebook.uid], ['name', 'birthday', 'affiliations', 'sex'])[0] - - for attr in info: - print '%s: %s' % (attr, info[attr]) - - friends = facebook.friends.get() - friends = facebook.users.getInfo(friends[0:5], ['name', 'birthday', 'relationship_status']) - - for friend in friends: - if 'birthday' in friend: - print friend['name'], 'has a birthday on', friend['birthday'], 'and is', friend['relationship_status'] - else: - print friend['name'], 'has no birthday and is', friend['relationship_status'] - - arefriends = facebook.friends.areFriends([friends[0]['uid']], [friends[1]['uid']]) - - photos = facebook.photos.getAlbums(friends[1]['uid']) - print photos diff --git a/examples/fbsample/.project b/examples/fbsample/.project new file mode 100644 index 0000000..6f8c900 --- /dev/null +++ b/examples/fbsample/.project @@ -0,0 +1,17 @@ + + + fbsample + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + diff --git a/examples/fbsample/.pydevproject b/examples/fbsample/.pydevproject new file mode 100644 index 0000000..f47ecb1 --- /dev/null +++ b/examples/fbsample/.pydevproject @@ -0,0 +1,7 @@ + + + + +python 2.6 +Default + diff --git a/examples/pyfacebook_sample/__init__.py b/examples/fbsample/__init__.py similarity index 100% rename from examples/pyfacebook_sample/__init__.py rename to examples/fbsample/__init__.py diff --git a/examples/fbsample/db.sqlite3 b/examples/fbsample/db.sqlite3 new file mode 100644 index 0000000..89fe4a3 Binary files /dev/null and b/examples/fbsample/db.sqlite3 differ diff --git a/examples/pyfacebook_sample/models.py b/examples/fbsample/fbapp/__init__.py similarity index 100% rename from examples/pyfacebook_sample/models.py rename to examples/fbsample/fbapp/__init__.py diff --git a/examples/fbsample/fbapp/models.py b/examples/fbsample/fbapp/models.py new file mode 100644 index 0000000..d51f3ba --- /dev/null +++ b/examples/fbsample/fbapp/models.py @@ -0,0 +1,45 @@ +from django.db import models + +# get_facebook_client lets us get the current Facebook object +# from outside of a view, which lets us have cleaner code +from facebook.djangofb import get_facebook_client + + +def _2int(d, k): + try: + d = d.__dict__ + except: + pass + + t = d.get(k, '') + if t == 'None': + t = 0 + else: + t = int(t) + return t + +class UserManager(models.Manager): + """Custom manager for a Facebook User.""" + + def get_current(self): + """Gets a User object for the logged-in Facebook user.""" + facebook = get_facebook_client() + user, created = self.get_or_create(id=_2int(facebook, 'uid')) + if created: + # we could do some custom actions for new users here... + pass + return user + +class User(models.Model): + """A simple User model for Facebook users.""" + + # We use the user's UID as the primary key in our database. + id = models.IntegerField(primary_key=True) + + # TODO: The data that you want to store for each user would go here. + # For this sample, we let users let people know their favorite progamming + # language, in the spirit of Extended Info. + language = models.CharField(max_length=64, default='Python') + + # Add the custom manager + objects = UserManager() diff --git a/examples/fbsample/fbapp/templates/canvas.fbml b/examples/fbsample/fbapp/templates/canvas.fbml new file mode 100644 index 0000000..6734dd1 --- /dev/null +++ b/examples/fbsample/fbapp/templates/canvas.fbml @@ -0,0 +1,22 @@ + + {% comment %} + We can use {{ fbuser }} to get at the current user. + {{ fbuser.id }} will be the user's UID, and {{ fbuser.language }} + is his/her favorite language (Python :-). + {% endcomment %} + Welcome, ! + + +
+ Your favorite language is {{ fbuser.language|escape }}. +

+ +
+

+ +
+ + +
+
+
diff --git a/examples/fbsample/fbapp/urls.py b/examples/fbsample/fbapp/urls.py new file mode 100644 index 0000000..2a31354 --- /dev/null +++ b/examples/fbsample/fbapp/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls.defaults import * + +urlpatterns = patterns('fbsample.fbapp.views', + (r'^$', 'canvas'), + # Define other pages you want to create here +) + diff --git a/examples/fbsample/fbapp/views.py b/examples/fbsample/fbapp/views.py new file mode 100644 index 0000000..34ad23a --- /dev/null +++ b/examples/fbsample/fbapp/views.py @@ -0,0 +1,37 @@ +from django.http import HttpResponse +from django.views.generic.simple import direct_to_template +#uncomment the following two lines and the one below +#if you dont want to use a decorator instead of the middleware +#from django.utils.decorators import decorator_from_middleware +#from facebook.djangofb import FacebookMiddleware + +# Import the Django helpers +import facebook.djangofb as facebook + +# The User model defined in models.py +from models import User + +# We'll require login for our canvas page. This +# isn't necessarily a good idea, as we might want +# to let users see the page without granting our app +# access to their info. See the wiki for details on how +# to do this. +#@decorator_from_middleware(FacebookMiddleware) +@facebook.require_login() +def canvas(request): + # Get the User object for the currently logged in user + user = User.objects.get_current() + + # Check if we were POSTed the user's new language of choice + if 'language' in request.POST: + user.language = request.POST['language'][:64] + user.save() + + # User is guaranteed to be logged in, so pass canvas.fbml + # an extra 'fbuser' parameter that is the User object for + # the currently logged in user. + return direct_to_template(request, 'canvas.fbml', extra_context={'fbuser': user}) + +@facebook.require_login() +def ajax(request): + return HttpResponse('hello world') diff --git a/examples/fbsample/manage.py b/examples/fbsample/manage.py new file mode 100644 index 0000000..b8c4be8 --- /dev/null +++ b/examples/fbsample/manage.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +from django.core.management import execute_manager +try: + import settings # Assumed to be in the same directory. +except ImportError: + import sys + sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__) + sys.exit(1) + +if __name__ == "__main__": + execute_manager(settings) diff --git a/examples/fbsample/run.bat b/examples/fbsample/run.bat new file mode 100644 index 0000000..d65cfa2 --- /dev/null +++ b/examples/fbsample/run.bat @@ -0,0 +1 @@ +python manage.py runserver 0.0.0.0:80 \ No newline at end of file diff --git a/examples/fbsample/settings.py b/examples/fbsample/settings.py new file mode 100644 index 0000000..342f0b2 --- /dev/null +++ b/examples/fbsample/settings.py @@ -0,0 +1,88 @@ +# Django settings for fbsample project. + +DEBUG = True +TEMPLATE_DEBUG = DEBUG + +ADMINS = ( + # ('Your Name', 'your_email@domain.com'), +) + +MANAGERS = ADMINS + +DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. +DATABASE_NAME = 'db.sqlite3' # Or path to database file if using sqlite3. +DATABASE_USER = '' # Not used with sqlite3. +DATABASE_PASSWORD = '' # Not used with sqlite3. +DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. +DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. + +# Local time zone for this installation. Choices can be found here: +# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name +# although not all choices may be available on all operating systems. +# If running in a Windows environment this must be set to the same as your +# system time zone. +TIME_ZONE = 'America/Chicago' + +# Language code for this installation. All choices can be found here: +# http://www.i18nguy.com/unicode/language-identifiers.html +LANGUAGE_CODE = 'en-us' + +SITE_ID = 1 + +# If you set this to False, Django will make some optimizations so as not +# to load the internationalization machinery. +USE_I18N = True + +# Absolute path to the directory that holds media. +# Example: "/home/media/media.lawrence.com/" +MEDIA_ROOT = '' + +# URL that handles the media served from MEDIA_ROOT. Make sure to use a +# trailing slash if there is a path component (optional in other cases). +# Examples: "http://media.lawrence.com", "http://example.com/media/" +MEDIA_URL = '' + +# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a +# trailing slash. +# Examples: "http://foo.com/media/", "/media/". +ADMIN_MEDIA_PREFIX = '/media/' + +# Make this unique, and don't share it with anybody. +SECRET_KEY = 'yg6zh@+u^w3agtjwy^da)#277d3j#a%3m@)pev8_j0ozztwe4+' + +# List of callables that know how to import templates from various sources. +TEMPLATE_LOADERS = ( + 'django.template.loaders.filesystem.load_template_source', + 'django.template.loaders.app_directories.load_template_source', +# 'django.template.loaders.eggs.load_template_source', +) + +MIDDLEWARE_CLASSES = ( + 'django.middleware.common.CommonMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + + 'facebook.djangofb.FacebookMiddleware', +) + +ROOT_URLCONF = 'fbsample.urls' + +TEMPLATE_DIRS = ( + # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". + # Always use forward slashes, even on Windows. + # Don't forget to use absolute paths, not relative paths. +) + +INSTALLED_APPS = ( + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.sites', + + 'fbsample.fbapp', +) + +# get it from here +# http://www.facebook.com/editapps.php?ref=mb +FACEBOOK_API_KEY = 'x' +FACEBOOK_SECRET_KEY = 'xx' diff --git a/examples/fbsample/urls.py b/examples/fbsample/urls.py new file mode 100644 index 0000000..d0065e1 --- /dev/null +++ b/examples/fbsample/urls.py @@ -0,0 +1,19 @@ +from django.conf.urls.defaults import * + +# Uncomment the next two lines to enable the admin: +# from django.contrib import admin +# admin.autodiscover() + +urlpatterns = patterns('', + # Example: + # (r'^fbsample/', include('fbsample.foo.urls')), + + # Uncomment the admin/doc line below and add 'django.contrib.admindocs' + # to INSTALLED_APPS to enable admin documentation: + # (r'^admin/doc/', include('django.contrib.admindocs.urls')), + + # Uncomment the next line to enable the admin: + # (r'^admin/', include(admin.site.urls)), + + (r'^fbsample/', include('fbsample.fbapp.urls')), +) diff --git a/examples/pyfacebook_sample/README b/examples/pyfacebook_sample/README deleted file mode 100644 index 14094f3..0000000 --- a/examples/pyfacebook_sample/README +++ /dev/null @@ -1,37 +0,0 @@ -This is a sample Django application for PyFacebook. - -To use this application, copy the entire folder to an existing -Django application. Then you need to edit the following settings: - -* In settings.py, make sure that TEMPLATE_LOADERS contains - 'django.template.loaders.app_directories.load_template_source'. This - is so that templates can be loaded from this template directory. - -* In settings.py, define FACEBOOK_API_KEY and FACEBOOK_SECRET_KEY to your - own values. - -* In settings.py, add the following line to the variable MIDDLEWARE_CLASSES: - 'facebook.djangofb.FacebookMiddleware'. This will attach a facebook object - to every incoming request. - -* However if you don't wan't to attach a facebook object to every incoming - request you can use the middleware as a decorator. - from django.utils.decorators import decorator_from_middleware - from facebook.djangofb import FacebookMiddleware - - @decorator_from_middleware(FacebookMiddleware) - @facebook.require_login() - def canvas(request): - ... - -* In urls.py, have something in your urlpatterns like: - (r'^facebook/', include('YOUR_PROJECT_NAME.pyfacebook_sample.urls')), - This will tell the sample application to live under /facebook/. - -* On the Facebook applications page, make sure that you set your callback - to the appropriate URL. In this example, it would be - 'http://YOUR_IP/facebook/canvas/', and DON'T FORGET THE TRAILING SLASH :-) - -* Change any occurrences of pyfacebook to your own application name. - -That should be about it... diff --git a/examples/pyfacebook_sample/templates/facebook/canvas.fbml b/examples/pyfacebook_sample/templates/facebook/canvas.fbml deleted file mode 100644 index ff735f7..0000000 --- a/examples/pyfacebook_sample/templates/facebook/canvas.fbml +++ /dev/null @@ -1,18 +0,0 @@ - - Welcome, {{ name }}! - - -
- This is the sample PyFaceBook application. -

- -
- Display something on your profile: -

- -
- - -
-
-
diff --git a/examples/pyfacebook_sample/urls.py b/examples/pyfacebook_sample/urls.py deleted file mode 100644 index f822349..0000000 --- a/examples/pyfacebook_sample/urls.py +++ /dev/null @@ -1,23 +0,0 @@ -from django.conf.urls.defaults import * - -# Hack to get the project name -project = __name__.split('.')[0] - -# You'd want to change this to wherever your app lives -urlpatterns = patterns(project + '.pyfacebook_sample.views', - # Some functionality - users can post text to their homepage - (r'^canvas/post/', 'post'), - - # For the mock AJAX functionality - (r'^canvas/ajax/', 'ajax'), - - # This is the canvas callback, i.e. what will be seen - # when you visit http://apps.facebook.com/. - (r'^canvas/', 'canvas'), - - # Extra callbacks can be set in the Facebook app settings - # page. For example, post_add will be called when a user - # has added the application. - (r'^post_add/', 'post_add'), - -) diff --git a/examples/pyfacebook_sample/views.py b/examples/pyfacebook_sample/views.py deleted file mode 100644 index 1bcc7d2..0000000 --- a/examples/pyfacebook_sample/views.py +++ /dev/null @@ -1,37 +0,0 @@ -from django.http import HttpResponse, HttpResponseRedirect -from django.shortcuts import render_to_response -#uncomment the following two lines and the one below -#if you dont want to use a decorator instead of the middleware -#from django.utils.decorators import decorator_from_middleware -#from facebook.djangofb import FacebookMiddleware - -import facebook.djangofb as facebook - -#@decorator_from_middleware(FacebookMiddleware) -@facebook.require_login() -def canvas(request): - # If you're using FBML, it'd be better to use than to do this - this is just as an example - values = request.facebook.users.getInfo([request.facebook.uid], ['first_name', 'is_app_user', 'has_added_app'])[0] - - name, is_app_user, has_added_app = values['first_name'], values['is_app_user'], values['has_added_app'] - - if has_added_app == '0': - return request.facebook.redirect(request.facebook.get_add_url()) - - return render_to_response('facebook/canvas.fbml', {'name': name}) - -@facebook.require_login() -def post(request): - request.facebook.profile.setFBML(request.POST['profile_text'], request.facebook.uid) - - return request.facebook.redirect(request.facebook.get_url('profile', id=request.facebook.uid)) - -@facebook.require_login() -def post_add(request): - request.facebook.profile.setFBML(uid=request.facebook.uid, profile='Congratulations on adding PyFaceBook. Please click on the PyFaceBook link on the left side to change this text.') - - return request.facebook.redirect('http://apps.facebook.com/pyfacebook/') - -@facebook.require_login() -def ajax(request): - return HttpResponse('hello world')