From d70632c624eacb4990d967e368aa7962721426e1 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Fri, 24 Apr 2020 16:17:20 +0200 Subject: [PATCH] Make event logs filter more user friendly --- logs/forms.py | 58 ++++++++++++++++++++++++++++++++++++++++---------- logs/models.py | 24 ++++++++++++++++++--- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/logs/forms.py b/logs/forms.py index 72212b0c..ffae8e52 100644 --- a/logs/forms.py +++ b/logs/forms.py @@ -28,6 +28,7 @@ from re2o.base import get_input_formats_help_text import inspect # Import all models in which there are classes to be filtered on +import cotisations.models import machines.models import preferences.models import tickets.models @@ -35,7 +36,23 @@ import topologie.models import users.models -def get_classes(module): +CHOICES_ACTION_TYPE = ( + ("users", _("Users")), + ("machines", _("Machines")), + ("subscriptions", _("Subscription")), + ("whitelists", _("Whitelists")), + ("bans", _("Bans")), + ("topology", _("Topology")), + ("all", _("All")), +) + +CHOICES_TYPE = ( + ("ip", _("IPv4")), + ("mac", _("MAC address")), +) + + +def all_classes(module): classes = [] for name, obj in inspect.getmembers(module): @@ -45,18 +62,37 @@ def get_classes(module): return classes -# Get the list of all imported classes -modules = [machines.models, preferences.models, tickets.models, topologie.models, users.models] -classes = sum([get_classes(m) for m in modules], []) +def classes_for_action_type(action_type): + """Return the list of class names to be displayed for a + given actions type filter""" + if action_type == "users": + return [ + users.models.User.__name__, + users.models.Adherent.__name__, + users.models.Club.__name__, + users.models.EMailAddress.__name__ + ] -CHOICES_ACTION_TYPE = [ - (str(i), classes[i]) for i in range(len(classes)) -] + if action_type == "machines": + return [ + machines.models.Machine.__name__, + machines.models.Interface.__name__ + ] -CHOICES_TYPE = ( - ("ip", _("IPv4")), - ("mac", _("MAC address")), -) + if action_type == "subscriptions": + return all_classes(cotisations.models) + + if action_type == "whitelists": + return [users.models.Whitelist.__name__] + + if action_type == "ban": + return [users.models.Ban.__name__] + + if action_type == "topology": + return all_classes(topologie.models) + + # "all" is a special case, just return None + return None class ActionsSearchForm(Form): diff --git a/logs/models.py b/logs/models.py index f1db0c08..5ef9b0ce 100644 --- a/logs/models.py +++ b/logs/models.py @@ -36,6 +36,7 @@ from users.models import Club from topologie.models import Room from topologie.models import Port +from .forms import classes_for_action_type class ActionsSearch: def get(self, params): @@ -46,7 +47,7 @@ class ActionsSearch: user = params.get("u", None) start = params.get("s", None) end = params.get("e", None) - actions_type = params.get("t", None) + action_types = params.get("t", None) query = Q() @@ -59,8 +60,9 @@ class ActionsSearch: if end: query &= Q(date_created__leq=end) - if actions_type: - query &= Q(version__content_type__in=actions_type) + action_classes = self.classes_for_action_types(action_types) + if action_classes: + query &= Q(object__classname=action_classes) return ( Revision.objects.all() @@ -69,6 +71,22 @@ class ActionsSearch: .prefetch_related("version_set__object") ) + def classes_for_action_types(self, action_types): + if action_types is None: + return None + + classes = [] + for action_type in action_types: + c = classes_for_action_type(action_type) + + # Selecting "all" removes the filter + if c is None: + return None + + classes += c + + return classes + class MachineHistorySearchEvent: def __init__(self, user, machine, interface, start=None, end=None):