Skip to content

Commit

Permalink
Convert deliveries page to datatable
Browse files Browse the repository at this point in the history
  • Loading branch information
sde1000 committed Jan 16, 2024
1 parent 298fd74 commit f40c336
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
53 changes: 53 additions & 0 deletions quicktill/tillweb/datatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
LogEntry,
Transline,
Department,
Delivery,
Supplier,
zero,
)

Expand Down Expand Up @@ -346,6 +348,57 @@ def sessiontotals(request, info):
})


@tillweb_view
def deliveries(request, info):
columns = {
'id': Delivery.id,
'date': Delivery.date,
'supplier': Supplier.name,
'docnumber': Delivery.docnumber,
'checked': Delivery.checked,
}
search_value = request.GET.get("search[value]")
q = td.s.query(Delivery)\
.join(Supplier)\
.options(contains_eager(Delivery.supplier))

# Apply filters from parameters. The 'unfiltered' item count for
# this table is after this filtering step.
try:
supplierid = int(request.GET.get('supplierid'))
q = q.filter(Supplier.id == supplierid)
except (ValueError, TypeError):
pass

# Apply filters from search value. The 'filtered' item count is
# after this filtering step.
fq = q
if search_value:
try:
intsearch = int(search_value)
except ValueError:
intsearch = None
qs = [
columns['supplier'].ilike(f'%{search_value}%'),
columns['docnumber'].ilike(f'{search_value}%'),
]
if intsearch:
qs.append(columns['id'] == intsearch)
fq = q.filter(or_(*qs))

return _datatables_json(
request, q, fq, columns, lambda d: {
'id': d.id,
'url': d.get_absolute_url(),
'date': d.date,
'supplier': d.supplier.name,
'supplier_url': d.supplier.get_absolute_url(),
'docnumber': d.docnumber,
'checked': d.checked,
'DT_RowClass': "table-warning" if not d.checked else None,
})


@tillweb_view
def logs(request, info):
columns = {
Expand Down
42 changes: 41 additions & 1 deletion quicktill/tillweb/templates/tillweb/deliveries.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@
<a class="btn btn-primary mb-2" href="{% url "tillweb-create-delivery" pubname=pubname %}">Add new delivery</a>
{% endif %}

{% include "tillweb/deliverylist.html" %}
<table class="table table-striped table-sm" id="deliveries">
<thead class="thead-light">
<tr>
<th scope="col">ID</th>
<th scope="col">Date</th>
<th scope="col">Supplier</th>
<th scope="col">Document</th>
<th scope="col">Confirmed?</th>
</tr>
</thead>
<tbody></tbody>
</table>

<script type="text/javascript">
$(document).ready(function(){
$("#deliveries").DataTable({
ajax: {
url: '{% url "tillweb-datatable-deliveries" pubname=pubname %}',
dataSrc: 'data'
},
columns: [
{ data: 'id',
render: render_link('url', DataTable.render.text()) },
{ data: 'date',
render: render_link('url', DataTable.render.date()),
searchable: false },
{ data: 'supplier',
render: render_link('supplier_url', DataTable.render.text()) },
{ data: 'docnumber',
render: DataTable.render.text() },
{ data: 'checked',
render: render_yesno(),
searchable: false },
],
order: [ [0, 'desc'] ],
searching: true,
paging: true,
serverSide: true
});
});
</script>

{% endblock %}
2 changes: 2 additions & 0 deletions quicktill/tillweb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
name="tillweb-datatable-translines"),
path('datatable/payments.json', datatable.payments,
name="tillweb-datatable-payments"),
path('datatable/deliveries.json', datatable.deliveries,
name="tillweb-datatable-deliveries"),
path('datatable/logs.json', datatable.logs,
name="tillweb-datatable-logs"),
path('datatable/users.json', datatable.users,
Expand Down

0 comments on commit f40c336

Please sign in to comment.