8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-24 12:23:11 +00:00

Make event logs filter more user friendly

This commit is contained in:
Jean-Romain Garnier 2020-04-24 16:17:20 +02:00 committed by Jean-Romain Garnier
parent c5b9715cd4
commit 82e74fc7a3
2 changed files with 68 additions and 14 deletions

View file

@ -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):

View file

@ -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):