Holger Frey
7 years ago
3 changed files with 86 additions and 0 deletions
@ -0,0 +1,83 @@
@@ -0,0 +1,83 @@
|
||||
from pyramid.security import Allow, Authenticated, Deny, DENY_ALL, Everyone |
||||
|
||||
from .base import BaseResource, PaginationResourceMixin |
||||
from ordr2.models import Category, Order, OrderStatus |
||||
|
||||
|
||||
|
||||
class OrderResource(BaseResource): |
||||
def __acl__(self): |
||||
acl = [ |
||||
(Allow, 'role:user', 'view'), |
||||
(Allow, 'role:user', 'create'), |
||||
(Allow, 'role:purchaser', 'edit'), |
||||
(Allow, 'role:purchaser', 'delete'), |
||||
] |
||||
if self.model.status == OrderStatus.OPEN: |
||||
acl.append( (Allow, 'user:' + str(self.model.username), 'edit') ) |
||||
acl.append( (Allow, 'user:' + str(self.model.username), 'delete') ) |
||||
acl.append(DENY_ALL) |
||||
return acl |
||||
|
||||
|
||||
class OrderList(BaseResource, PaginationResourceMixin): |
||||
sql_model_class = Order |
||||
child_resource_class = OrderResource |
||||
default_sorting = 'created.asc' |
||||
default_items_per_page = 12 |
||||
|
||||
def __acl__(self): |
||||
return [ |
||||
(Allow, 'role:user', 'view'), |
||||
(Allow, 'role:user', 'create'), |
||||
(Allow, 'role:purchaser', 'edit'), |
||||
(Allow, 'role:purchaser', 'delete'), |
||||
DENY_ALL |
||||
] |
||||
|
||||
|
||||
def prepare_filtered_query(self, dbsession, filter_params): |
||||
''' setup the base filtered query ''' |
||||
query = dbsession.query(self.sql_model_class) |
||||
|
||||
try: |
||||
status_name = filter_params.get('status', None) |
||||
status_name = status_name.lower() |
||||
status = OrderStatus(status_name) |
||||
query = query.filter_by(status=status) |
||||
except (AttributeError, ValueError): |
||||
status_name = None |
||||
self.filters['category'] = status_name |
||||
|
||||
user_name = filter_params.get('user', None) |
||||
if user_name: |
||||
query = query.filter_by(created_by=user_name) |
||||
self.filters['user'] = user_name |
||||
|
||||
return query |
||||
|
||||
|
||||
def prepare_sorted_query(self, query, sorting): |
||||
''' setup the base filtered query ''' |
||||
available_fields = { |
||||
'cas': 'cas_description', |
||||
'category': 'category', |
||||
'catalog': 'catalog_nr', |
||||
'vendor': 'vendor', |
||||
'pkg': 'package_size', |
||||
'price': 'unit_price', |
||||
'currency': 'currency', |
||||
'amount': 'amount', |
||||
'total': 'total_price', |
||||
'created': 'created_date' |
||||
} |
||||
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) |
||||
default_sort = self.parse_sort_parameters(self.default_sorting) |
||||
if sorting.field != default_sort.field: |
||||
default_sort = self.parse_sort_parameters(self.default_sorting) |
||||
query = self.prepare_sorted_query(query, default_sort) |
||||
return query |
Reference in new issue