Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

forms.py 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# 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.

Maël Kervella's avatar
Maël Kervella committed
23 24
"""The forms used by the search app"""

25 26
from __future__ import unicode_literals

27 28
from django import forms
from django.forms import Form
29
from django.utils.translation import ugettext_lazy as _
30
from re2o.utils import get_input_formats_help_text
31 32

CHOICES_USER = (
33 34 35
    ('0', _("Active")),
    ('1', _("Disabled")),
    ('2', _("Archived")),
36 37 38
)

CHOICES_AFF = (
39 40 41 42 43 44 45 46
    ('0', _("Users")),
    ('1', _("Machines")),
    ('2', _("Invoices")),
    ('3', _("Bans")),
    ('4', _("Whitelists")),
    ('5', _("Rooms")),
    ('6', _("Ports")),
    ('7', _("Switches")),
47 48
)

49

50
def initial_choices(choice_set):
Maël Kervella's avatar
Maël Kervella committed
51 52
    """Return the choices that should be activated by default for a
    given set of choices"""
53
    return [i[0] for i in choice_set]
54 55 56


class SearchForm(Form):
Maël Kervella's avatar
Maël Kervella committed
57
    """The form for a simple search"""
58
    q = forms.CharField(
59
        label=_("Search"),
60
        help_text=(
61 62 63
            _("Use « » and «,» to specify distinct words, «\"query\"» for"
              " an exact search and «\\» to escape a character.")
            ),
64 65
        max_length=100
    )
66 67 68


class SearchFormPlus(Form):
Maël Kervella's avatar
Maël Kervella committed
69
    """The form for an advanced search (with filters)"""
70
    q = forms.CharField(
71
        label=_("Search"),
72
        help_text=(
73 74
            _("Use « » and «,» to specify distinct words, «\"query\"» for"
              " an exact search and «\\» to escape a character.")
75
        ),
76
        max_length=100,
77 78 79
        required=False
    )
    u = forms.MultipleChoiceField(
80
        label=_("Users filter"),
81 82 83 84 85 86
        required=False,
        widget=forms.CheckboxSelectMultiple,
        choices=CHOICES_USER,
        initial=initial_choices(CHOICES_USER)
    )
    a = forms.MultipleChoiceField(
87
        label=_("Display filter"),
88 89 90 91 92 93 94
        required=False,
        widget=forms.CheckboxSelectMultiple,
        choices=CHOICES_AFF,
        initial=initial_choices(CHOICES_AFF)
    )
    s = forms.DateField(
        required=False,
95
        label=_("Start date"),
96 97 98
    )
    e = forms.DateField(
        required=False,
99
        label=_("End date")
100
    )
101 102 103 104 105 106 107 108 109

    def __init__(self, *args, **kwargs):
        super(SearchFormPlus, self).__init__(*args, **kwargs)
        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
        )
110