Browse Source

added BaseResource and RootResource

master
Holger Frey 7 years ago
parent
commit
83e697bc0b
  1. 5
      ordr2/__init__.py
  2. 62
      ordr2/resources/__init__.py
  3. 2
      ordr2/scripts/initializedb.py
  4. 12
      ordr2/views/__init__.py
  5. 33
      ordr2/views/default.py
  6. 3
      ordr2/views/errors.py
  7. 16
      ordr2/views/public.py

5
ordr2/__init__.py

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
''' Top-level package for Ordr2. ''' ''' Top-level package for Ordr2. '''
__author__ = 'Holger Frey' __author__ = 'Holger Frey'
@ -13,8 +11,11 @@ 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('.resources') config.include('.resources')
config.scan() config.scan()
return config.make_wsgi_app() return config.make_wsgi_app()

62
ordr2/resources/__init__.py

@ -1,10 +1,63 @@
class Root(object): ''' Resources (sub) package, used to connect URLs to views '''
class BaseResource(object):
'''
Base resouce class for location aware resources
establishes a simple dict like interface for returning child resources
'''
# __name__ and __parent__ properties for location awareness
__name__ = None __name__ = None
__parent__ = None __parent__ = None
# dict to match the next url path segment
# for example: to return an AccountResouce when the next path segment is
# 'account' use nodes = {'account': AccountResource}
nodes = {}
def __init__(self, name, parent):
''' Create a base resource
:param str name:
url path segment that identifies this resource in its lineage
:param parent:
parent resource in the lineage
:type parent:
ordr2.resources.BaseResource
'''
self.__name__ = name
self.__parent__ = parent
self.request = parent.request
def __getitem__(self, key):
''' provides a dict like interface to access child resources
:param str key:
path segment for a child resource
:rtype:
ordr2.resources.BaseResource or KeyError
'''
node_class = self.nodes[key]
return node_class(key, self)
class RootResource(BaseResource):
''' The root resource for the application '''
def __init__(self, request):
''' Create the root resource
:param request:
the current request object
:type request:
pyramid.request.Request
'''
self.__name__ = None
self.__parent__ = None
self.request = request
def root_factory(request):
return Root()
def includeme(config): def includeme(config):
@ -14,5 +67,4 @@ def includeme(config):
Activate this setup using ``config.include('ordr2.resources')``. Activate this setup using ``config.include('ordr2.resources')``.
''' '''
config.set_root_factory(root_factory) config.set_root_factory(RootResource)
config.add_static_view('static', 'ordr2:static', cache_max_age=3600)

2
ordr2/scripts/initializedb.py

@ -1,3 +1,5 @@
''' initialize the database '''
import os import os
import sys import sys
import transaction import transaction

12
ordr2/views/__init__.py

@ -1 +1,11 @@
# package ''' views (sub) package '''
def includeme(config):
'''
Initialize the resources for traversal in a Pyramid app.
Activate this setup using ``config.include('ordr2.views')``.
'''
config.add_static_view('static', 'ordr2:static', cache_max_age=3600)

33
ordr2/views/default.py

@ -1,33 +0,0 @@
from pyramid.response import Response
from pyramid.view import view_config
from sqlalchemy.exc import DBAPIError
from ..models import MyModel
@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()
except DBAPIError:
return Response(db_err_msg, content_type='text/plain', status=500)
return {'one': one, 'project': 'Ordr2'}
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
to initialize your database tables. Check your virtual
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.
After you fix the problem, please restart the Pyramid application to
try it again.
'''

3
ordr2/views/notfound.py → ordr2/views/errors.py

@ -1,7 +1,8 @@
from pyramid.view import notfound_view_config from pyramid.view import notfound_view_config
@notfound_view_config(renderer='../templates/404.jinja2') @notfound_view_config(renderer='ordr2:templates/404.jinja2')
def notfound_view(context, request): def notfound_view(context, request):
''' Displays a custom file not found (404) page '''
request.response.status = 404 request.response.status = 404
return {} return {}

16
ordr2/views/public.py

@ -0,0 +1,16 @@
''' pubic accessible views '''
from pyramid.response import Response
from pyramid.view import view_config
from sqlalchemy.exc import DBAPIError
from ..models import MyModel
@view_config(
context='ordr2:resources.RootResource',
renderer='ordr2:templates/mytemplate.jinja2'
)
def index(context, request):
return {}