From e8305b97a36104a924259cf36b279dc20e89d0df Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Tue, 3 Oct 2017 17:50:39 +0200 Subject: [PATCH] status change on multiple orders --- ordr2/events.py | 3 + ordr2/templates/emails/order.jinja2 | 36 +++++++ .../orders/edit_multiple_stati.jinja2 | 75 ++++++++++++++ ordr2/views/orders.py | 98 ++++++++++++++++++- 4 files changed, 210 insertions(+), 2 deletions(-) create mode 100755 ordr2/templates/emails/order.jinja2 create mode 100644 ordr2/templates/orders/edit_multiple_stati.jinja2 diff --git a/ordr2/events.py b/ordr2/events.py index 6b264cd..3609b8f 100644 --- a/ordr2/events.py +++ b/ordr2/events.py @@ -30,6 +30,9 @@ class PasswordReset(UserNotification): subject='[ordr] Password Reset' template = 'ordr2:templates/emails/password_reset.jinja2' +class OrderStatusChange(UserNotification): + subject='[ordr] Order Status Change' + template = 'ordr2:templates/emails/order.jinja2' @subscriber(UserLogIn) diff --git a/ordr2/templates/emails/order.jinja2 b/ordr2/templates/emails/order.jinja2 new file mode 100755 index 0000000..74421ad --- /dev/null +++ b/ordr2/templates/emails/order.jinja2 @@ -0,0 +1,36 @@ + + + + + ordr Notification + + + + +

Hi there!

+

+ Your purchase of the following item + {% if data.status.name == 'ORDERED' %} + has been orderd: + {% else %} + has arrived: + {% endif %} + {{ data }} +

+

+ If you want to check details about the purchase go here: {{ request.resource_url(request.root, 'orders', data.id) }} +

+

+ +

+

+ Regards, +
+ ordr +

+

+ Please don't respont to this email! This is an automatically generated notification by the system. + +

+ + diff --git a/ordr2/templates/orders/edit_multiple_stati.jinja2 b/ordr2/templates/orders/edit_multiple_stati.jinja2 new file mode 100644 index 0000000..21f8516 --- /dev/null +++ b/ordr2/templates/orders/edit_multiple_stati.jinja2 @@ -0,0 +1,75 @@ +{% extends "ordr2:templates/layout.jinja2" %} +{% import 'ordr2:templates/macros.jinja2' as macros with context %} + +{% block subtitle %} Oders | Change Status {% endblock subtitle %} + +{% block content %} +
+ +
+
+
+

Change Status of Order{{ 's' if orders|length > 1 }}

+
+
+ +
+
+ +
+

The status of the following order{{ 's' if orders|length > 1 }} will be changed:

+
+ +
+ + + + + + + + + + + + {% for order in orders %} + + + + + + + {% endfor %} + +
Date CreatedCAS / DescriptionPlaced byStatus
{{ order.created_date }}{{ order.cas_description }} {{ order.created_by }} + +
+ +
+
+ + +
+ +
+ +
+ +
+
+ +
+ +
+{% endblock content %} diff --git a/ordr2/views/orders.py b/ordr2/views/orders.py index 5d6258c..50fa8b7 100644 --- a/ordr2/views/orders.py +++ b/ordr2/views/orders.py @@ -4,12 +4,31 @@ 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.events import OrderStatusChange +from ordr2.models import Category, Order, OrderStatus, User from ordr2.schemas.orders import ConsumableSchema from . import update_column_display + +# helper method + +def change_in_order_status(request, order, old): + noteworthy = False + if old != OrderStatus.ORDERED and order.status == OrderStatus.ORDERED: + noteworthy = True + if old != OrderStatus.COMPLETED and order.status == OrderStatus.COMPLETED: + noteworthy = True + if noteworthy: + account = request.dbsession.\ + query(User).\ + filter_by(user_name=order.created_by).\ + first() + if account: + event = OrderStatusChange(request, account, order) + request.registry.notify(event) + + # oder list and multiple editing @view_config( @@ -34,4 +53,79 @@ def change_column_view(context, request): ''' changes the columns to display ''' update_column_display(request, 'orders') return HTTPFound(context.url()) + + +@view_config( + context='ordr2:resources.OrderList', + name='actions', + request_param='action=status', + permission='edit', + request_method='POST', + renderer='ordr2:templates/orders/edit_multiple_stati.jinja2' + ) +def edit_multiple_stati_form(context, request): + order_ids = [v for k, v in request.POST.items() if k == 'marked'] + orders = request.dbsession.\ + query(Order).\ + filter(Order.id.in_(order_ids)).\ + order_by(Order.created_date).\ + all() + if len(orders) == 0: + return HTTPFound(context.url()) + stati = [(s.name, s.value.capitalize()) for s in OrderStatus] + return {'orders': orders, 'stati': stati} + + +@view_config( + context='ordr2:resources.OrderList', + name='stati', + permission='edit', + request_method='POST' + ) +def edit_multiple_stati_form_processing(context, request): + + if 'change' in request.POST: + count = 0 + for key, value in request.POST.items(): + if not key.startswith('order-'): + continue + _, order_id = key.split('-', 1) + order = request.dbsession.query(Order).get(order_id) + if order: + old_status = order.status + try: + order.status = OrderStatus[value] + except ValueError: + pass + if old_status != order.status: + change_in_order_status(request, order, old_status) + count += 1 + + if count == 1: + request.flash('success', 'One order was updated') + elif count > 1: + msg = '{} orders were updated.'.format(count) + request.flash('success', msg) + + return HTTPFound(context.url()) + + + + + + + + + + + + + + + + + + + +