From 0416d07195022fa4186f5968c06853615caf26ce Mon Sep 17 00:00:00 2001 From: Lova Andriarimalala <43842786+Xpirix@users.noreply.github.com> Date: Thu, 3 Oct 2024 11:43:17 +0300 Subject: [PATCH] Sort the search result by download number --- qgis-app/custom_haystack_urls.py | 14 ++++++++++++-- .../templates/plugins/plugin_list_grid_card.html | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/qgis-app/custom_haystack_urls.py b/qgis-app/custom_haystack_urls.py index c14fa7f5..8de61755 100755 --- a/qgis-app/custom_haystack_urls.py +++ b/qgis-app/custom_haystack_urls.py @@ -28,9 +28,19 @@ def build_form(self, form_kwargs=None): def get_results(self): """ - Fetches the results + Fetches the search results and sorts them in descending order based on the 'downloads' attribute. + If the 'downloads' attribute is not present or the object is None, it defaults to 0. """ - return self.form.searchqueryset + results = self.form.searchqueryset + sort_by = 'downloads' + results = sorted( + results, + key=lambda x: int( + getattr(x.object, sort_by) + ) if x.object is not None else 0, + reverse=True # Reverse the sort order + ) + return results urlpatterns = [ diff --git a/qgis-app/plugins/templates/plugins/plugin_list_grid_card.html b/qgis-app/plugins/templates/plugins/plugin_list_grid_card.html index 9809f81c..4a03c8e9 100644 --- a/qgis-app/plugins/templates/plugins/plugin_list_grid_card.html +++ b/qgis-app/plugins/templates/plugins/plugin_list_grid_card.html @@ -1,6 +1,7 @@ {% load i18n plugin_utils plugins_tagcloud thumbnail static%} {% load local_timezone humanize %} +{% if object.package_name %}