Browse Source

added admin user resources

php2python
Holger Frey 7 years ago
parent
commit
b01bf05d82
  1. 4
      ordr2/resources/__init__.py
  2. 55
      ordr2/resources/admin.py

4
ordr2/resources/__init__.py

@ -1,7 +1,7 @@
from pyramid.security import Allow, Everyone from pyramid.security import Allow, Everyone
from .account import Account from .account import Account
from .admin import Admin from .admin import Admin, UserList, UserAccount
from .base import BaseResource from .base import BaseResource
@ -17,7 +17,7 @@ class Root(BaseResource):
} }
def __init__(self, request): def __init__(self, request):
self._request = request self.request = request
def __acl__(self): def __acl__(self):
return [ (Allow, Everyone, 'view') ] return [ (Allow, Everyone, 'view') ]

55
ordr2/resources/admin.py

@ -1,9 +1,62 @@
from pyramid.security import Allow, Authenticated, Deny, Everyone 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): class Admin(BaseResource):
nodes = {
'users': UserList
}
def __acl__(self): def __acl__(self):
return [ (Allow, 'role:admin', 'view') ] return [ (Allow, 'role:admin', 'view') ]