Sort federated servers list

This commit is contained in:
Mouse Reeve 2021-03-28 13:36:05 -07:00
parent 158d2c5231
commit 7373a7e8c4
3 changed files with 34 additions and 3 deletions

View file

@ -8,13 +8,25 @@
<table class="table is-striped"> <table class="table is-striped">
<tr> <tr>
<th>{% trans "Server name" %}</th> {% url 'settings-federation' as url %}
<th>{% trans "Software" %}</th> <th>
{% trans "Server name" as text %}
{% include 'snippets/table-sort-header.html' with field="server_name" sort=sort text=text %}
</th>
<th>
{% trans "Date federated" as text %}
{% include 'snippets/table-sort-header.html' with field="created_date" sort=sort text=text %}
</th>
<th>
{% trans "Software" as text %}
{% include 'snippets/table-sort-header.html' with field="application_type" sort=sort text=text %}
</th>
<th>{% trans "Status" %}</th> <th>{% trans "Status" %}</th>
</tr> </tr>
{% for server in servers %} {% for server in servers %}
<tr> <tr>
<td><a href="{% url 'settings-federated-server' server.id %}">{{ server.server_name }}</a></td> <td><a href="{% url 'settings-federated-server' server.id %}">{{ server.server_name }}</a></td>
<td>{{ server.created_date }}</td>
<td>{{ server.application_type }} ({{ server.application_version }})</td> <td>{{ server.application_type }} ({{ server.application_version }})</td>
<td>{{ server.status }}</td> <td>{{ server.status }}</td>
</tr> </tr>

View file

@ -0,0 +1,13 @@
{% load i18n %}
<a href="{{ url }}?sort={% if sort == field %}-{% endif %}{{ field }}">
{{ text }}
{% if sort == field %}
<span class="icon icon-arrow-down">
<span class="is-sr-only">{% trans "Sorted asccending" %}</span>
</span>
{% elif sort == "-"|add:field %}
<span class="icon icon-arrow-up">
<span class="is-sr-only">{% trans "Sorted descending" %}</span>
</span>
{% endif %}
</a>

View file

@ -27,8 +27,14 @@ class Federation(View):
page = 1 page = 1
servers = models.FederatedServer.objects.all() servers = models.FederatedServer.objects.all()
sort = request.GET.get('sort')
sort_fields = ['created_date', 'application_type', 'server_name']
if sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]:
servers = servers.order_by(sort)
paginated = Paginator(servers, PAGE_LENGTH) paginated = Paginator(servers, PAGE_LENGTH)
data = {"servers": paginated.page(page)} data = {"servers": paginated.page(page), "sort": sort}
return TemplateResponse(request, "settings/federation.html", data) return TemplateResponse(request, "settings/federation.html", data)