From 52a594b3be8120622adc0f3233470e86d07921d1 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Mon, 16 Oct 2017 17:34:31 +0200 Subject: [PATCH] added account resource moved BaseResource to separate module to prevent circular imports --- ordr2/resources/__init__.py | 64 +++++-------------------------------- ordr2/resources/account.py | 35 ++++++++++++++++++++ ordr2/resources/base.py | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 56 deletions(-) create mode 100644 ordr2/resources/account.py create mode 100644 ordr2/resources/base.py diff --git a/ordr2/resources/__init__.py b/ordr2/resources/__init__.py index 784869c..a56c87f 100644 --- a/ordr2/resources/__init__.py +++ b/ordr2/resources/__init__.py @@ -2,62 +2,8 @@ from pyramid.security import Allow, Everyone, DENY_ALL - -class BaseResource(object): - ''' - Base resouce class for location aware resources - - :param str name: - url path segment that identifies this resource in its lineage - :param parent: - parent resource - :type parent: - ordr2.resources.BaseResource - :param model: - (optional) a model instance represented by this resource, - e.g. a database entry - - Provides a dict like interface for retrieving child resources used by - traversal style routing in the Pyramid web framework. - - The ``nodes`` property is a dictionary to to match the next url path - segment in traversal to a child class. - For example: to return an AccountResouce when the next path segment is - 'account' use ``nodes = {'account': AccountResource}``. - - ''' - - # __name__ and __parent__ properties for location aware resources - __name__ = None - __parent__ = None - - #: name of the main nav_section for template highlighting - nav_section = None - - #: dict to match the next url path segment - nodes = {} - - def __init__(self, name, parent, model=None): - ''' Create a base resource ''' - self.__name__ = name - self.__parent__ = parent - self.request = parent.request #: the current request - self.model = model #: a related model instance - - def __acl__(self): - ''' access controll list for the resource ''' - return [DENY_ALL] - - def __getitem__(self, key): - ''' provides the 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) +from .base import BaseResource +from .account import AccountResource class RootResource(BaseResource): @@ -69,8 +15,14 @@ class RootResource(BaseResource): pyramid.request.Request ''' + #: name of the main navigation section for template highlighting nav_section = 'root' + #: dict to match the next url path segment + nodes = { + 'account': AccountResource + } + def __init__(self, request): ''' Create the root resource ''' self.__name__ = None diff --git a/ordr2/resources/account.py b/ordr2/resources/account.py new file mode 100644 index 0000000..24a65ff --- /dev/null +++ b/ordr2/resources/account.py @@ -0,0 +1,35 @@ +''' Resources for account registraion and settings ''' + +from pyramid.security import Allow, Authenticated, Everyone, Deny, DENY_ALL + +from ordr2.resources.base import BaseResource + + +class AccountResource(BaseResource): + ''' Resouce class for account registration and settings ''' + + #: name of the main navigation section for template highlighting + nav_section = 'account' + + def __init__(self, name, parent, model=None): + ''' Create a base resource ''' + super().__init__(name, parent) + # the current model depends is the current logged in user or None + self.model = self.request.user + + def __acl__(self): + ''' access controll list for the resource + + - everyone can log in our log out + - authenticated users can change their settings + - unauthenticated users can register + + ''' + return [ + (Allow, Everyone, 'login'), + (Allow, Everyone, 'logout'), + (Deny, Authenticated, 'register'), + (Allow, Everyone, 'register'), + (Allow, Authenticated, 'settings'), + ] + diff --git a/ordr2/resources/base.py b/ordr2/resources/base.py new file mode 100644 index 0000000..6376055 --- /dev/null +++ b/ordr2/resources/base.py @@ -0,0 +1,58 @@ +''' Base resource classes ''' + + +class BaseResource(object): + ''' + Base resouce class for location aware resources + + :param str name: + url path segment that identifies this resource in its lineage + :param parent: + parent resource + :type parent: + ordr2.resources.BaseResource + :param model: + (optional) a model instance represented by this resource, + e.g. a database entry + + Provides a dict like interface for retrieving child resources used by + traversal style routing in the Pyramid web framework. + + The ``nodes`` property is a dictionary to to match the next url path + segment in traversal to a child class. + For example: to return an AccountResouce when the next path segment is + 'account' use ``nodes = {'account': AccountResource}``. + + ''' + + # __name__ and __parent__ properties for location aware resources + __name__ = None + __parent__ = None + + #: name of the main navigation section for template highlighting + nav_section = None + + #: dict to match the next url path segment + nodes = {} + + def __init__(self, name, parent, model=None): + ''' Create a base resource ''' + self.__name__ = name + self.__parent__ = parent + self.request = parent.request #: the current request + self.model = model #: a related model instance + + def __acl__(self): + ''' access controll list for the resource ''' + return [DENY_ALL] + + def __getitem__(self, key): + ''' provides the 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)