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. 2
      ordr2/models/mymodel.py
  7. 18
      ordr2/resources/__init__.py
  8. 3
      ordr2/routes.py
  9. 1
      ordr2/views/__init__.py
  10. 16
      ordr2/views/default.py
  11. 2
      ordr2/views/notfound.py

3
.gitignore vendored

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

0
ordr2.sqlite

7
ordr2/__init__.py

@ -2,7 +2,7 @@ @@ -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 @@ -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()

8
ordr2/models/__init__.py

@ -23,7 +23,7 @@ def get_session_factory(engine): @@ -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): @@ -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): @@ -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

10
ordr2/models/meta.py

@ -5,11 +5,11 @@ from sqlalchemy.schema import MetaData @@ -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)

2
ordr2/models/mymodel.py

@ -3,7 +3,7 @@ from sqlalchemy import ( @@ -3,7 +3,7 @@ from sqlalchemy import (
Index,
Integer,
Text,
)
)
from .meta import Base

18
ordr2/resources/__init__.py

@ -0,0 +1,18 @@ @@ -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 @@ @@ -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 @@ @@ -0,0 +1 @@
# package

16
ordr2/views/default.py

@ -6,8 +6,8 @@ from sqlalchemy.exc import DBAPIError @@ -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): @@ -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.
"""
'''

2
ordr2/views/notfound.py

@ -2,6 +2,6 @@ from pyramid.view import notfound_view_config @@ -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 {}