Skip to content

Commit

Permalink
Add a datatable showing user tokens
Browse files Browse the repository at this point in the history
In the future we could expand this to creating, editing and
deleting user tokens.
  • Loading branch information
sde1000 committed Jan 24, 2024
1 parent 8b7abb6 commit 1cc51be
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
40 changes: 39 additions & 1 deletion quicktill/tillweb/datatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Transaction,
Payment,
User,
UserToken,
LogEntry,
Transline,
Department,
Expand Down Expand Up @@ -62,11 +63,15 @@ def _datatables_json(request, query, filtered_query, columns, rowfunc):
if cexpr is not None:
order_columns.append(cexpr)
else:
error = f'column {cname} not defined'
error = f'column {cname} not defined, valid columns are '\
f'{list(columns.keys())}'
break
else:
break

if error:
return JsonResponse({'error': error})

q = _datatables_order(filtered_query, order_columns, request.GET)
q = _datatables_paginate(q, request.GET)
r = {
Expand Down Expand Up @@ -665,6 +670,39 @@ def users(request, info):
})


@tillweb_view
def tokens(request, info):
columns = {
'token': UserToken.token,
'description': UserToken.description,
'last_seen': UserToken.last_seen,
'user': User.fullname,
}
q = td.s.query(UserToken)\
.join(User, isouter=True)\
.options(contains_eager(UserToken.user))

fq = q

search_value = request.GET.get("search[value]")
if search_value:
qs = [
columns['token'].ilike(f"%{search_value}%"),
columns['description'].ilike(f"%{search_value}%"),
columns['user'].ilike(f"%{search_value}%"),
]
fq = fq.filter(or_(*qs))

return _datatables_json(
request, q, fq, columns, lambda t: {
'token': t.token,
'description': t.description,
'last_seen': t.last_seen,
'user': t.user.fullname if t.user else None,
'user_url': t.user.get_absolute_url() if t.user else None,
})


# N.B. This is _not_ a serverSide: true datatable; we just need to
# return the requested data
@tillweb_view
Expand Down
1 change: 1 addition & 0 deletions quicktill/tillweb/templates/tillweb/tillweb.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
<div class="dropdown-menu">
<a class="dropdown-item" href="{% url "tillweb-till-users" pubname=pubname %}">Users</a>
<a class="dropdown-item" href="{% url "tillweb-till-groups" pubname=pubname %}">Groups</a>
<a class="dropdown-item" href="{% url "tillweb-tokens" pubname=pubname %}">Tokens</a>
</div>
</li>
<li class="nav-item"><a class="nav-link" href="{% url "tillweb-logs" pubname=pubname %}">Logs</a></li>
Expand Down
55 changes: 55 additions & 0 deletions quicktill/tillweb/templates/tillweb/tokens.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% extends "tillweb/tillweb.html" %}

{% block title %}{{till}} — User tokens{% endblock %}

{% block tillcontent %}

{% comment %}
{% if may_create_token %}
<a class="btn btn-primary mb-2" id="add_new_token_button" href="{% url "tillweb-create-token" pubname=pubname %}">Add new token</a>
{% endif %}
{% endcomment %}

<table class="table table-sm table-striped" id="tokenlist">
<thead class="thead-light">
<tr>
<th scope="col">Token</th>
<th scope="col">Description</th>
<th scope="col">Last seen</th>
<th scope="col">Assigned user</th>
</tr>
</thead>
<tbody></tbody>
</table>

<script type="text/javascript">
$(document).ready(function() {
const tokenlist_api = $("#tokenlist").DataTable({
ajax: {
url: '{% url "tillweb-datatable-tokens" pubname=pubname %}',
dataSrc: 'data',
},
columns: [
{ data: 'token',
render: DataTable.render.text(),
},
{ data: 'description',
render: DataTable.render.text(),
},
{ data: 'last_seen',
render: DataTable.render.datetime(),
searchable: false,
},
{ data: 'user',
render: render_link('user_url', DataTable.render.text()),
},
],
order: [ [2, 'asc'] ],
searching: true,
paging: true,
serverSide: true,
});
});
</script>

{% endblock %}
4 changes: 4 additions & 0 deletions quicktill/tillweb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
name="tillweb-till-group"),
path('new/group/', views.create_group, name="tillweb-create-till-group"),

path('token/', views.tokenlist, name="tillweb-tokens"),

path('logs/', views.logsindex, name="tillweb-logs"),
path('logs/<int:logid>', views.logdetail, name="tillweb-logentry"),

Expand Down Expand Up @@ -143,6 +145,8 @@
name="tillweb-datatable-logs"),
path('datatable/users.json', datatable.users,
name="tillweb-datatable-users"),
path('datatable/tokens.json', datatable.tokens,
name="tillweb-datatable-tokens"),
path('datatable/depttotals.json', datatable.depttotals,
name="tillweb-datatable-depttotals"),
path('datatable/usertotals.json', datatable.usertotals,
Expand Down
7 changes: 7 additions & 0 deletions quicktill/tillweb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2837,6 +2837,13 @@ def create_group(request, info):
})


@tillweb_view
def tokenlist(request, info):
return ('tokens.html', {
'nav': [("Tokens", info.reverse("tillweb-tokens"))],
})


@tillweb_view
def logsindex(request, info):
return ('logs.html', {
Expand Down

0 comments on commit 1cc51be

Please sign in to comment.