Add mechanism for user to bulk-change status for purchase credits

trying to stay pretty generic yet...
This commit is contained in:
Lance Edgar 2017-08-15 12:58:16 -05:00
parent 0516d44842
commit ec4e52fa1a
2 changed files with 163 additions and 1 deletions

View file

@ -0,0 +1,97 @@
## -*- coding: utf-8; -*-
<%inherit file="/master/index.mako" />
<%def name="extra_javascript()">
${parent.extra_javascript()}
<script type="text/javascript">
function update_change_status_button() {
var count = $('.grid tr:not(.header) td.checkbox input:checked').length;
$('button.change-status').button('option', 'disabled', count < 1);
}
$(function() {
$('.grid-wrapper').on('click', 'tr.header td.checkbox input', function() {
update_change_status_button();
});
$('.grid-wrapper').on('click', '.grid tr:not(.header) td.checkbox input', function() {
update_change_status_button();
});
$('.grid-wrapper').on('click', '.grid tr:not(.header)', function() {
update_change_status_button();
});
$('button.change-status').click(function() {
var uuids = [];
$('.grid tr:not(.header) td.checkbox input:checked').each(function() {
uuids.push($(this).parents('tr:first').data('uuid'));
});
if (! uuids.length) {
alert("You must first select one or more credits.");
return false;
}
var form = $('form[name="change-status"]');
form.find('[name="uuids"]').val(uuids.toString());
$('#change-status-dialog').dialog({
title: "Change Credit Status",
width: 500,
height: 300,
modal: true,
open: function() {
// TODO: why must we do this here instead of using auto-enhance ?
$('#change-status-dialog select[name="status"]').selectmenu();
},
buttons: [
{
text: "Submit",
click: function(event) {
disable_button(dialog_button(event));
form.submit();
}
},
{
text: "Cancel",
click: function() {
$(this).dialog('close');
}
}
]
});
});
});
</script>
</%def>
<%def name="grid_tools()">
${parent.grid_tools()}
<button type="button" class="change-status" disabled="disabled">Change Status</button>
</%def>
${parent.body()}
<div id="change-status-dialog" style="display: none;">
${h.form(url('purchases.credits.change_status'), name='change-status')}
${h.csrf_token(request)}
${h.hidden('uuids')}
<br />
<p>Please choose the appropriate status for the selected credits.</p>
<div class="fieldset">
<div class="field-wrapper status">
<label for="status">Status</label>
<div class="field">
${h.select('status', None, status_options)}
</div>
</div>
</div>
${h.end_form()}
</div>

View file

@ -28,7 +28,9 @@ from __future__ import unicode_literals, absolute_import
from rattail.db import model
from tailbone import forms
from webhelpers2.html import tags
from tailbone import forms, grids
from tailbone.views import MasterView2 as MasterView
@ -41,6 +43,7 @@ class PurchaseCreditView(MasterView):
url_prefix = '/purchases/credits'
creatable = False
editable = False
checkboxes = True
grid_columns = [
'vendor',
@ -67,6 +70,12 @@ class PurchaseCreditView(MasterView):
g.default_sortkey = 'date_received'
g.default_sortdir = 'desc'
g.filters['status'].set_value_renderer(grids.filters.EnumValueRenderer(self.enum.PURCHASE_CREDIT_STATUS))
g.filters['status'].default_active = True
g.filters['status'].default_verb = 'not_equal'
g.filters['status'].default_value = self.enum.PURCHASE_CREDIT_STATUS_SATISFIED
g.set_enum('status', self.enum.PURCHASE_CREDIT_STATUS)
# g.set_type('upc', 'gpc')
g.set_type('cases_shorted', 'quantity')
g.set_type('units_shorted', 'quantity')
@ -80,6 +89,62 @@ class PurchaseCreditView(MasterView):
g.set_label('mispick_upc', "Mispick UPC")
g.set_label('date_received', "Date")
def change_status(self):
if self.request.method != 'POST':
self.request.session.flash("Sorry, you must POST to change credit status", 'error')
return self.redirect(self.get_index_url())
status = self.request.POST.get('status', '')
if status.isdigit():
status = int(status)
else:
self.request.session.flash("Received invalid status: {}".format(status), 'error')
return self.redirect(self.get_index_url())
credits_ = []
for uuid in self.request.POST.get('uuids', '').split(','):
uuid = uuid.strip()
if uuid:
credit = self.Session.query(model.PurchaseCredit).get(uuid)
if credit:
credits_.append(credit)
if not credits_:
self.request.session.flash("Received zero valid credits", 'error')
return self.redirect(self.get_index_url())
# okay, really change status
for credit in credits_:
credit.status = status
self.request.session.flash("Changed status for {} credits".format(len(credits_)))
return self.redirect(self.get_index_url())
def template_kwargs_index(self, **kwargs):
kwargs['status_options'] = self.status_options()
return kwargs
def status_options(self):
options = []
for value in sorted(self.enum.PURCHASE_CREDIT_STATUS):
options.append(tags.Option(self.enum.PURCHASE_CREDIT_STATUS[value], value))
return options
@classmethod
def defaults(cls, config):
route_prefix = cls.get_route_prefix()
url_prefix = cls.get_url_prefix()
permission_prefix = cls.get_permission_prefix()
model_title_plural = cls.get_model_title_plural()
# change status
config.add_tailbone_permission(permission_prefix, '{}.change_status'.format(permission_prefix),
"Change status for {}".format(model_title_plural))
config.add_route('{}.change_status'.format(route_prefix), '{}/change-status'.format(url_prefix))
config.add_view(cls, attr='change_status', route_name='{}.change_status'.format(route_prefix),
permission='{}.change_status'.format(permission_prefix))
cls._defaults(config)
def includeme(config):
PurchaseCreditView.defaults(config)