From 36e268300f3b1d4d31f275c2526e7d6a75a9d5ed Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Tue, 9 Jul 2024 14:23:59 +0700 Subject: [PATCH] 14731 sort / status --- netbox/core/choices.py | 26 +++++++++++++++++++++++ netbox/core/views.py | 29 +++++++++++++++++++------- netbox/templates/core/plugin_list.html | 20 +++++++++++++++--- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/netbox/core/choices.py b/netbox/core/choices.py index ee0febaff..3bd174064 100644 --- a/netbox/core/choices.py +++ b/netbox/core/choices.py @@ -81,3 +81,29 @@ class ObjectChangeActionChoices(ChoiceSet): (ACTION_UPDATE, _('Updated'), 'blue'), (ACTION_DELETE, _('Deleted'), 'red'), ) + + +class PluginSortChoices(ChoiceSet): + + SORT_NAME_AZ = 'az' + SORT_NAME_ZA = 'za' + SORT_UPDATED = 'updated' + SORT_PUBLISHED = 'published' + + CHOICES = ( + (SORT_NAME_AZ, _('By Name (A-Z)'), 'az'), + (SORT_NAME_ZA, _('By Name (Z-A)'), 'za'), + (SORT_UPDATED, _('By updated date'), 'updated'), + (SORT_PUBLISHED, _('By published date'), 'published'), + ) + + +class PluginStatusChoices(ChoiceSet): + + STATUS_ALL = 'all' + STATUS_INSTALLED = 'installed' + + CHOICES = ( + (STATUS_ALL, _('All'), 'all'), + (STATUS_INSTALLED, _('Installed'), 'installed'), + ) diff --git a/netbox/core/views.py b/netbox/core/views.py index 8f5826520..2e8ae27a9 100644 --- a/netbox/core/views.py +++ b/netbox/core/views.py @@ -2,8 +2,10 @@ import importlib import importlib.util import json import platform +import pytz import requests +from datetime import datetime from django import __version__ as DJANGO_VERSION from django.apps import apps from django.conf import settings @@ -38,6 +40,7 @@ from utilities.htmx import htmx_partial from utilities.query import count_related from utilities.views import ContentTypePermissionRequiredMixin, GetRelatedModelsMixin, register_model_view from . import filtersets, forms, tables +from .choices import PluginSortChoices, PluginStatusChoices from .models import * from .tables import CertifiedPluginTable @@ -673,8 +676,8 @@ def get_local_plugins(plugins): 'tag_line': plugin_config.description, 'description_short': plugin_config.description, 'author': plugin_config.author or _('Unknown Author'), - 'version': plugin_config.version, - 'icon': None, + 'created': datetime.min.replace(tzinfo=pytz.UTC), + 'updated': datetime.min.replace(tzinfo=pytz.UTC), 'is_local': True, 'is_installed': True, 'is_certified': False, @@ -753,8 +756,8 @@ def get_catalog_plugins(plugins): 'tag_line': data['tag_line'], 'description_short': data['description_short'], 'author': data['author']['name'] or _('Unknown Author'), - 'version': 'x', - 'icon': None, + 'created': datetime.fromisoformat(data['created_at']), + 'updated': datetime.fromisoformat(data['updated_at']), 'is_local': False, 'is_installed': False, 'is_certified': data['release_latest']['is_certified'], @@ -783,11 +786,23 @@ class PluginListView(UserPassesTestMixin, View): return self.request.user.is_staff def get(self, request): + sort = request.GET.get('sort', PluginSortChoices.SORT_NAME_AZ) + status = request.GET.get('status', PluginStatusChoices.STATUS_ALL) + q = request.GET.get('q', None) - # Plugins plugins = get_plugins() - plugins = [v for k, v in plugins.items()] - plugins = sorted(plugins, key=lambda d: d['name']) + if status == PluginStatusChoices.STATUS_INSTALLED: + plugins = [v for k, v in plugins.items() if v['is_installed']] + else: + plugins = [v for k, v in plugins.items()] + if sort == PluginSortChoices.SORT_NAME_ZA: + plugins = sorted(plugins, key=lambda d: d['name'], reverse=True) + elif sort == PluginSortChoices.SORT_UPDATED: + plugins = sorted(plugins, key=lambda d: d['updated']) + elif sort == PluginSortChoices.SORT_PUBLISHED: + plugins = sorted(plugins, key=lambda d: d['created']) + else: + plugins = sorted(plugins, key=lambda d: d['name']) return render(request, 'core/plugin_list.html', { 'plugins': plugins, diff --git a/netbox/templates/core/plugin_list.html b/netbox/templates/core/plugin_list.html index 958d70737..384a28e63 100644 --- a/netbox/templates/core/plugin_list.html +++ b/netbox/templates/core/plugin_list.html @@ -36,13 +36,18 @@