From b01bf05d821162998072797c0ef7c1b53beba652 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Fri, 29 Sep 2017 01:25:25 +0200 Subject: [PATCH] added admin user resources --- ordr2/resources/__init__.py | 4 +-- ordr2/resources/admin.py | 55 ++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/ordr2/resources/__init__.py b/ordr2/resources/__init__.py index 5f19bdd..00bf576 100644 --- a/ordr2/resources/__init__.py +++ b/ordr2/resources/__init__.py @@ -1,7 +1,7 @@ from pyramid.security import Allow, Everyone from .account import Account -from .admin import Admin +from .admin import Admin, UserList, UserAccount from .base import BaseResource @@ -17,7 +17,7 @@ class Root(BaseResource): } def __init__(self, request): - self._request = request + self.request = request def __acl__(self): return [ (Allow, Everyone, 'view') ] diff --git a/ordr2/resources/admin.py b/ordr2/resources/admin.py index 01c04fb..0599f88 100644 --- a/ordr2/resources/admin.py +++ b/ordr2/resources/admin.py @@ -1,9 +1,62 @@ from pyramid.security import Allow, Authenticated, Deny, Everyone -from .base import BaseResource +from .base import BaseResource, PaginationResourceMixin +from ordr2.models import User + + + + +class UserAccount(BaseResource): + def __acl__(self): + return [ (Allow, 'role:admin', 'view') ] + + +class UserList(BaseResource, PaginationResourceMixin): + sql_model_class = User + child_resource_class = UserAccount + default_sorting = 'user.asc' + default_items_per_page = 12 + + def __acl__(self): + return [ (Allow, 'role:admin', 'view') ] + + + def prepare_filtered_query(self, dbsession, filter_params): + ''' setup the base filtered query ''' + query = dbsession.query(self.sql_model_class) + role = filter_params.get('role', None) + if role is not None: + query = query.filter_by(role=role) + self.filters['role'] = role + return query + + + def prepare_sorted_query(self, query, sorting): + ''' setup the base filtered query ''' + available_fields = { + 'user': 'user_name', + 'first': 'first_name', + 'last': 'last_name', + 'email': 'email', + 'role': 'role' + } + name = available_fields.get(sorting.field, None) + model_field = getattr(self.sql_model_class, name, None) + if model_field: + sort_func = sorting.func(model_field) + query = query.order_by(sort_func) + if sorting.text != self.default_sorting.lower(): + default_sort = self.parse_sort_parameters(self.default_sorting) + query = self.prepare_sorted_query(query, default_sort) + return query + class Admin(BaseResource): + nodes = { + 'users': UserList + } + def __acl__(self): return [ (Allow, 'role:admin', 'view') ]