From 83e697bc0bceeefd3b0f3c8e792100600b325401 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Thu, 12 Oct 2017 23:51:35 +0200 Subject: [PATCH] added BaseResource and RootResource --- ordr2/__init__.py | 5 ++- ordr2/resources/__init__.py | 62 +++++++++++++++++++++++--- ordr2/scripts/initializedb.py | 2 + ordr2/views/__init__.py | 12 ++++- ordr2/views/default.py | 33 -------------- ordr2/views/{notfound.py => errors.py} | 3 +- ordr2/views/public.py | 16 +++++++ 7 files changed, 91 insertions(+), 42 deletions(-) delete mode 100644 ordr2/views/default.py rename ordr2/views/{notfound.py => errors.py} (53%) create mode 100644 ordr2/views/public.py diff --git a/ordr2/__init__.py b/ordr2/__init__.py index ede0451..a9c7ca4 100644 --- a/ordr2/__init__.py +++ b/ordr2/__init__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - ''' Top-level package for Ordr2. ''' __author__ = 'Holger Frey' @@ -13,8 +11,11 @@ from pyramid.config import Configurator def main(global_config, **settings): ''' This function returns a Pyramid WSGI application. ''' config = Configurator(settings=settings) + config.include('pyramid_jinja2') config.include('.models') config.include('.resources') + config.scan() + return config.make_wsgi_app() diff --git a/ordr2/resources/__init__.py b/ordr2/resources/__init__.py index 938d86a..509b94c 100644 --- a/ordr2/resources/__init__.py +++ b/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 __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): @@ -14,5 +67,4 @@ def includeme(config): 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) + config.set_root_factory(RootResource) diff --git a/ordr2/scripts/initializedb.py b/ordr2/scripts/initializedb.py index 7307ecc..9beefe1 100644 --- a/ordr2/scripts/initializedb.py +++ b/ordr2/scripts/initializedb.py @@ -1,3 +1,5 @@ +''' initialize the database ''' + import os import sys import transaction diff --git a/ordr2/views/__init__.py b/ordr2/views/__init__.py index 5bb534f..fe727e6 100644 --- a/ordr2/views/__init__.py +++ b/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) diff --git a/ordr2/views/default.py b/ordr2/views/default.py deleted file mode 100644 index a1b6cf5..0000000 --- a/ordr2/views/default.py +++ /dev/null @@ -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. -''' diff --git a/ordr2/views/notfound.py b/ordr2/views/errors.py similarity index 53% rename from ordr2/views/notfound.py rename to ordr2/views/errors.py index 9798e0c..967ce90 100644 --- a/ordr2/views/notfound.py +++ b/ordr2/views/errors.py @@ -1,7 +1,8 @@ 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): + ''' Displays a custom file not found (404) page ''' request.response.status = 404 return {} diff --git a/ordr2/views/public.py b/ordr2/views/public.py new file mode 100644 index 0000000..686860e --- /dev/null +++ b/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 {}