Skip to content
Browse files

modernize imports

  • Loading branch information...
1 parent a014886 commit 5cc54b52af8bad2353f9f36485e162ba91d3bfd4 @mcdonc mcdonc committed Feb 9, 2013
Showing with 68 additions and 46 deletions.
  1. +1 −8 shootout/__init__.py
  2. +27 −18 shootout/models.py
  3. +8 −3 shootout/scripts/initializedb.py
  4. +7 −2 shootout/subscribers.py
  5. +4 −2 shootout/tests/test_models.py
  6. +21 −13 shootout/views.py
View
9 shootout/__init__.py
@@ -6,7 +6,7 @@
from sqlalchemy import engine_from_config
-from shootout.models import DBSession
+from .models import DBSession
def main(global_config, **settings): # pragma: no cover
@@ -28,13 +28,7 @@ def main(global_config, **settings): # pragma: no cover
session_factory=session_factory
)
- config.add_subscriber('shootout.subscribers.add_base_template',
- 'pyramid.events.BeforeRender')
- config.add_subscriber('shootout.subscribers.csrf_validation',
- 'pyramid.events.NewRequest')
-
config.add_static_view('static', 'shootout:static')
-
config.add_route('idea', '/ideas/{idea_id}')
config.add_route('user', '/users/{username}')
config.add_route('tag', '/tags/{tag_name}')
@@ -45,7 +39,6 @@ def main(global_config, **settings): # pragma: no cover
config.add_route('logout', '/logout')
config.add_route('about', '/about')
config.add_route('main', '/')
-
config.scan()
return config.make_wsgi_app()
View
45 shootout/models.py
@@ -1,34 +1,43 @@
import cryptacular.bcrypt
-from sqlalchemy import Table
-from sqlalchemy import Column
-from sqlalchemy import ForeignKey
-from sqlalchemy.orm import scoped_session
-from sqlalchemy.orm import sessionmaker
-from sqlalchemy.orm import relation
-from sqlalchemy.orm import backref
-from sqlalchemy.orm import column_property
-from sqlalchemy.orm import synonym
-from sqlalchemy.orm import joinedload
-from sqlalchemy.types import Integer
-from sqlalchemy.types import Unicode
-from sqlalchemy.types import UnicodeText
+from sqlalchemy import (
+ Table,
+ Column,
+ ForeignKey,
+ )
+
+from sqlalchemy.orm import (
+ scoped_session,
+ sessionmaker,
+ relation,
+ backref,
+ column_property,
+ synonym,
+ joinedload,
+ )
+
+from sqlalchemy.types import (
+ Integer,
+ Unicode,
+ UnicodeText,
+ )
+
from sqlalchemy.sql import func
from sqlalchemy.ext.declarative import declarative_base
from zope.sqlalchemy import ZopeTransactionExtension
-from pyramid.security import Everyone
-from pyramid.security import Authenticated
-from pyramid.security import Allow
-
+from pyramid.security import (
+ Everyone,
+ Authenticated,
+ Allow,
+ )
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
crypt = cryptacular.bcrypt.BCRYPTPasswordManager()
-
def hash_password(password):
return unicode(crypt.encode(password))
View
11 shootout/scripts/initializedb.py
@@ -1,12 +1,17 @@
import os
import sys
-import transaction
from sqlalchemy import engine_from_config
-from pyramid.paster import get_appsettings, setup_logging
+from pyramid.paster import (
+ get_appsettings,
+ setup_logging,
+ )
-from shootout.models import DBSession, Base
+from shootout.models import (
+ DBSession,
+ Base,
+ )
def usage(argv):
cmd = os.path.basename(argv[0])
View
9 shootout/subscribers.py
@@ -1,12 +1,17 @@
from pyramid.httpexceptions import HTTPForbidden
from pyramid.renderers import get_renderer
+from pyramid.events import (
+ subscriber,
+ BeforeRender,
+ NewRequest,
+ )
-
+@subscriber(BeforeRender)
def add_base_template(event):
base = get_renderer('templates/base.pt').implementation()
event.update({'base': base})
-
+@subscriber(NewRequest)
def csrf_validation(event):
if event.request.method == "POST":
token = event.request.POST.get("_csrf")
View
6 shootout/tests/test_models.py
@@ -5,8 +5,10 @@
def _initTestingDB():
- from shootout.models import DBSession
- from shootout.models import Base
+ from shootout.models import (
+ DBSession,
+ Base,
+ )
from sqlalchemy import create_engine
engine = create_engine('sqlite://')
DBSession.configure(bind=engine)
View
34 shootout/views.py
@@ -7,17 +7,26 @@
from pyramid_simpleform.renderers import FormRenderer
from pyramid.view import view_config
-from pyramid.url import route_url
from pyramid.renderers import render
-from pyramid.httpexceptions import HTTPMovedPermanently
-from pyramid.httpexceptions import HTTPFound
-from pyramid.httpexceptions import HTTPNotFound
-from pyramid.security import authenticated_userid, remember, forget
+from pyramid.httpexceptions import (
+ HTTPMovedPermanently,
+ HTTPFound,
+ HTTPNotFound,
+ )
-from shootout.models import DBSession
-from shootout.models import User, Idea, Tag
+from pyramid.security import (
+ authenticated_userid,
+ remember,
+ forget,
+ )
+from .models import (
+ DBSession,
+ User,
+ Idea,
+ Tag,
+ )
@view_config(permission='view', route_name='main',
renderer='templates/main.pt')
@@ -56,7 +65,7 @@ def idea_vote(request):
voter_username = authenticated_userid(request)
voter = User.get_by_username(voter_username)
- redirect_url = route_url('idea', request, idea_id=idea.idea_id)
+ redirect_url = request.route_url('idea', idea_id=idea.idea_id)
response = HTTPMovedPermanently(location=redirect_url)
if voter.user_id == idea.author_id:
@@ -105,7 +114,7 @@ def user_add(request):
headers = remember(request, username)
- redirect_url = route_url('main', request)
+ redirect_url = request.route_url('main')
return HTTPFound(location=redirect_url, headers=headers)
@@ -158,7 +167,7 @@ def idea_add(request):
idea.tags = tags
session.add(idea)
- redirect_url = route_url('idea', request, idea_id=idea.idea_id)
+ redirect_url = request.route_url('idea', idea_id=idea.idea_id)
return HTTPFound(location=redirect_url)
@@ -240,7 +249,7 @@ def about_view(request):
@view_config(permission='view', route_name='login')
def login_view(request):
- main_view = route_url('main', request)
+ main_view = request.route_url('main')
came_from = request.params.get('came_from', main_view)
post_data = request.POST
@@ -262,8 +271,7 @@ def logout_view(request):
request.session.invalidate()
request.session.flash(u'Logged out successfully.')
headers = forget(request)
- return HTTPFound(location=route_url('main', request),
- headers=headers)
+ return HTTPFound(location=request.route_url('main'), headers=headers)
def toolbar_view(request):

0 comments on commit 5cc54b5

Please sign in to comment.
Something went wrong with that request. Please try again.