add pagination to snapshots logs

This commit is contained in:
Cayo Puigdefabregas 2022-11-28 11:58:42 +01:00
parent abc773fe6e
commit 5e2dd3344b
3 changed files with 100 additions and 9 deletions

View File

@ -1210,9 +1210,18 @@ class SnapshotListView(GenericMixin):
return flask.render_template(self.template_name, **self.context) return flask.render_template(self.template_name, **self.context)
def get_snapshots_log(self): def get_snapshots_log(self):
page = int(request.args.get('page', 1))
per_page = int(request.args.get('per_page', PER_PAGE))
snapshots_log = SnapshotsLog.query.filter( snapshots_log = SnapshotsLog.query.filter(
SnapshotsLog.owner == g.user SnapshotsLog.owner == g.user
).order_by(SnapshotsLog.created.desc()) ).order_by(SnapshotsLog.created.desc())
snapshots_log = snapshots_log.paginate(page=page, per_page=per_page)
snapshots_log.first = per_page * snapshots_log.page - per_page + 1
snapshots_log.last = len(snapshots_log.items) + snapshots_log.first - 1
return snapshots_log
logs = {} logs = {}
for snap in snapshots_log: for snap in snapshots_log:
try: try:

View File

@ -78,6 +78,12 @@ class SnapshotsLog(Thing):
snapshots.append(s) snapshots.append(s)
return snapshots and 'Update' or 'New Device' return snapshots and 'Update' or 'New Device'
def get_system_uuid(self):
try:
return self.snapshot.device.system_uuid or ''
except AttributeError:
return ''
class PlaceholdersLog(Thing): class PlaceholdersLog(Thing):
"""A Placeholder log.""" """A Placeholder log."""

View File

@ -22,6 +22,38 @@
<div class="tab-content pt-5"> <div class="tab-content pt-5">
<div id="devices-list" class="tab-pane fade devices-list active show"> <div id="devices-list" class="tab-pane fade devices-list active show">
<div class="tab-content pt-2"> <div class="tab-content pt-2">
<div class="dataTable-top" style="float: left;">
<div class="dataTable-dropdown">
<label>
<select class="dataTable-selector">
<option value="5"{% if snapshots_log.per_page == 5 %} selected="selected"{% endif %}>
5
</option>
<option value="10"{% if snapshots_log.per_page == 10 %} selected="selected"{% endif %}>
10
</option>
<option value="15"{% if snapshots_log.per_page == 15 %} selected="selected"{% endif %}>
15
</option>
<option value="20"{% if snapshots_log.per_page == 20 %} selected="selected"{% endif %}>
20
</option>
<option value="25"{% if snapshots_log.per_page == 25 %} selected="selected"{% endif %}>
25
</option>
<option value="50"{% if snapshots_log.per_page == 50 %} selected="selected"{% endif %}>
50
</option>
<option value="100"{% if snapshots_log.per_page == 100 %} selected="selected"{% endif %}>
100
</option>
</select> entries per page
</label>
</div>
<div class="dataTable-search">
</div>
</div>
<div class="dataTable-container">
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
@ -39,7 +71,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for snap in snapshots_log %} {% for snap in snapshots_log.items %}
<tr> <tr>
<td> <td>
{% if snap.sid and snap.snapshot_uuid %} {% if snap.sid and snap.snapshot_uuid %}
@ -59,26 +91,26 @@
{{ snap.version }} {{ snap.version }}
</td> </td>
<td> <td>
{% if snap.device %} {% if snap.get_device() %}
<a href="{{ url_for('inventory.device_details', id=snap.device) }}"> <a href="{{ url_for('inventory.device_details', id=snap.device) }}">
{{ snap.device }} {{ snap.get_device() }}
</a> </a>
{% endif %} {% endif %}
</td> </td>
<td> <td>
{{ snap.system_uuid }} {{ snap.get_system_uuid() }}
</td> </td>
<td> <td>
{{ snap.status }} {{ snap.get_status() }}
</td> </td>
<td> <td>
{{ snap.new_device }} {{ snap.get_new_device() }}
</td> </td>
<td> <td>
{{ snap.type_device }} {{ snap.get_type_device() }}
</td> </td>
<td> <td>
{{ snap.original_dhid }} {{ snap.get_original_dhid() }}
</td> </td>
<td>{{ snap.created.strftime('%Y-%m-%d %H:%M') }}</td> <td>{{ snap.created.strftime('%Y-%m-%d %H:%M') }}</td>
<td> <td>
@ -93,6 +125,38 @@
</tbody> </tbody>
</table> </table>
<div class="dataTable-bottom">
<div class="dataTable-info">
Showing {{ snapshots_log.first }} to {{ snapshots_log.last }} of {{ snapshots_log.total }} entries
</div>
<nav class="dataTable-pagination">
<ul class="dataTable-pagination-list">
{% if snapshots_log.has_prev %}
<li class="pager">
<a href="{{ url_for('inventory.snapshotslist', page=snapshots_log.prev_num, per_page=snapshots_log.per_page) }}"></a>
</li>
{% endif %}
{% for page in snapshots_log.iter_pages() %}
{% if page %}
{% if page == snapshots_log.page %}
<li class="active"><a href="javascript:void()">{{ page }}</a></li>
{% else %}
<li class="">
<a href="{{ url_for('inventory.snapshotslist', page=page, per_page=snapshots_log.per_page) }}">
{{ page }}
</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
{% if snapshots_log.has_next %}
<li class="pager">
<a href="{{ url_for('inventory.snapshotslist', page=snapshots_log.next_num, per_page=snapshots_log.per_page) }}"></a>
</li>
{% endif %}
</ul>
</nav>
</div>
</div> </div>
</div> </div>
@ -109,6 +173,18 @@
<!-- Custom Code --> <!-- Custom Code -->
<script> <script>
const table = new simpleDatatables.DataTable("table") $(document).ready(() => {
$(".dataTable-selector").on("change", function() {
const per_page = $('.dataTable-selector').val();
window.location.href = "{{ url_for('inventory.snapshotslist', page=1) }}&per_page="+per_page;
});
});
</script>
<script>
let table = new simpleDatatables.DataTable("table", {
footer: false,
paging: false,
})
</script> </script>
{% endblock main %} {% endblock main %}