forms.py 4.99 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
nanoy's avatar
nanoy committed
2
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
jr-garnier's avatar
jr-garnier committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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.

22
"""The forms used by the machine search view"""
jr-garnier's avatar
jr-garnier committed
23
24
25
26
27

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
chirac's avatar
chirac committed
28
from re2o.mixins import AutocompleteModelMixin
jr-garnier's avatar
jr-garnier committed
29

jr-garnier's avatar
jr-garnier committed
30
31
32
import inspect

# Import all models in which there are classes to be filtered on
33
import cotisations.models
jr-garnier's avatar
jr-garnier committed
34
35
36
37
38
39
import machines.models
import preferences.models
import topologie.models
import users.models


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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):
57
58
59
60
61
62
63
64
65
    """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.
    """
jr-garnier's avatar
jr-garnier committed
66
67
68
69
    classes = []

    for name, obj in inspect.getmembers(module):
        if inspect.isclass(obj):
jr-garnier's avatar
jr-garnier committed
70
            classes.append(name)
jr-garnier's avatar
jr-garnier committed
71
72
73
74

    return classes


75
def classes_for_action_type(action_type):
76
77
78
79
80
81
82
83
84
85
    """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.
    """
86
87
88
89
90
91
92
    if action_type == "users":
        return [
            users.models.User.__name__,
            users.models.Adherent.__name__,
            users.models.Club.__name__,
            users.models.EMailAddress.__name__
        ]
jr-garnier's avatar
jr-garnier committed
93

94
95
96
97
98
    if action_type == "machines":
        return [
            machines.models.Machine.__name__,
            machines.models.Interface.__name__
        ]
jr-garnier's avatar
jr-garnier committed
99

100
101
102
103
104
105
    if action_type == "subscriptions":
        return all_classes(cotisations.models)

    if action_type == "whitelists":
        return [users.models.Whitelist.__name__]

jr-garnier's avatar
jr-garnier committed
106
    if action_type == "bans":
107
108
109
110
111
112
113
        return [users.models.Ban.__name__]

    if action_type == "topology":
        return all_classes(topologie.models)

    # "all" is a special case, just return None
    return None
jr-garnier's avatar
jr-garnier committed
114
115


jr-garnier's avatar
jr-garnier committed
116
class ActionsSearchForm(Form):
117
    """Form used to do an advanced search through the logs."""
chirac's avatar
chirac committed
118
    user = forms.ModelChoiceField(
jr-garnier's avatar
jr-garnier committed
119
        label=_("Performed by"),
120
        queryset=users.models.User.objects.all(),
jr-garnier's avatar
jr-garnier committed
121
        required=False,
chirac's avatar
chirac committed
122
        widget=AutocompleteModelMixin(url="/users/user-autocomplete"),
jr-garnier's avatar
jr-garnier committed
123
    )
chirac's avatar
chirac committed
124
    action_type = forms.MultipleChoiceField(
jr-garnier's avatar
jr-garnier committed
125
126
127
128
129
130
        label=_("Action type"),
        required=False,
        widget=forms.CheckboxSelectMultiple,
        choices=CHOICES_ACTION_TYPE,
        initial=[i[0] for i in CHOICES_ACTION_TYPE],
    )
chirac's avatar
chirac committed
131
132
    start_date = forms.DateField(required=False, label=_("Start date"))
    end_date = forms.DateField(required=False, label=_("End date"))
jr-garnier's avatar
jr-garnier committed
133

jr-garnier's avatar
jr-garnier committed
134
    def __init__(self, *args, **kwargs):
jr-garnier's avatar
jr-garnier committed
135
        super(ActionsSearchForm, self).__init__(*args, **kwargs)
chirac's avatar
chirac committed
136
137
        self.fields["start_date"].help_text = get_input_formats_help_text(
            self.fields["start_date"].input_formats
jr-garnier's avatar
jr-garnier committed
138
        )
chirac's avatar
chirac committed
139
140
        self.fields["end_date"].help_text = get_input_formats_help_text(
            self.fields["end_date"].input_formats
jr-garnier's avatar
jr-garnier committed
141
142
143
144
        )


class MachineHistorySearchForm(Form):
145
    """Form used to do a search through the machine histories."""
jr-garnier's avatar
jr-garnier committed
146
147
148
149
150
151
    q = forms.CharField(
        label=_("Search"),
        max_length=100,
    )
    t = forms.CharField(
        label=_("Search type"),
jr-garnier's avatar
jr-garnier committed
152
        widget=forms.Select(choices=CHOICES_TYPE)
jr-garnier's avatar
jr-garnier committed
153
154
155
156
157
    )
    s = forms.DateField(required=False, label=_("Start date"))
    e = forms.DateField(required=False, label=_("End date"))

    def __init__(self, *args, **kwargs):
158
        super(MachineHistorySearchForm, self).__init__(*args, **kwargs)
jr-garnier's avatar
jr-garnier committed
159
160
161
162
163
164
        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
        )