Commits

Elliot Peele committed 20928fa Draft

increase comlexity of views

Comments (0)

Files changed (9)

authztest/__init__.py

     config = Configurator(settings=settings)
     config.add_route('base', '/base')
     config.add_route('base_api_auth', '/base/apiauth')
-    config.add_route('test', '/')
 
     config.add_route('login', '/login')
     config.add_route('logout', '/logout')
 
+    config.add_route('api', '/api')
+    config.add_route('api_v1', '/api/v1')
+    config.add_route('api_v1_data', '/api/v1/data')
+
     secret = 'asdfasdf'
     config.set_authorization_policy(AuthorizationPolicy())
     config.set_authentication_policy(AuthTktAuthenticationPolicy(secret))

authztest/core/__init__.py

Empty file added.

authztest/core/views.py

+import logging
+
+log = logging.getLogger('authztest.views')
+
+from pyramid.view import view_config
+from pyramid.view import view_defaults as pyramid_view_defaults
+from pyramid.httpexceptions import HTTPNotImplemented
+
+
+class view_defaults(pyramid_view_defaults):
+    def __call__(self, wrapped):
+        defaults = {}
+        for cls in reversed(wrapped.mro()):
+            if hasattr(cls, '__view_defaults__'):
+                defaults.update(cls.__view_defaults__)
+        defaults.update(self.__dict__)
+        wrapped.__view_defaults__ = defaults
+        return wrapped
+
+
+@view_defaults(route_name='base', permission='view')
+class BaseView(object):
+    def __init__(self, request):
+        self.request = request
+
+    def _call_method(self, method):
+        if hasattr(self, method):
+            func = getattr(self, method)
+            return func()
+        else:
+            raise HTTPNotImplemented
+
+    @view_config(request_method='POST')
+    def post(self):
+        return self._call_method('_post')
+
+    @view_config(request_method='PUT')
+    def put(self):
+        return self._call_method('_put')
+
+    @view_config(request_method='GET')
+    def get(self):
+        return self._call_method('_get')
+
+    @view_config(request_method='DELETE')
+    def delete(self):
+        return self._call_method('_delete')
+
+
+@view_defaults(renderer='json', route_name='base_api')
+class APIView(BaseView):
+    pass
+
+
+@view_defaults(renderer='json', route_name='base_api_auth', permission='authenticated')
+class APIAuthView(BaseView):
+    pass

authztest/views.py

-import logging
-
-log = logging.getLogger('authztest.views')
-
-from pyramid.view import view_config as pyramid_view_config
-from pyramid.view import view_defaults as pyramid_view_defaults
-from pyramid.security import forget
-from pyramid.security import remember
-from pyramid.httpexceptions import HTTPNotImplemented
-
-class view_config(pyramid_view_config):
-    def __call__(self, wrapped):
-        wrapped = pyramid_view_config.__call__(self, wrapped)
-        log.debug('applying view config to %s %s %s'
-            % (wrapped, getattr(self, 'route_name', None),
-               getattr(self, '', None)))
-
-
-class view_defaults(pyramid_view_defaults):
-    def __call__(self, wrapped):
-        defaults = {}
-        for cls in reversed(wrapped.mro()):
-            if hasattr(cls, '__view_defaults__'):
-                defaults.update(cls.__view_defaults__)
-        defaults.update(self.__dict__)
-        wrapped.__view_defaults__ = defaults
-        return wrapped
-
-
-@view_defaults(route_name='base', permission='view')
-class BaseView(object):
-    def __init__(self, request):
-        self.request = request
-
-    def _call_method(self, method):
-        if hasattr(self, method):
-            func = getattr(self, method)
-            return func()
-        else:
-            raise HTTPNotImplemented
-
-    @view_config(request_method='POST')
-    def post(self):
-        return self._call_method('_post')
-
-    @view_config(request_method='PUT')
-    def put(self):
-        return self._call_method('_put')
-
-    @view_config(request_method='GET')
-    def get(self):
-        return self._call_method('_get')
-
-    @view_config(request_method='DELETE')
-    def delete(self):
-        return self._call_method('_delete')
-
-
-@view_defaults(renderer='json', route_name='base_api_auth', permission='authenticated')
-class APIAuthView(BaseView):
-    pass
-
-@view_defaults(route_name='test')
-class Test(APIAuthView):
-    def _get(self):
-        return {'data': 'get'}
-
-    def _post(self):
-        return {'data': 'post'}
-
-
-@view_config(route_name='login')
-def login(request):
-    headers = remember(request, 'testuser')
-    request.response.headerlist.extend(headers)
-    return request.response
-
-@view_config(route_name='logout')
-def logout(request):
-    headers = forget(request)
-    request.response.headerlist.extend(headers)
-    return request.response

authztest/views/__init__.py

Empty file added.

authztest/views/api/__init__.py

+from authztest.core.views import APIView
+from authztest.core.views import view_defaults
+
+@view_defaults(route_name='api')
+class API(APIView):
+    def _get(self):
+        return {
+            'v1': self.request.route_url('api_v1'),
+        }

authztest/views/api/v1/__init__.py

+from authztest.core.views import APIView
+from authztest.core.views import view_defaults
+
+@view_defaults(route_name='api_v1')
+class APIV1(APIView):
+    def _get(self):
+        return {
+            'data': self.request.route_url('data'),
+        }

authztest/views/api/v1/views.py

+from authztest.core.views import APIAuthView
+from authztest.core.views import view_defaults
+
+@view_defaults(route_name='api_v1_data')
+class DataView(APIAuthView):
+    def _get(self):
+        return {'data': 'get'}
+
+    def _put(self):
+        return {'data': 'put'}
+
+    def _post(self):
+        return {'data': 'post'}
+
+    def _delete(self):
+        return {'data': 'delete'}

authztest/views/auth.py

+import logging
+
+log = logging.getLogger('authztest.views')
+
+from authztest.core.views import view_config
+from pyramid.security import forget
+from pyramid.security import remember
+
+@view_config(route_name='login')
+def login(request):
+    headers = remember(request, 'testuser')
+    request.response.headerlist.extend(headers)
+    return request.response
+
+@view_config(route_name='logout')
+def logout(request):
+    headers = forget(request)
+    request.response.headerlist.extend(headers)
+    return request.response