Extensions list: sort_by parameter #159

Merged
Márton Lente merged 36 commits from filter-sort into main 2024-06-03 12:57:45 +02:00
2 changed files with 29 additions and 34 deletions
Showing only changes of commit 0da53c8f38 - Show all commits

View File

@ -1,5 +1,5 @@
{% extends "common/base.html" %}
{% load i18n %}
{% load common i18n %}
{% block page_title %}{% include "extensions/components/listing_title.html" %}{% endblock page_title %}
@ -69,41 +69,13 @@
Sort by <i class="i-chevron-down"></i>
</button>
<ul class="dropdown-menu dropdown-menu-filter-sort dropdown-menu-right js-dropdown-menu" id="js-dropdown-menu-sort">
{% for option_sort_by, name in sort_by_options.items %}
<li>
<a class="dropdown-item" href="#">
Newest First
</a>
</li>
<li>
<a class="dropdown-item" href="#">
Recently Updated
</a>
</li>
<li>
<a class="dropdown-item" href="#">
Most Reviewed
</a>
</li>
<li>
<a class="dropdown-item" href="#">
Rating
</a>
</li>
<li>
<a class="dropdown-item" href="#">
Downloads
</a>
</li>
<li>
<a class="dropdown-item" href="#">
Title (A-Z)
</a>
</li>
<li>
<a class="dropdown-item" href="#">
Title (Z-A)
<a class="dropdown-item{% if option_sort_by == sort_by %} is-active{% endif %}" href="?{% query_transform sort_by=option_sort_by %}">
{{ name }}
</a>
</li>
{% endfor %}
</ul>
</div>
</div>

View File

@ -1,8 +1,10 @@
from collections import OrderedDict
import logging
from django.contrib.auth import get_user_model
from django.db.models import Count, Q
from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import gettext_lazy as _
from django.views.generic.list import ListView
@ -66,6 +68,18 @@ def extension_version_download(request, type_slug, slug, version, filename):
class SearchView(ListedExtensionsView):
paginate_by = 16
template_name = 'extensions/list.html'
default_sort_by = '-date_approved'
sort_by_options = OrderedDict(
[
('-date_approved', _('Newest First')),
('date_approved', _('Oldest First')),
# ('-total_ratings_count', _('Most Reviewed')),
('-average_score', _('Rating')),
('-download_count', _('Downloads')),
('name', _('Title (A-Z)')),
('-name', _('Title (Z-A)')),
]
)
def _get_type_id_by_slug(self):
return next(k for k, v in EXTENSION_TYPE_SLUGS.items() if v == self.kwargs['type_slug'])
@ -73,6 +87,12 @@ class SearchView(ListedExtensionsView):
def _get_type_by_slug(self):
return EXTENSION_TYPE_PLURAL[self._get_type_id_by_slug()]
def _get_sort_by(self):
sort_by = self.request.GET.get('sort_by', self.default_sort_by)
if sort_by not in self.sort_by_options:
sort_by = self.default_sort_by
return sort_by
def get_queryset(self):
queryset = super().get_queryset()
if self.kwargs.get('tag_slug'):
@ -107,7 +127,7 @@ class SearchView(ListedExtensionsView):
'preview_set__file',
'ratings',
'team',
)
).order_by(self._get_sort_by())
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
@ -121,6 +141,9 @@ class SearchView(ListedExtensionsView):
if self.kwargs.get('team_slug'):
context['team'] = get_object_or_404(teams.models.Team, slug=self.kwargs['team_slug'])
context['sort_by'] = self._get_sort_by()
context['sort_by_options'] = self.sort_by_options
# Determine which tags to list depending on the context.
tag_type_id = None
if context.get('type'):