ui: tags: Minor refactoring in menu filtering and sorting

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2025-03-03 14:01:29 -08:00 committed by James Valleroy
parent 29d6cb2302
commit 9555697140
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -171,17 +171,7 @@ def index(request):
})
class AppsIndexView(TemplateView):
"""View for apps index.
This view supports filtering apps by one or more tags. If no tags are
provided, it will show all the apps. If one or more tags are provided,
it will select apps matching any of the provided tags.
"""
template_name = 'apps.html'
@staticmethod
def _pick_menu_items(menu_items, selected_tags):
def _pick_menu_items(menu_items, selected_tags):
"""Return a sorted list of menu items filtered by tags."""
def _mismatch_map(menu_item) -> list[bool]:
@ -201,9 +191,8 @@ class AppsIndexView(TemplateView):
and then by the name of the menu item in current locale (by
configured collation order).
"""
return (_mismatch_map(menu_item).count(True),
_mismatch_map(menu_item), menu_item.order,
menu_item.name.lower())
return (_mismatch_map(menu_item).count(True), _mismatch_map(menu_item),
menu_item.order, menu_item.name.lower())
# Filter out menu items that don't match any of the selected tags. If
# no tags are selected, return all menu items. Otherwise, return all
@ -215,6 +204,16 @@ class AppsIndexView(TemplateView):
return sorted(filtered_menu_items, key=_sort_key)
class AppsIndexView(TemplateView):
"""View for apps index.
This view supports filtering apps by one or more tags. If no tags are
provided, it will show all the apps. If one or more tags are provided,
it will select apps matching any of the provided tags.
"""
template_name = 'apps.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['show_disabled'] = True
@ -229,8 +228,8 @@ class AppsIndexView(TemplateView):
for menu_item in menu_items:
all_tags.update(menu_item.tags or [])
context['all_tags'] = sorted(all_tags, key=lambda tag: _(tag))
context['menu_items'] = self._pick_menu_items(menu_items, tags)
context['all_tags'] = sorted(all_tags, key=_)
context['menu_items'] = _pick_menu_items(menu_items, tags)
return context