From 26f7ea962c1d73535d2e0501b31ec5f5d269b0fe Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Tue, 3 Oct 2017 11:29:00 +0200 Subject: [PATCH] added viewing order list --- ordr2/events.py | 19 +-- ordr2/resources/__init__.py | 3 +- ordr2/resources/orders.py | 14 +- ordr2/templates/orders/order_list.jinja2 | 190 +++++++++++++++++++++++ ordr2/views/__init__.py | 20 +++ ordr2/views/orders.py | 37 +++++ 6 files changed, 266 insertions(+), 17 deletions(-) create mode 100644 ordr2/templates/orders/order_list.jinja2 create mode 100644 ordr2/views/orders.py diff --git a/ordr2/events.py b/ordr2/events.py index 3218f93..6b264cd 100644 --- a/ordr2/events.py +++ b/ordr2/events.py @@ -2,6 +2,9 @@ from pyramid.events import NewRequest, subscriber from pyramid.renderers import render from pyramid_mailer.message import Message +from ordr2.views import set_display_defaults + + class UserLogIn(object): def __init__(self, request, user): self.request = request @@ -28,18 +31,16 @@ class PasswordReset(UserNotification): template = 'ordr2:templates/emails/password_reset.jinja2' + @subscriber(UserLogIn) +def set_display_defaults_on_log_in(event): + set_display_defaults(event.request) + + @subscriber(NewRequest) -def set_display_defaults(event): +def check_display_defaults(event): if event.request.user and 'display' not in event.request.session: - defaults = { - 'users': { - 'first': True, - 'last': True, - 'email': True, - } - } - event.request.session['display'] = defaults + set_display_defaults(event.request) @subscriber(UserNotification) diff --git a/ordr2/resources/__init__.py b/ordr2/resources/__init__.py index d6bb8ac..601a6f5 100644 --- a/ordr2/resources/__init__.py +++ b/ordr2/resources/__init__.py @@ -20,7 +20,8 @@ class Root(BaseResource): nodes = { 'account': Account, - 'admin': Admin + 'admin': Admin, + 'orders': OrderList } def __init__(self, request): diff --git a/ordr2/resources/orders.py b/ordr2/resources/orders.py index 078cc2a..e3c0af6 100644 --- a/ordr2/resources/orders.py +++ b/ordr2/resources/orders.py @@ -14,8 +14,8 @@ class OrderResource(BaseResource): (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( (Allow, 'user:' + str(self.model.created_by), 'edit') ) + acl.append( (Allow, 'user:' + str(self.model.created_by), 'delete') ) acl.append(DENY_ALL) return acl @@ -23,7 +23,7 @@ class OrderResource(BaseResource): class OrderList(BaseResource, PaginationResourceMixin): sql_model_class = Order child_resource_class = OrderResource - default_sorting = 'created.asc' + default_sorting = 'created.desc' default_items_per_page = 12 def __acl__(self): @@ -47,7 +47,7 @@ class OrderList(BaseResource, PaginationResourceMixin): query = query.filter_by(status=status) except (AttributeError, ValueError): status_name = None - self.filters['category'] = status_name + self.filters['status'] = status_name user_name = filter_params.get('user', None) if user_name: @@ -64,12 +64,12 @@ class OrderList(BaseResource, PaginationResourceMixin): 'category': 'category', 'catalog': 'catalog_nr', 'vendor': 'vendor', - 'pkg': 'package_size', 'price': 'unit_price', - 'currency': 'currency', 'amount': 'amount', 'total': 'total_price', - 'created': 'created_date' + 'created': 'created_date', + 'user': 'created_by', + 'status': 'status', } name = available_fields.get(sorting.field, None) model_field = getattr(self.sql_model_class, name, None) diff --git a/ordr2/templates/orders/order_list.jinja2 b/ordr2/templates/orders/order_list.jinja2 new file mode 100644 index 0000000..4c56b81 --- /dev/null +++ b/ordr2/templates/orders/order_list.jinja2 @@ -0,0 +1,190 @@ +{% extends "ordr2:templates/layout.jinja2" %} +{% import 'ordr2:templates/macros.jinja2' as macros with context %} + +{% block subtitle %} Orders {% endblock subtitle %} + +{% block content %} +
+
+ +
+
+
+

+ Users +

+
+ {{ macros.filter_box('All Orders', 'status', stati, {'user': None} ) }} + {{ macros.filter_box('My Orders', 'status', stati, {'user': request.user.user_name} ) }} +
+ +
+
+ +
+ +
+ {% if request.has_permission('create', context) %} + + {% endif %} + {% if request.has_permission('edit', context) or request.has_permission('delete', context) %} +
+ {% if request.has_permission('edit', context) %} + + {% endif %} + {% if request.has_permission('delete', context) %} + + {% endif %} +
+ {% endif %} + + + + +
+
+ + {{ macros.flash_messages() }} + + {% if orders %} + {{ macros.show_or_hide_columns('orders') }} + + + + + {{ macros.sortable_table_header('Date Created', 'created') }} + {{ macros.sortable_table_header('CAS / Description', 'cas') }} + {{ macros.sortable_table_header('Vendor', 'vendor') }} + {{ macros.sortable_table_header('Catalog Nr.', 'catalog') }} + {{ macros.sortable_table_header('Unit Price', 'price') }} + {{ macros.sortable_table_header('Quantity', 'amount') }} + {{ macros.sortable_table_header('Total Price', 'total') }} + {{ macros.sortable_table_header('Account', 'account') }} + {{ macros.sortable_table_header('Category', 'category') }} + {{ macros.sortable_table_header('Status', 'status') }} + {{ macros.sortable_table_header('Ordered By', 'user') }} + + + + + {% for order in orders %} + + + + + + + + + + + + + + + + {% endfor %} + +
+ + Actions
+ + {{ order.model.created_date }}{{ order.model.cas_description }}{{ order.model.vendor }}{{ order.model.catalog_nr }}{{ '%.2f'|format(order.model.unit_price) }} {{ order.model.currency }}{{ order.model.amount }}{{ '%.2f'|format(order.model.total_price) }} {{ order.model.currency }}{{ order.model.category.value|capitalize }}{{ order.model.status.value|capitalize }} + {{ order.model.created_by }} + + {% if request.has_permission('edit', order) %} + edit + {% else %} + edit + {% endif %} + {% if request.has_permission('delete', order) %} + delete + {% endif %} +
+ + {{ macros.pagination() }} + {% else %} +
+

Oh snap! Nothing to display!

+

Your query didn't return any data.

+
+ {% endif %} +
+
+
+
+ +
+{% endblock content %} diff --git a/ordr2/views/__init__.py b/ordr2/views/__init__.py index 435f66b..b3684d3 100644 --- a/ordr2/views/__init__.py +++ b/ordr2/views/__init__.py @@ -22,6 +22,26 @@ def update_column_display(request, section): request.session['display'][section] = display +def set_display_defaults(request): + defaults = { + 'users': { + 'first': True, + 'last': True, + 'email': True, + }, + 'orders': { + 'account': False, + 'cas': True, + 'catalog': False, + 'vendor': True, + 'price': False, + 'amount': False, + 'total': True, + } + } + request.session['display'] = defaults + + def includeme(config): ''' adding request helpers and static views ''' config.add_request_method(flash, 'flash') diff --git a/ordr2/views/orders.py b/ordr2/views/orders.py new file mode 100644 index 0000000..5d6258c --- /dev/null +++ b/ordr2/views/orders.py @@ -0,0 +1,37 @@ +import deform + +from pyramid.httpexceptions import HTTPFound +from pyramid.renderers import render +from pyramid.view import view_config + +from ordr2.events import AccountActivation, PasswordReset +from ordr2.models import Category, Order, OrderStatus +from ordr2.schemas.orders import ConsumableSchema + +from . import update_column_display + +# oder list and multiple editing + +@view_config( + context='ordr2:resources.OrderList', + permission='view', + renderer='ordr2:templates/orders/order_list.jinja2' + ) +def order_list(context, request): + ''' display the order list ''' + orders = context.items() + stati = [(s.value.lower(), s.value.capitalize()) for s in OrderStatus] + return {'orders':orders, 'stati':stati} + + +@view_config( + context='ordr2:resources.OrderList', + name = 'changeview', + permission='view', + request_method='POST' + ) +def change_column_view(context, request): + ''' changes the columns to display ''' + update_column_display(request, 'orders') + return HTTPFound(context.url()) +