From f96a30be7ef6b790e67c9a9207b2c7cafe423b04 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Tue, 26 Sep 2017 16:03:52 +0200 Subject: [PATCH] replaced URL dispatch with traversal routing --- .gitignore | 3 +++ ordr2.sqlite | 0 ordr2/__init__.py | 7 +++---- ordr2/models/__init__.py | 10 +++++----- ordr2/models/meta.py | 10 +++++----- ordr2/models/mymodel.py | 2 +- ordr2/resources/__init__.py | 18 ++++++++++++++++++ ordr2/routes.py | 3 --- ordr2/views/__init__.py | 1 + ordr2/views/default.py | 16 ++++++++-------- ordr2/views/notfound.py | 2 +- 11 files changed, 45 insertions(+), 27 deletions(-) delete mode 100644 ordr2.sqlite create mode 100644 ordr2/resources/__init__.py delete mode 100644 ordr2/routes.py diff --git a/.gitignore b/.gitignore index a3ff839..7292bf9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# ignore sqlite database +ordr2.sqlite + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/ordr2.sqlite b/ordr2.sqlite deleted file mode 100644 index e69de29..0000000 diff --git a/ordr2/__init__.py b/ordr2/__init__.py index 10559b3..ede0451 100644 --- a/ordr2/__init__.py +++ b/ordr2/__init__.py @@ -2,7 +2,7 @@ ''' Top-level package for Ordr2. ''' -__author__ = '''Holger Frey''' +__author__ = 'Holger Frey' __email__ = 'frey@imtek.de' __version__ = '0.0.1' @@ -11,11 +11,10 @@ from pyramid.config import Configurator def main(global_config, **settings): - """ This function returns a Pyramid WSGI application. - """ + ''' This function returns a Pyramid WSGI application. ''' config = Configurator(settings=settings) config.include('pyramid_jinja2') config.include('.models') - config.include('.routes') + config.include('.resources') config.scan() return config.make_wsgi_app() diff --git a/ordr2/models/__init__.py b/ordr2/models/__init__.py index cf5c3c3..3d04458 100644 --- a/ordr2/models/__init__.py +++ b/ordr2/models/__init__.py @@ -23,7 +23,7 @@ def get_session_factory(engine): def get_tm_session(session_factory, transaction_manager): - """ + ''' Get a ``sqlalchemy.orm.Session`` instance backed by a transaction. This function will hook the session to the transaction manager which @@ -42,7 +42,7 @@ def get_tm_session(session_factory, transaction_manager): with transaction.manager: dbsession = get_tm_session(session_factory, transaction.manager) - """ + ''' dbsession = session_factory() zope.sqlalchemy.register( dbsession, transaction_manager=transaction_manager) @@ -50,12 +50,12 @@ def get_tm_session(session_factory, transaction_manager): def includeme(config): - """ + ''' Initialize the model for a Pyramid app. Activate this setup using ``config.include('ordr2.models')``. - """ + ''' settings = config.get_settings() # use pyramid_tm to hook the transaction lifecycle to the request @@ -70,4 +70,4 @@ def includeme(config): lambda r: get_tm_session(session_factory, r.tm), 'dbsession', reify=True - ) + ) diff --git a/ordr2/models/meta.py b/ordr2/models/meta.py index 0682247..327b219 100644 --- a/ordr2/models/meta.py +++ b/ordr2/models/meta.py @@ -5,11 +5,11 @@ from sqlalchemy.schema import MetaData # providers will autogenerate vastly different names making migrations more # difficult. See: http://alembic.zzzcomputing.com/en/latest/naming.html NAMING_CONVENTION = { - "ix": 'ix_%(column_0_label)s', - "uq": "uq_%(table_name)s_%(column_0_name)s", - "ck": "ck_%(table_name)s_%(constraint_name)s", - "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", - "pk": "pk_%(table_name)s" + 'ix': 'ix_%(column_0_label)s', + 'uq': 'uq_%(table_name)s_%(column_0_name)s', + 'ck': 'ck_%(table_name)s_%(constraint_name)s', + 'fk': 'fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s', + 'pk': 'pk_%(table_name)s' } metadata = MetaData(naming_convention=NAMING_CONVENTION) diff --git a/ordr2/models/mymodel.py b/ordr2/models/mymodel.py index d65a01a..8c9d701 100644 --- a/ordr2/models/mymodel.py +++ b/ordr2/models/mymodel.py @@ -3,7 +3,7 @@ from sqlalchemy import ( Index, Integer, Text, -) + ) from .meta import Base diff --git a/ordr2/resources/__init__.py b/ordr2/resources/__init__.py new file mode 100644 index 0000000..938d86a --- /dev/null +++ b/ordr2/resources/__init__.py @@ -0,0 +1,18 @@ +class Root(object): + __name__ = None + __parent__ = None + + +def root_factory(request): + return Root() + + +def includeme(config): + ''' + Initialize the resources for traversal in a Pyramid app. + + Activate this setup using ``config.include('ordr2.resources')``. + + ''' + config.set_root_factory(root_factory) + config.add_static_view('static', 'ordr2:static', cache_max_age=3600) diff --git a/ordr2/routes.py b/ordr2/routes.py deleted file mode 100644 index 25504ad..0000000 --- a/ordr2/routes.py +++ /dev/null @@ -1,3 +0,0 @@ -def includeme(config): - config.add_static_view('static', 'static', cache_max_age=3600) - config.add_route('home', '/') diff --git a/ordr2/views/__init__.py b/ordr2/views/__init__.py index e69de29..5bb534f 100644 --- a/ordr2/views/__init__.py +++ b/ordr2/views/__init__.py @@ -0,0 +1 @@ +# package diff --git a/ordr2/views/default.py b/ordr2/views/default.py index 7cd5b3e..a1b6cf5 100644 --- a/ordr2/views/default.py +++ b/ordr2/views/default.py @@ -6,8 +6,8 @@ from sqlalchemy.exc import DBAPIError from ..models import MyModel -@view_config(route_name='home', renderer='../templates/mytemplate.jinja2') -def my_view(request): +@view_config(context='ordr2.resources.Root', renderer='../templates/mytemplate.jinja2') +def my_view(context, request): try: query = request.dbsession.query(MyModel) one = query.filter(MyModel.name == 'one').first() @@ -16,18 +16,18 @@ def my_view(request): return {'one': one, 'project': 'Ordr2'} -db_err_msg = """\ +db_err_msg = '''\ Pyramid is having a problem using your SQL database. The problem might be caused by one of the following things: -1. You may need to run the "initialize_ordr2_db" script +1. You may need to run the 'initialize_ordr2_db' script to initialize your database tables. Check your virtual - environment's "bin" directory for this script and try to run it. + environment's 'bin' directory for this script and try to run it. 2. Your database server may not be running. Check that the - database server referred to by the "sqlalchemy.url" setting in - your "development.ini" file is running. + database server referred to by the 'sqlalchemy.url' setting in + your 'development.ini' file is running. After you fix the problem, please restart the Pyramid application to try it again. -""" +''' diff --git a/ordr2/views/notfound.py b/ordr2/views/notfound.py index 69d6e28..9798e0c 100644 --- a/ordr2/views/notfound.py +++ b/ordr2/views/notfound.py @@ -2,6 +2,6 @@ from pyramid.view import notfound_view_config @notfound_view_config(renderer='../templates/404.jinja2') -def notfound_view(request): +def notfound_view(context, request): request.response.status = 404 return {}