Skip to content

Commit f58a64a

Browse files
author
purp
committed
rm old invalid example, add fbsample which works well on django-1.1
1 parent 13c3f2f commit f58a64a

File tree

20 files changed

+278
-222
lines changed

20 files changed

+278
-222
lines changed

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>pyfacebook</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

.pydevproject

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?>
3+
4+
<pydev_project>
5+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.6</pydev_property>
6+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
7+
</pydev_project>

examples/examples.py

-107
This file was deleted.

examples/fbsample/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>fbsample</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

examples/fbsample/.pydevproject

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?>
3+
4+
<pydev_project>
5+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.6</pydev_property>
6+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
7+
</pydev_project>

examples/fbsample/db.sqlite3

26 KB
Binary file not shown.

examples/fbsample/fbapp/models.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from django.db import models
2+
3+
# get_facebook_client lets us get the current Facebook object
4+
# from outside of a view, which lets us have cleaner code
5+
from facebook.djangofb import get_facebook_client
6+
7+
8+
def _2int(d, k):
9+
try:
10+
d = d.__dict__
11+
except:
12+
pass
13+
14+
t = d.get(k, '')
15+
if t == 'None':
16+
t = 0
17+
else:
18+
t = int(t)
19+
return t
20+
21+
class UserManager(models.Manager):
22+
"""Custom manager for a Facebook User."""
23+
24+
def get_current(self):
25+
"""Gets a User object for the logged-in Facebook user."""
26+
facebook = get_facebook_client()
27+
user, created = self.get_or_create(id=_2int(facebook, 'uid'))
28+
if created:
29+
# we could do some custom actions for new users here...
30+
pass
31+
return user
32+
33+
class User(models.Model):
34+
"""A simple User model for Facebook users."""
35+
36+
# We use the user's UID as the primary key in our database.
37+
id = models.IntegerField(primary_key=True)
38+
39+
# TODO: The data that you want to store for each user would go here.
40+
# For this sample, we let users let people know their favorite progamming
41+
# language, in the spirit of Extended Info.
42+
language = models.CharField(max_length=64, default='Python')
43+
44+
# Add the custom manager
45+
objects = UserManager()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<fb:header>
2+
{% comment %}
3+
We can use {{ fbuser }} to get at the current user.
4+
{{ fbuser.id }} will be the user's UID, and {{ fbuser.language }}
5+
is his/her favorite language (Python :-).
6+
{% endcomment %}
7+
Welcome, <fb:name uid="{{ fbuser.id }}" firstnameonly="true" useyou="false" />!
8+
</fb:header>
9+
10+
<div class="clearfix" style="float: left; border: 1px #d8dfea solid; padding: 10px 10px 10px 10px; margin-left: 30px; margin-bottom: 30px; width: 500px;">
11+
Your favorite language is {{ fbuser.language|escape }}.
12+
<br /><br />
13+
14+
<div class="grayheader clearfix">
15+
<br /><br />
16+
17+
<form action="." method="POST">
18+
<input type="text" name="language" value="{{ fbuser.language|escape }}" />
19+
<input type="submit" value="Change" />
20+
</form>
21+
</div>
22+
</div>

examples/fbsample/fbapp/urls.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.conf.urls.defaults import *
2+
3+
urlpatterns = patterns('fbsample.fbapp.views',
4+
(r'^$', 'canvas'),
5+
# Define other pages you want to create here
6+
)
7+

examples/fbsample/fbapp/views.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from django.http import HttpResponse
2+
from django.views.generic.simple import direct_to_template
3+
#uncomment the following two lines and the one below
4+
#if you dont want to use a decorator instead of the middleware
5+
#from django.utils.decorators import decorator_from_middleware
6+
#from facebook.djangofb import FacebookMiddleware
7+
8+
# Import the Django helpers
9+
import facebook.djangofb as facebook
10+
11+
# The User model defined in models.py
12+
from models import User
13+
14+
# We'll require login for our canvas page. This
15+
# isn't necessarily a good idea, as we might want
16+
# to let users see the page without granting our app
17+
# access to their info. See the wiki for details on how
18+
# to do this.
19+
#@decorator_from_middleware(FacebookMiddleware)
20+
@facebook.require_login()
21+
def canvas(request):
22+
# Get the User object for the currently logged in user
23+
user = User.objects.get_current()
24+
25+
# Check if we were POSTed the user's new language of choice
26+
if 'language' in request.POST:
27+
user.language = request.POST['language'][:64]
28+
user.save()
29+
30+
# User is guaranteed to be logged in, so pass canvas.fbml
31+
# an extra 'fbuser' parameter that is the User object for
32+
# the currently logged in user.
33+
return direct_to_template(request, 'canvas.fbml', extra_context={'fbuser': user})
34+
35+
@facebook.require_login()
36+
def ajax(request):
37+
return HttpResponse('hello world')

examples/fbsample/manage.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
from django.core.management import execute_manager
3+
try:
4+
import settings # Assumed to be in the same directory.
5+
except ImportError:
6+
import sys
7+
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__)
8+
sys.exit(1)
9+
10+
if __name__ == "__main__":
11+
execute_manager(settings)

examples/fbsample/run.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python manage.py runserver 0.0.0.0:80

examples/fbsample/settings.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Django settings for fbsample project.
2+
3+
DEBUG = True
4+
TEMPLATE_DEBUG = DEBUG
5+
6+
ADMINS = (
7+
# ('Your Name', '[email protected]'),
8+
)
9+
10+
MANAGERS = ADMINS
11+
12+
DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
13+
DATABASE_NAME = 'db.sqlite3' # Or path to database file if using sqlite3.
14+
DATABASE_USER = '' # Not used with sqlite3.
15+
DATABASE_PASSWORD = '' # Not used with sqlite3.
16+
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
17+
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
18+
19+
# Local time zone for this installation. Choices can be found here:
20+
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
21+
# although not all choices may be available on all operating systems.
22+
# If running in a Windows environment this must be set to the same as your
23+
# system time zone.
24+
TIME_ZONE = 'America/Chicago'
25+
26+
# Language code for this installation. All choices can be found here:
27+
# http://www.i18nguy.com/unicode/language-identifiers.html
28+
LANGUAGE_CODE = 'en-us'
29+
30+
SITE_ID = 1
31+
32+
# If you set this to False, Django will make some optimizations so as not
33+
# to load the internationalization machinery.
34+
USE_I18N = True
35+
36+
# Absolute path to the directory that holds media.
37+
# Example: "/home/media/media.lawrence.com/"
38+
MEDIA_ROOT = ''
39+
40+
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
41+
# trailing slash if there is a path component (optional in other cases).
42+
# Examples: "http://media.lawrence.com", "http://example.com/media/"
43+
MEDIA_URL = ''
44+
45+
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
46+
# trailing slash.
47+
# Examples: "http://foo.com/media/", "/media/".
48+
ADMIN_MEDIA_PREFIX = '/media/'
49+
50+
# Make this unique, and don't share it with anybody.
51+
SECRET_KEY = 'yg6zh@+u^w3agtjwy^da)#277d3j#a%3m@)pev8_j0ozztwe4+'
52+
53+
# List of callables that know how to import templates from various sources.
54+
TEMPLATE_LOADERS = (
55+
'django.template.loaders.filesystem.load_template_source',
56+
'django.template.loaders.app_directories.load_template_source',
57+
# 'django.template.loaders.eggs.load_template_source',
58+
)
59+
60+
MIDDLEWARE_CLASSES = (
61+
'django.middleware.common.CommonMiddleware',
62+
'django.contrib.sessions.middleware.SessionMiddleware',
63+
'django.contrib.auth.middleware.AuthenticationMiddleware',
64+
65+
'facebook.djangofb.FacebookMiddleware',
66+
)
67+
68+
ROOT_URLCONF = 'fbsample.urls'
69+
70+
TEMPLATE_DIRS = (
71+
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
72+
# Always use forward slashes, even on Windows.
73+
# Don't forget to use absolute paths, not relative paths.
74+
)
75+
76+
INSTALLED_APPS = (
77+
'django.contrib.auth',
78+
'django.contrib.contenttypes',
79+
'django.contrib.sessions',
80+
'django.contrib.sites',
81+
82+
'fbsample.fbapp',
83+
)
84+
85+
# get it from here
86+
# http://www.facebook.com/editapps.php?ref=mb
87+
FACEBOOK_API_KEY = 'x'
88+
FACEBOOK_SECRET_KEY = 'xx'

examples/fbsample/urls.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.conf.urls.defaults import *
2+
3+
# Uncomment the next two lines to enable the admin:
4+
# from django.contrib import admin
5+
# admin.autodiscover()
6+
7+
urlpatterns = patterns('',
8+
# Example:
9+
# (r'^fbsample/', include('fbsample.foo.urls')),
10+
11+
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
12+
# to INSTALLED_APPS to enable admin documentation:
13+
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
14+
15+
# Uncomment the next line to enable the admin:
16+
# (r'^admin/', include(admin.site.urls)),
17+
18+
(r'^fbsample/', include('fbsample.fbapp.urls')),
19+
)

0 commit comments

Comments
 (0)