From d9d630a2afc9b0ee327fd8acd3cfec1e3ed99e2c Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 4 Oct 2017 21:41:38 +0200 Subject: [PATCH] added search for orders, users and consumables --- ordr2/models/orders.py | 14 ++-- ordr2/resources/admin.py | 31 ++++++++ ordr2/resources/orders.py | 16 ++++ ordr2/templates/admin/consumable_list.jinja2 | 11 ++- ordr2/templates/admin/user_list.jinja2 | 4 +- ordr2/templates/orders/list.jinja2 | 8 +- ordr2/views/admin.py | 30 +++++++ ordr2/views/orders.py | 83 ++++++++++++++++++++ setup.py | 1 + 9 files changed, 184 insertions(+), 14 deletions(-) diff --git a/ordr2/models/orders.py b/ordr2/models/orders.py index 36d7dea..9003107 100644 --- a/ordr2/models/orders.py +++ b/ordr2/models/orders.py @@ -6,7 +6,7 @@ from collections import namedtuple from datetime import datetime from sqlalchemy import ( Column, - Date, + DateTime, Enum, Float, Integer, @@ -47,9 +47,9 @@ class Consumable(Base): unit_price = Column(Float, nullable=False) currency = Column(Text, nullable=False, default='EUR') comment = Column(Text, nullable=False, default='') - date_created = Column(Date, nullable=False, default=datetime.utcnow) + date_created = Column(DateTime, nullable=False, default=datetime.utcnow) date_modified = Column( - Date, + DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow @@ -83,13 +83,13 @@ class Order(Base): account = Column(Text, nullable=False, default='') comment = Column(Text, nullable=False, default='') - created_date = Column(Date, nullable=False, default=datetime.utcnow) + created_date = Column(DateTime, nullable=False, default=datetime.utcnow) created_by = Column(Text, nullable=False) - approval_date = Column(Date, nullable=True) + approval_date = Column(DateTime, nullable=True) approval_by = Column(Text, nullable=False, default='') - ordered_date = Column(Date, nullable=True) + ordered_date = Column(DateTime, nullable=True) ordered_by = Column(Text, nullable=False, default='') - completed_date = Column(Date, nullable=True) + completed_date = Column(DateTime, nullable=True) completed_by = Column(Text, nullable=False, default='') diff --git a/ordr2/resources/admin.py b/ordr2/resources/admin.py index f93f1ce..1b79ee2 100644 --- a/ordr2/resources/admin.py +++ b/ordr2/resources/admin.py @@ -1,3 +1,5 @@ +from sqlalchemy import or_ + from pyramid.security import Allow, Authenticated, Deny, DENY_ALL, Everyone from .base import BaseResource, PaginationResourceMixin @@ -34,6 +36,7 @@ class UserList(BaseResource, PaginationResourceMixin): def prepare_filtered_query(self, dbsession, filter_params): ''' setup the base filtered query ''' query = dbsession.query(self.sql_model_class) + role_name = filter_params.get('role', None) try: role_name = role_name.lower() @@ -42,6 +45,20 @@ class UserList(BaseResource, PaginationResourceMixin): except (AttributeError, ValueError): role_name = None self.filters['role'] = role_name + + search = filter_params.get('search', None) + if search: + term = '%{}%'.format(search) + query = query.filter( + or_( + self.sql_model_class.user_name.ilike(term), + self.sql_model_class.first_name.ilike(term), + self.sql_model_class.last_name.ilike(term), + self.sql_model_class.email.ilike(term) + ) + ) + self.filters['search'] = search + return query @@ -98,6 +115,7 @@ class ConsumableList(BaseResource, PaginationResourceMixin): 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 = category_name.lower() @@ -106,6 +124,19 @@ class ConsumableList(BaseResource, PaginationResourceMixin): except (AttributeError, ValueError): category_name = None self.filters['category'] = category_name + + search = filter_params.get('search', None) + if search: + term = '%{}%'.format(search) + query = query.filter( + or_( + self.sql_model_class.cas_description.ilike(term), + self.sql_model_class.vendor.ilike(term), + self.sql_model_class.catalog_nr.ilike(term) + ) + ) + self.filters['search'] = search + return query diff --git a/ordr2/resources/orders.py b/ordr2/resources/orders.py index e3c0af6..a41998d 100644 --- a/ordr2/resources/orders.py +++ b/ordr2/resources/orders.py @@ -1,3 +1,5 @@ +from sqlalchemy import or_ + from pyramid.security import Allow, Authenticated, Deny, DENY_ALL, Everyone from .base import BaseResource, PaginationResourceMixin @@ -54,6 +56,20 @@ class OrderList(BaseResource, PaginationResourceMixin): query = query.filter_by(created_by=user_name) self.filters['user'] = user_name + search = filter_params.get('search', None) + if search: + term = '%{}%'.format(search) + query = query.filter( + or_( + self.sql_model_class.cas_description.ilike(term), + self.sql_model_class.vendor.ilike(term), + self.sql_model_class.catalog_nr.ilike(term), + self.sql_model_class.account.ilike(term), + self.sql_model_class.created_by.ilike(term) + ) + ) + self.filters['search'] = search + return query diff --git a/ordr2/templates/admin/consumable_list.jinja2 b/ordr2/templates/admin/consumable_list.jinja2 index 7fa1102..ffcdd4e 100644 --- a/ordr2/templates/admin/consumable_list.jinja2 +++ b/ordr2/templates/admin/consumable_list.jinja2 @@ -14,11 +14,20 @@ Consumables - {{ macros.filter_box('Category', 'category', categories) }} + {{ macros.filter_box('Category', 'category', categories, {'search':None}) }}
+
+ + +
diff --git a/ordr2/templates/admin/user_list.jinja2 b/ordr2/templates/admin/user_list.jinja2 index 98cc0da..6572664 100644 --- a/ordr2/templates/admin/user_list.jinja2 +++ b/ordr2/templates/admin/user_list.jinja2 @@ -14,7 +14,7 @@ Users
- {{ macros.filter_box('Role', 'role', roles) }} + {{ macros.filter_box('Role', 'role', roles, {'search':None}) }}
@@ -22,7 +22,7 @@