Holger Frey
7 years ago
2 changed files with 56 additions and 3 deletions
@ -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') ] |
||||||
|
Reference in new issue