Browse Source

replaced URL dispatch with traversal routing

master
Holger Frey 7 years ago
parent
commit
f96a30be7e
  1. 3
      .gitignore
  2. 0
      ordr2.sqlite
  3. 7
      ordr2/__init__.py
  4. 8
      ordr2/models/__init__.py
  5. 10
      ordr2/models/meta.py
  6. 18
      ordr2/resources/__init__.py
  7. 3
      ordr2/routes.py
  8. 1
      ordr2/views/__init__.py
  9. 16
      ordr2/views/default.py
  10. 2
      ordr2/views/notfound.py

3
.gitignore vendored

@ -1,3 +1,6 @@
# ignore sqlite database
ordr2.sqlite
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

0
ordr2.sqlite

7
ordr2/__init__.py

@ -2,7 +2,7 @@
''' Top-level package for Ordr2. ''' ''' Top-level package for Ordr2. '''
__author__ = '''Holger Frey''' __author__ = 'Holger Frey'
__email__ = 'frey@imtek.de' __email__ = 'frey@imtek.de'
__version__ = '0.0.1' __version__ = '0.0.1'
@ -11,11 +11,10 @@ from pyramid.config import Configurator
def main(global_config, **settings): def main(global_config, **settings):
""" This function returns a Pyramid WSGI application. ''' This function returns a Pyramid WSGI application. '''
"""
config = Configurator(settings=settings) config = Configurator(settings=settings)
config.include('pyramid_jinja2') config.include('pyramid_jinja2')
config.include('.models') config.include('.models')
config.include('.routes') config.include('.resources')
config.scan() config.scan()
return config.make_wsgi_app() return config.make_wsgi_app()

8
ordr2/models/__init__.py

@ -23,7 +23,7 @@ def get_session_factory(engine):
def get_tm_session(session_factory, transaction_manager): def get_tm_session(session_factory, transaction_manager):
""" '''
Get a ``sqlalchemy.orm.Session`` instance backed by a transaction. Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.
This function will hook the session to the transaction manager which 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: with transaction.manager:
dbsession = get_tm_session(session_factory, transaction.manager) dbsession = get_tm_session(session_factory, transaction.manager)
""" '''
dbsession = session_factory() dbsession = session_factory()
zope.sqlalchemy.register( zope.sqlalchemy.register(
dbsession, transaction_manager=transaction_manager) dbsession, transaction_manager=transaction_manager)
@ -50,12 +50,12 @@ def get_tm_session(session_factory, transaction_manager):
def includeme(config): def includeme(config):
""" '''
Initialize the model for a Pyramid app. Initialize the model for a Pyramid app.
Activate this setup using ``config.include('ordr2.models')``. Activate this setup using ``config.include('ordr2.models')``.
""" '''
settings = config.get_settings() settings = config.get_settings()
# use pyramid_tm to hook the transaction lifecycle to the request # use pyramid_tm to hook the transaction lifecycle to the request

10
ordr2/models/meta.py

@ -5,11 +5,11 @@ from sqlalchemy.schema import MetaData
# providers will autogenerate vastly different names making migrations more # providers will autogenerate vastly different names making migrations more
# difficult. See: http://alembic.zzzcomputing.com/en/latest/naming.html # difficult. See: http://alembic.zzzcomputing.com/en/latest/naming.html
NAMING_CONVENTION = { NAMING_CONVENTION = {
"ix": 'ix_%(column_0_label)s', 'ix': 'ix_%(column_0_label)s',
"uq": "uq_%(table_name)s_%(column_0_name)s", 'uq': 'uq_%(table_name)s_%(column_0_name)s',
"ck": "ck_%(table_name)s_%(constraint_name)s", 'ck': 'ck_%(table_name)s_%(constraint_name)s',
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", 'fk': 'fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s',
"pk": "pk_%(table_name)s" 'pk': 'pk_%(table_name)s'
} }
metadata = MetaData(naming_convention=NAMING_CONVENTION) metadata = MetaData(naming_convention=NAMING_CONVENTION)

18
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)

3
ordr2/routes.py

@ -1,3 +0,0 @@
def includeme(config):
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')

1
ordr2/views/__init__.py

@ -0,0 +1 @@
# package

16
ordr2/views/default.py

@ -6,8 +6,8 @@ from sqlalchemy.exc import DBAPIError
from ..models import MyModel from ..models import MyModel
@view_config(route_name='home', renderer='../templates/mytemplate.jinja2') @view_config(context='ordr2.resources.Root', renderer='../templates/mytemplate.jinja2')
def my_view(request): def my_view(context, request):
try: try:
query = request.dbsession.query(MyModel) query = request.dbsession.query(MyModel)
one = query.filter(MyModel.name == 'one').first() one = query.filter(MyModel.name == 'one').first()
@ -16,18 +16,18 @@ def my_view(request):
return {'one': one, 'project': 'Ordr2'} return {'one': one, 'project': 'Ordr2'}
db_err_msg = """\ db_err_msg = '''\
Pyramid is having a problem using your SQL database. The problem Pyramid is having a problem using your SQL database. The problem
might be caused by one of the following things: 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 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 2. Your database server may not be running. Check that the
database server referred to by the "sqlalchemy.url" setting in database server referred to by the 'sqlalchemy.url' setting in
your "development.ini" file is running. your 'development.ini' file is running.
After you fix the problem, please restart the Pyramid application to After you fix the problem, please restart the Pyramid application to
try it again. try it again.
""" '''

2
ordr2/views/notfound.py

@ -2,6 +2,6 @@ from pyramid.view import notfound_view_config
@notfound_view_config(renderer='../templates/404.jinja2') @notfound_view_config(renderer='../templates/404.jinja2')
def notfound_view(request): def notfound_view(context, request):
request.response.status = 404 request.response.status = 404
return {} return {}