Skip to content

Commit

Permalink
rm old invalid example, add fbsample which works well on django-1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
purp committed Mar 2, 2010
1 parent 13c3f2f commit f58a64a
Show file tree
Hide file tree
Showing 20 changed files with 278 additions and 222 deletions.
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>pyfacebook</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
7 changes: 7 additions & 0 deletions .pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?>

<pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.6</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
107 changes: 0 additions & 107 deletions examples/examples.py

This file was deleted.

17 changes: 17 additions & 0 deletions examples/fbsample/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>fbsample</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
7 changes: 7 additions & 0 deletions examples/fbsample/.pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?>

<pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.6</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
File renamed without changes.
Binary file added examples/fbsample/db.sqlite3
Binary file not shown.
File renamed without changes.
45 changes: 45 additions & 0 deletions examples/fbsample/fbapp/models.py
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 22 additions & 0 deletions examples/fbsample/fbapp/templates/canvas.fbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<fb:header>
{% 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, <fb:name uid="{{ fbuser.id }}" firstnameonly="true" useyou="false" />!
</fb:header>

<div class="clearfix" style="float: left; border: 1px #d8dfea solid; padding: 10px 10px 10px 10px; margin-left: 30px; margin-bottom: 30px; width: 500px;">
Your favorite language is {{ fbuser.language|escape }}.
<br /><br />

<div class="grayheader clearfix">
<br /><br />

<form action="." method="POST">
<input type="text" name="language" value="{{ fbuser.language|escape }}" />
<input type="submit" value="Change" />
</form>
</div>
</div>
7 changes: 7 additions & 0 deletions examples/fbsample/fbapp/urls.py
Original file line number Diff line number Diff line change
@@ -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
)

37 changes: 37 additions & 0 deletions examples/fbsample/fbapp/views.py
Original file line number Diff line number Diff line change
@@ -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')
11 changes: 11 additions & 0 deletions examples/fbsample/manage.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions examples/fbsample/run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python manage.py runserver 0.0.0.0:80
88 changes: 88 additions & 0 deletions examples/fbsample/settings.py
Original file line number Diff line number Diff line change
@@ -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'
19 changes: 19 additions & 0 deletions examples/fbsample/urls.py
Original file line number Diff line number Diff line change
@@ -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')),
)
Loading

0 comments on commit f58a64a

Please sign in to comment.