add label printing to product index

This commit is contained in:
Lance Edgar 2012-08-01 05:54:03 -07:00
parent b4193db0f2
commit 6212932f74
3 changed files with 104 additions and 2 deletions

View file

@ -38,6 +38,9 @@ def before_render(event):
* ``rattail``
"""
# Import labels module so it's available if/when needed.
import rattail.labels
request = event.get('request') or threadlocal.get_current_request()
renderer_globals = event

View file

@ -7,6 +7,21 @@
${parent.head_tags()}
<style type="text/css">
table.header td.tools table {
margin-left: auto;
}
table.header td.tools table th,
table.header td.tools table td {
padding: 0px;
text-align: left;
}
table.header td.tools table #label-quantity {
text-align: right;
width: 30px;
}
div.grid.Product table tbody td.size,
div.grid.Product table tbody td.regular_price,
div.grid.Product table tbody td.sale_price {
@ -14,7 +29,58 @@
text-align: right;
}
div.grid.Product table tbody td.labels {
text-align: center;
}
</style>
% if edbob.config.getboolean('rattail.labels', 'enabled', default=False):
<script language="javascript" type="text/javascript">
$(function() {
$('div.grid.Product a.print-label').live('click', function() {
var quantity = $('#label-quantity').val();
// TODO: Validate quantity as integer etc.
$.ajax({
url: '${url('products.print_label')}',
data: {
'uuid': get_uuid(this),
'profile': $('#label-profile').val(),
'quantity': quantity,
},
});
alert("Label has been printed.");
return false;
});
});
</script>
% endif
</%def>
<%def name="tools()">
% if edbob.config.getboolean('rattail.labels', 'enabled', default=False):
<table>
<thead>
<tr>
<td>Label</td>
<td>Qty.</td>
</tr>
</thead>
<tbody>
<td>
<select name="label-profile" id="label-profile">
% for profile in rattail.labels.iter_profiles():
<option value="${profile.name}">${profile.display_name}</option>
% endfor
</select>
</td>
<td>
<input type="text" name="label-quantity" id="label-quantity" value="1" />
</td>
</tbody>
</table>
% endif
</%def>
${parent.body()}

View file

@ -26,12 +26,18 @@
``rattail.pyramid.views.products`` -- Product Views
"""
from webhelpers.html.tags import link_to
from edbob.pyramid.filters import filter_ilike
from edbob.pyramid.grids import sorter
from edbob.pyramid.views import GridView
from edbob.pyramid.views.crud import Crud
import edbob
from edbob.pyramid import Session
import rattail
import rattail.labels
from rattail.pyramid.forms import (
UpcFieldRenderer, RegularPriceFieldRenderer, SalePriceFieldRenderer)
@ -102,6 +108,12 @@ class ProductGrid(GridView):
g.sale_price.label("Sale Price"),
],
readonly=True)
def callback(prod):
return link_to("Print", '#', class_='print-label')
if edbob.config.getboolean('rattail.labels', 'enabled', default=False):
g.add_column('labels', "Labels", callback)
return g
@ -119,10 +131,31 @@ class ProductCrud(Crud):
return fs
def print_label(request):
profile = request.params.get('profile')
profile = rattail.labels.get_profile(profile) if profile else None
assert profile
uuid = request.params.get('uuid')
product = Session.query(rattail.Product).get(uuid) if uuid else None
assert product
quantity = request.params.get('quantity')
assert quantity.isdigit()
quantity = int(quantity)
printer = profile.get_printer()
printer.print_labels([(product, quantity)])
return {}
def includeme(config):
ProductGrid.add_route(config, 'products.list', '/products')
ProductCrud.add_routes(config)
config.add_route('products.print_label', '/products/label')
config.add_view(print_label, route_name='products.print_label', renderer='json')
# from sqlalchemy.orm import joinedload