Browse Source

added consumable resources

php2python
Holger Frey 7 years ago
parent
commit
627826ab03
  1. 66
      ordr2/resources/admin.py

66
ordr2/resources/admin.py

@ -1,10 +1,10 @@ @@ -1,10 +1,10 @@
from pyramid.security import Allow, Authenticated, Deny, DENY_ALL, Everyone
from .base import BaseResource, PaginationResourceMixin
from ordr2.models import User, Role
from ordr2.models import Category, Consumable, User, Role
# user accounr resources
class UserAccount(BaseResource):
def __acl__(self):
@ -67,6 +67,68 @@ class UserList(BaseResource, PaginationResourceMixin): @@ -67,6 +67,68 @@ class UserList(BaseResource, PaginationResourceMixin):
# consumables resources
class ConsumableResource(BaseResource):
def __acl__(self):
return [
(Allow, 'role:admin', 'view'),
(Allow, 'role:admin', 'edit'),
(Allow, 'role:admin', 'delete'),
DENY_ALL
]
class ConsumableList(BaseResource, PaginationResourceMixin):
sql_model_class = Consumable
child_resource_class = ConsumableResource
default_sorting = 'cas.asc'
default_items_per_page = 12
def __acl__(self):
return [
(Allow, 'role:admin', 'view'),
(Allow, 'role:admin', 'edit'),
(Allow, 'role:admin', 'delete'),
DENY_ALL
]
def prepare_filtered_query(self, dbsession, filter_params):
''' setup the base filtered query '''
query = dbsession.query(self.sql_model_class)
category_name = filter_params.get('category', None)
try:
category_name = cat_name.lower()
category = Category(category_name)
query = query.filter_by(category=category)
except (AttributeError, ValueError):
category_name = None
self.filters['category'] = category_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'
}
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
class Admin(BaseResource):
nodes = {