8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-30 08:32:26 +00:00
re2o/logs/forms.py

157 lines
4.9 KiB
Python
Raw Normal View History

# -*- mode: python; coding: utf-8 -*-
2020-11-23 16:06:37 +00:00
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
2020-04-22 16:17:06 +00:00
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2020 Jean-Romain Garnier
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""The forms used by the machine search view"""
2020-04-22 16:17:06 +00:00
from django import forms
from django.forms import Form
from django.utils.translation import ugettext_lazy as _
from re2o.base import get_input_formats_help_text
from re2o.widgets import AutocompleteModelWidget
2020-04-22 16:17:06 +00:00
2020-04-24 13:37:05 +00:00
import inspect
# Import all models in which there are classes to be filtered on
import cotisations.models
2020-04-24 13:37:05 +00:00
import machines.models
import preferences.models
import topologie.models
import users.models
CHOICES_ACTION_TYPE = (
("users", _("Users")),
("machines", _("Machines")),
("subscriptions", _("Subscription")),
("whitelists", _("Whitelists")),
("bans", _("Bans")),
("topology", _("Topology")),
("all", _("All")),
)
2020-12-31 19:12:36 +00:00
CHOICES_TYPE = (("ip", _("IPv4")), ("mac", _("MAC address")))
def all_classes(module):
"""Get the list of all class names of the module.
Args:
module: the module in which to retrieve classes.
Returns:
A list containing the names of all classes that are defined in
the module.
"""
2020-04-24 13:37:05 +00:00
classes = []
for name, obj in inspect.getmembers(module):
if inspect.isclass(obj):
2020-04-24 13:56:02 +00:00
classes.append(name)
2020-04-24 13:37:05 +00:00
return classes
def classes_for_action_type(action_type):
"""Get the list of class names to be displayed for a given action type
filter.
Args:
action_type: the string used to filter the class names.
Returns:
A list containing the class names corresponding to the action type
filter.
"""
if action_type == "users":
return [
users.models.User.__name__,
users.models.Adherent.__name__,
users.models.Club.__name__,
2020-12-31 19:12:36 +00:00
users.models.EMailAddress.__name__,
]
2020-04-24 13:37:05 +00:00
if action_type == "machines":
2020-12-31 19:12:36 +00:00
return [machines.models.Machine.__name__, machines.models.Interface.__name__]
2020-04-24 13:37:05 +00:00
if action_type == "subscriptions":
return all_classes(cotisations.models)
if action_type == "whitelists":
return [users.models.Whitelist.__name__]
2020-04-24 14:34:36 +00:00
if action_type == "bans":
return [users.models.Ban.__name__]
if action_type == "topology":
return all_classes(topologie.models)
# "all" is a special case, just return None
return None
2020-04-22 16:17:06 +00:00
2020-04-24 13:37:05 +00:00
class ActionsSearchForm(Form):
"""Form used to do an advanced search through the logs."""
2020-12-31 19:12:36 +00:00
2020-12-30 17:49:52 +00:00
user = forms.ModelChoiceField(
2020-04-24 13:37:05 +00:00
label=_("Performed by"),
queryset=users.models.User.objects.all(),
2020-04-24 13:37:05 +00:00
required=False,
widget=AutocompleteModelWidget(url="/users/user-autocomplete"),
2020-04-24 13:37:05 +00:00
)
2020-12-30 17:49:52 +00:00
action_type = forms.MultipleChoiceField(
2020-04-24 13:37:05 +00:00
label=_("Action type"),
required=False,
widget=forms.CheckboxSelectMultiple,
choices=CHOICES_ACTION_TYPE,
initial=[i[0] for i in CHOICES_ACTION_TYPE],
)
2020-12-30 17:49:52 +00:00
start_date = forms.DateField(required=False, label=_("Start date"))
end_date = forms.DateField(required=False, label=_("End date"))
2020-04-22 16:17:06 +00:00
2020-04-24 13:37:05 +00:00
def __init__(self, *args, **kwargs):
2020-04-24 13:56:02 +00:00
super(ActionsSearchForm, self).__init__(*args, **kwargs)
2020-12-30 17:49:52 +00:00
self.fields["start_date"].help_text = get_input_formats_help_text(
self.fields["start_date"].input_formats
2020-04-24 13:37:05 +00:00
)
2020-12-30 17:49:52 +00:00
self.fields["end_date"].help_text = get_input_formats_help_text(
self.fields["end_date"].input_formats
2020-04-24 13:37:05 +00:00
)
class MachineHistorySearchForm(Form):
"""Form used to do a search through the machine histories."""
2020-12-31 19:12:36 +00:00
q = forms.CharField(label=_("Search"), max_length=100)
2020-04-22 16:17:06 +00:00
t = forms.CharField(
2020-12-31 19:12:36 +00:00
label=_("Search type"), widget=forms.Select(choices=CHOICES_TYPE)
2020-04-22 16:17:06 +00:00
)
s = forms.DateField(required=False, label=_("Start date"))
e = forms.DateField(required=False, label=_("End date"))
def __init__(self, *args, **kwargs):
super(MachineHistorySearchForm, self).__init__(*args, **kwargs)
2020-04-22 16:17:06 +00:00
self.fields["s"].help_text = get_input_formats_help_text(
self.fields["s"].input_formats
)
self.fields["e"].help_text = get_input_formats_help_text(
self.fields["e"].input_formats
)