diff --git a/ordr/resources/__init__.py b/ordr/resources/__init__.py index 8c2780b..b395b9a 100644 --- a/ordr/resources/__init__.py +++ b/ordr/resources/__init__.py @@ -2,6 +2,8 @@ from pyramid.security import Allow, Everyone, DENY_ALL +from .account import RegistrationResource + class RootResource: ''' The root resource for the application @@ -23,6 +25,19 @@ class RootResource: def __acl__(self): ''' access controll list for the resource ''' return [(Allow, Everyone, 'view'), DENY_ALL] + + def __getitem__(self, key): + ''' retruns a child resource + + :param str key: name of the child resource + :returns: child resource + :raises: KeyError if child resource is not found + ''' + map = { + 'registration': RegistrationResource + } + child_class = map[key] + return child_class(request=self.request, name=key, parent=self) def includeme(config): diff --git a/ordr/resources/account.py b/ordr/resources/account.py new file mode 100644 index 0000000..662504c --- /dev/null +++ b/ordr/resources/account.py @@ -0,0 +1,29 @@ +''' Resources (sub) package, used to connect URLs to views ''' + +from pyramid.security import Allow, Everyone, DENY_ALL + + +class RegistrationResource: + ''' The resource for new user registration + + :param pyramid.request.Request request: the current request object + :param str name: the name of the resource + :param parent: the parent resouce + ''' + + nav_active = 'welcome' + + def __init__(self, request, name, parent): + ''' Create registration resource + + :param pyramid.request.Request request: the current request object + :param str name: the name of the resource + :param parent: the parent resouce + ''' + self.request = request + self.__name__ = name + self.__parent__ = parent + + def __acl__(self): + ''' access controll list for the resource ''' + return [(Allow, Everyone, 'view'), DENY_ALL] diff --git a/tests/resources/account.py b/tests/resources/account.py new file mode 100644 index 0000000..03c6cce --- /dev/null +++ b/tests/resources/account.py @@ -0,0 +1,20 @@ +''' Tests for the account resources ''' + + +def test_registration_init(): + from ordr.resources.account import RegistrationResource + resource = RegistrationResource( + request='some request', + name='a name', + parent='the parent' + ) + assert resource.__name__ == 'a name' + assert resource.__parent__ == 'the parent' + assert resource.request == 'some request' + + +def test_registration_acl(): + from pyramid.security import Allow, Everyone, DENY_ALL + from ordr.resources.account import RegistrationResource + resource = RegistrationResource('some request', 'a name', 'the parent') + assert resource.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL] diff --git a/tests/resources/root.py b/tests/resources/root.py new file mode 100644 index 0000000..4926532 --- /dev/null +++ b/tests/resources/root.py @@ -0,0 +1,36 @@ +''' Tests for the root resource ''' + +import pytest + + +def test_root_init(): + from ordr.resources import RootResource + root = RootResource('request') + assert root.__name__ is None + assert root.__parent__ is None + assert root.request == 'request' + + +def test_root_acl(): + from pyramid.security import Allow, Everyone, DENY_ALL + from ordr.resources import RootResource + root = RootResource(None) + assert root.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL] + + +def test_root_getitem(): + from ordr.resources import RootResource + from ordr.resources.account import RegistrationResource + root = RootResource(None) + child = root['registration'] + assert isinstance(child, RegistrationResource) + assert child.__name__ == 'registration' + assert child.__parent__ == root + assert child.request == root.request + + +def test_root_getitem_raises_error(): + from ordr.resources import RootResource + root = RootResource(None) + with pytest.raises(KeyError): + root['unknown child name'] diff --git a/tests/resources/root_resource.py b/tests/resources/root_resource.py deleted file mode 100644 index 7499fd0..0000000 --- a/tests/resources/root_resource.py +++ /dev/null @@ -1,14 +0,0 @@ - -def test_root_init(): - from ordr.resources import RootResource - root = RootResource('request') - assert root.__name__ is None - assert root.__parent__ is None - assert root.request == 'request' - - -def test_root_acl(): - from pyramid.security import Allow, Everyone, DENY_ALL - from ordr.resources import RootResource - root = RootResource(None) - assert root.__acl__() == [(Allow, Everyone, 'view'), DENY_ALL]