Skip to content

Commit

Permalink
Plugin list sorting options
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpirix committed Oct 3, 2024
1 parent 0416d07 commit f351e4e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 42 deletions.
51 changes: 29 additions & 22 deletions qgis-app/plugins/templates/plugins/plugin_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,30 @@ <h2>{% if title %}{{title}}{% else %}{% trans "All plugins" %}{% endif %}</h2>
<span>
{% blocktrans with records_count=page_obj.paginator.count %}{{ records_count }} records found{% endblocktrans %}
</span>
<div class="field has-addons is-justify-content-center">
<p class="control">
<button id="grid-view-btn" class="button is-small is-info">
<span class="icon is-small">
<i class="fas fa-th"></i>
</span>
<span>Grid View</span>
</button>
</p>
<p class="control">
<button id="table-view-btn" class="button is-small">
<span class="icon is-small">
<i class="fas fa-table"></i>
</span>
<span>Table View</span>
</button>
</p>
<div class="mt-3 is-flex is-justify-content-space-between is-flex-wrap-wrap">
<div>
<div class="field has-addons">
<p class="control">
<button id="grid-view-btn" class="button is-small is-info">
<span class="icon is-small">
<i class="fas fa-th"></i>
</span>
<span>Grid View</span>
</button>
</p>
<p class="control">
<button id="table-view-btn" class="button is-small">
<span class="icon is-small">
<i class="fas fa-table"></i>
</span>
<span>Table View</span>
</button>
</p>
</div>
</div>
<div>
{% include "plugins/plugin_list_sort.html" %}
</div>
</div>

<div id="grid-view" class="columns is-multiline plugins-list-grid">
Expand All @@ -86,12 +93,12 @@ <h2>{% if title %}{{title}}{% else %}{% trans "All plugins" %}{% endif %}</h2>
<thead>
<tr>
<th class="pt-3 pb-3">&nbsp;</th>
<th class="pt-3 pb-3">{% anchor name %}</th>
<th class="pt-3 pb-3">{% trans "Name" %}</th>
{% if not user.is_anonymous %}<th class="pt-3 pb-3"><img title="{% trans "Approved" %}" src="{% static "images/tick_16.png" %}" alt="{% trans "Approved" %}"/></th>{% endif %}
<th class="pt-3 pb-3">{% anchor downloads %}</th>
<th class="pt-3 pb-3">{% anchor author "Author" %}</th>
<th class="pt-3 pb-3">{% anchor latest_version_date "Latest Plugin Version" %}</th>
<th class="pt-3 pb-3">{% anchor average_vote "Stars (votes)" %}</th>
<th class="pt-3 pb-3">{% trans "Downloads" %}</th>
<th class="pt-3 pb-3">{% trans "Author" %}</th>
<th class="pt-3 pb-3">{% trans "Latest Plugin Version" %}</th>
<th class="pt-3 pb-3">{% trans "Stars (votes)" %}</th>
{% if user.is_authenticated %}<th colspan="2" class="pt-3 pb-3">{% trans "Manage" %}</th>{% endif %}
</tr>
</thead>
Expand Down
43 changes: 43 additions & 0 deletions qgis-app/plugins/templates/plugins/plugin_list_sort.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% load i18n %}
<div class="field is-grouped">
<p class="control">
<label for="sort-options">{% trans "Sort by:" %}</label>
<span class="select is-small">
<select id="sort-options" onchange="updateSorting('sort', this.value);">
<option value="name" {% if request.GET.sort == 'name' %}selected{% endif %}>{% trans "Name" %}</option>
<option value="downloads" {% if request.GET.sort == 'downloads' %}selected{% endif %}>{% trans "Downloads" %}</option>
<option value="author" {% if request.GET.sort == 'author' %}selected{% endif %}>{% trans "Author" %}</option>
<option value="latest_version_date" {% if request.GET.sort == 'latest_version_date' %}selected{% endif %}>{% trans "Latest Version" %}</option>
<option value="average_vote" {% if request.GET.sort == 'average_vote' %}selected{% endif %}>{% trans "Stars" %}</option>
</select>
</span>
</p>
<p class="control">
<span class="select is-small">
<select id="order-options" onchange="updateSorting('order', this.value);">
<option value="asc" {% if request.GET.order == 'asc' %}selected{% endif %}>{% trans "Ascending" %}</option>
<option value="desc" {% if request.GET.order == 'desc' %}selected{% endif %}>{% trans "Descending" %}</option>
</select>
</span>
</p>
</div>

<script>
function updateSorting(key, value) {
// Get current URL parameters
let params = new URLSearchParams(window.location.search);

// Update the relevant parameter (sort/order) based on the selection
params.set(key, value);

// Preserve the other sorting option (sort/order)
if (key === 'sort' && !params.has('order')) {
params.set('order', 'asc'); // Default to ascending if no order is selected
} else if (key === 'order' && !params.has('sort')) {
params.set('sort', 'name'); // Default to sorting by name if no sort field is selected
}

// Redirect to the updated URL
window.location.search = params.toString();
}
</script>
40 changes: 20 additions & 20 deletions qgis-app/plugins/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,30 +834,30 @@ def get_filtered_queryset(self, qs):
def get_queryset(self):
qs = super(PluginsList, self).get_queryset()
qs = self.get_filtered_queryset(qs)
sort_by = self.request.GET.get("sort", None)
if sort_by:
if sort_by[0] == "-":
_sort_by = sort_by[1:]
else:
_sort_by = sort_by

# Check if the sort criterion is a field or 'average_vote'
# or 'latest_version_date'
try:
(
_sort_by == "average_vote"
or _sort_by == "latest_version_date"
or self.model._meta.get_field(_sort_by)
)
except FieldDoesNotExist:
return qs
# Get the sort and order parameters from the URL (with default values)
sort_by = self.request.GET.get('sort', 'name') # Default sort by name
sort_order = self.request.GET.get('order', 'asc') # Default to ascending order

# Determine the correct sorting direction
if sort_order == 'desc':
sort_by = '-' + sort_by # Prepend '-' to sort in descending order

# Validate the sort field
if sort_by.lstrip('-') in ['average_vote', 'latest_version_date'] or self._is_valid_field(sort_by.lstrip('-')):
qs = qs.order_by(sort_by)
else:
# default
if not qs.ordered:
qs = qs.order_by(Lower("name"))
elif not qs.ordered:
qs = qs.order_by(Lower("name"))

return qs

def _is_valid_field(self, field_name):
try:
self.model._meta.get_field(field_name)
return True
except FieldDoesNotExist:
return False

def get_context_data(self, **kwargs):
context = super(PluginsList, self).get_context_data(**kwargs)
context.update(
Expand Down

0 comments on commit f351e4e

Please sign in to comment.