Skip to content

Commit

Permalink
Fix plugins list sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpirix committed Oct 3, 2024
1 parent f351e4e commit 615ad8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
32 changes: 18 additions & 14 deletions qgis-app/plugins/templates/plugins/plugin_list_sort.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<label for="sort-options">{% trans "Sort by:" %}</label>
<span class="select is-small">
<select id="sort-options" onchange="updateSorting('sort', this.value);">
<option value=""{% if not request.GET.sort %}selected{% endif %}>-</option>
<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>
Expand All @@ -15,6 +16,7 @@
<p class="control">
<span class="select is-small">
<select id="order-options" onchange="updateSorting('order', this.value);">
<option value="" {% if not request.GET.order %}selected{% endif %}>-</option>
<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>
Expand All @@ -24,20 +26,22 @@

<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
if (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();
}

// Redirect to the updated URL
window.location.search = params.toString();
}
</script>
25 changes: 13 additions & 12 deletions qgis-app/plugins/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,18 +836,19 @@ def get_queryset(self):
qs = self.get_filtered_queryset(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)
elif not qs.ordered:
qs = qs.order_by(Lower("name"))
sort_by = self.request.GET.get('sort', None) # Default sort by name
sort_order = self.request.GET.get('order', None) # Default to ascending order

if sort_by and sort_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)
elif not qs.ordered:
qs = qs.order_by(Lower("name"))

return qs

Expand Down

0 comments on commit 615ad8b

Please sign in to comment.