forms.py 3.33 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
    ('3', _("Not Yet Active")),
37 38 39
)

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

50

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


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


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

    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
        )
111