Skip to content

Commit

Permalink
make PEP8 compliant (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevipriyaSarkar authored and sciyoshi committed Oct 2, 2016
1 parent ebc4e7c commit ec2bbfb
Show file tree
Hide file tree
Showing 17 changed files with 626 additions and 390 deletions.
77 changes: 55 additions & 22 deletions bin/djangofb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python

if __name__ == '__main__':
import sys, os, re
import sys
import os
import re

def usage():
sys.stderr.write('Usage: djangofb.py startapp <appname>\n')
Expand All @@ -17,9 +19,14 @@ def usage():

try:
sys.path.insert(0, os.getcwd())
import settings # Assumed to be in the same directory or current directory.
# Assumed to be in the same directory or current directory.
import settings
except ImportError:
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r or in the current directory. 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.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r "
"or in the current directory. It appears you've customized things.\n"
"You'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)

from django.core import management
Expand All @@ -38,28 +45,39 @@ def usage():
parent_dir = os.path.basename(project_dir)
project_name = os.path.basename(directory)
if app_name == project_name:
sys.stderr.write(style.ERROR('Error: You cannot create an app with the same name (%r) as your project.\n' % app_name))
sys.stderr.write(
style.ERROR(
'Error: You cannot create an app with the same name (%r) as your project.\n' %
app_name))
sys.exit(1)
if app_name == 'facebook':
sys.stderr.write(style.ERROR('Error: You cannot name your app "facebook", since this can cause conflicts with imports in Python < 2.5.\n'))
sys.stderr.write(style.ERROR(
'Error: You cannot name your app "facebook", '
'since this can cause conflicts with imports in Python < 2.5.\n'))
sys.exit(1)
if not re.search(r'^\w+$', app_name):
sys.stderr.write(style.ERROR('Error: %r is not a valid app name. Please use only numbers, letters and underscores.\n' % (app_name)))
sys.stderr.write(
style.ERROR(
'Error: %r is not a valid app name. Please use only numbers, letters and underscores.\n' %
app_name))
sys.exit(1)

top_dir = os.path.join(directory, app_name)
try:
os.mkdir(top_dir)
except OSError, e:
except OSError as e:
sys.stderr.write(style.ERROR("Error: %s\n" % e))
sys.exit(1)

import facebook

template_dir = os.path.join(facebook.__path__[0], 'djangofb', 'default_app')
template_dir = os.path.join(
facebook.__path__[0],
'djangofb',
'default_app')

sys.stderr.write('Creating Facebook application %r...\n' % app_name)

for d, subdirs, files in os.walk(template_dir):
relative_dir = d[len(template_dir) + 1:]
if relative_dir:
Expand All @@ -73,32 +91,47 @@ def usage():
f_old = open(path_old, 'r')
f_new = open(path_new, 'w')
sys.stderr.write('Writing %s...\n' % path_new)
f_new.write(f_old.read().replace('{{ project }}', project_name).replace('{{ app }}', app_name))
f_new.write(
f_old.read().replace(
'{{ project }}',
project_name).replace(
'{{ app }}',
app_name))
f_new.close()
f_old.close()

sys.stderr.write('Done!\n\n')

from django.conf import settings

need_api_key = not hasattr(settings, 'FACEBOOK_API_KEY')
need_middleware = not 'facebook.djangofb.FacebookMiddleware' in settings.MIDDLEWARE_CLASSES
need_loader = not 'django.template.loaders.app_directories.load_template_source' in settings.TEMPLATE_LOADERS
need_install_app = not '%s.%s' % (project_name, app_name) in settings.INSTALLED_APPS
need_middleware = 'facebook.djangofb.FacebookMiddleware' not in settings.MIDDLEWARE_CLASSES
need_loader = 'django.template.loaders.app_directories.load_template_source' not in settings.TEMPLATE_LOADERS
need_install_app = not '%s.%s' % (
project_name, app_name) in settings.INSTALLED_APPS

if need_api_key or need_middleware or need_loader or need_install_app:
sys.stderr.write("""There are a couple of things you NEED to do before you can use this app:\n\n""")
sys.stderr.write(
"""There are a couple of things you NEED to do before you can use this app:\n\n""")
if need_api_key:
sys.stderr.write(""" * Set FACEBOOK_API_KEY and FACEBOOK_SECRET_KEY to the appropriate values in settings.py\n\n""")
sys.stderr.write(
""" * Set FACEBOOK_API_KEY and FACEBOOK_SECRET_KEY to the appropriate values in settings.py\n\n""")
if need_middleware:
sys.stderr.write(""" * Add 'facebook.djangofb.FacebookMiddleware' to your MIDDLEWARE_CLASSES in settings.py\n\n""")
sys.stderr.write(
""" * Add 'facebook.djangofb.FacebookMiddleware' to your MIDDLEWARE_CLASSES in settings.py\n\n""")
if need_loader:
sys.stderr.write(""" * Add 'django.template.loaders.app_directories.load_template_source' to your TEMPLATE_LOADERS in settings.py\n\n""")
sys.stderr.write(
""" * Add 'django.template.loaders.app_directories.load_template_source'
to your TEMPLATE_LOADERS in settings.py\n\n""")
if need_install_app:
sys.stderr.write(""" * Add '%s.%s' to your INSTALLED_APPS in settings.py\n\n""" % (project_name, app_name))
sys.stderr.write(
""" * Add '%s.%s' to your INSTALLED_APPS in settings.py\n\n""" %
(project_name, app_name))

sys.stderr.write("""The final step is to add (r'^%s/', include('%s.%s.urls')) to your urls.py, and then set your callback page in the application settings on Facebook to 'http://your.domain.com/%s/'.
sys.stderr.write(
"""The final step is to add (r'^%s/', include('%s.%s.urls')) to your urls.py, and then set your callback page in the application settings on Facebook to 'http://your.domain.com/%s/'.
Good luck!
""" % (project_name, project_name, app_name, project_name))
""" %
(project_name, project_name, app_name, project_name))
6 changes: 4 additions & 2 deletions examples/fbsample/fbapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ def _2int(d, k):
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()
Expand All @@ -30,6 +31,7 @@ def get_current(self):
pass
return user


class User(models.Model):
"""A simple User model for Facebook users."""

Expand Down
7 changes: 3 additions & 4 deletions examples/fbsample/fbapp/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.conf.urls.defaults import *

urlpatterns = patterns('fbsample.fbapp.views',
(r'^$', 'canvas'),
# Define other pages you want to create here
)

(r'^$', 'canvas'),
# Define other pages you want to create here
)
19 changes: 13 additions & 6 deletions examples/fbsample/fbapp/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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
# uncomment the following two lines and the one below
# if you don't 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
Expand All @@ -16,7 +16,9 @@
# 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)
# @decorator_from_middleware(FacebookMiddleware)


@facebook.require_login()
def canvas(request):
# Get the User object for the currently logged in user
Expand All @@ -30,7 +32,12 @@ def canvas(request):
# 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})
return direct_to_template(
request,
'canvas.fbml',
extra_context={
'fbuser': user})


@facebook.require_login()
def ajax(request):
Expand Down
7 changes: 5 additions & 2 deletions examples/fbsample/manage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env python
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
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.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__":
Expand Down
20 changes: 12 additions & 8 deletions examples/fbsample/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

MANAGERS = ADMINS

DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'db.sqlite3' # Or path to database file if using sqlite3.
# 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_ENGINE = 'sqlite3'
# Or path to database file if using sqlite3.
DATABASE_NAME = 'db.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.
# Set to empty string for localhost. Not used with sqlite3.
DATABASE_HOST = ''
# Set to empty string for default. Not used with sqlite3.
DATABASE_PORT = ''

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
Expand Down Expand Up @@ -54,14 +58,14 @@
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.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',
)

Expand All @@ -78,11 +82,11 @@
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',

'fbsample.fbapp',
)

# get it from here
# get it from here
# http://www.facebook.com/editapps.php?ref=mb
FACEBOOK_API_KEY = 'x'
FACEBOOK_SECRET_KEY = 'xx'
20 changes: 10 additions & 10 deletions examples/fbsample/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
# admin.autodiscover()

urlpatterns = patterns('',
# Example:
# (r'^fbsample/', include('fbsample.foo.urls')),
# 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 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')),
)
# 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 ec2bbfb

Please sign in to comment.