forms.py 9.68 KB
Newer Older
1
from flask_wtf import FlaskForm
2
from wtforms import StringField, PasswordField, BooleanField, DateField, HiddenField, IntegerField, SelectField, FileField, DateTimeField, TextAreaField, Field, widgets
Robin Sonnabend's avatar
Robin Sonnabend committed
3
from wtforms.validators import InputRequired, Optional
4

5
6
import ipaddress

7
8
from models.database import TodoState
from validators import CheckTodoDateByState
Robin Sonnabend's avatar
Robin Sonnabend committed
9
from calendarpush import Client as CalendarClient
10
from shared import current_user
11

Robin Sonnabend's avatar
Robin Sonnabend committed
12
13
import config

Robin Sonnabend's avatar
Robin Sonnabend committed
14
def get_protocoltype_choices(protocoltypes, add_all=True):
15
16
17
18
19
    choices = [
        (protocoltype.id, protocoltype.short_name)
        for protocoltype
        in sorted(protocoltypes, key=lambda t: t.short_name)
    ]
Robin Sonnabend's avatar
Robin Sonnabend committed
20
21
22
23
    if add_all:
        choices.insert(0, (-1, "Alle"))
    return choices

24
25
def get_todostate_choices():
    return [
26
        (state, state.get_name())
27
28
29
        for state in TodoState
    ]

Robin Sonnabend's avatar
Robin Sonnabend committed
30
def get_calendar_choices(protocoltype=None):
31
    calendars = CalendarClient().get_calendars()
32
33
34
35
    choices = []
    if calendars is not None:
        calendars = sorted(calendars)
        choices = list(zip(calendars, calendars))
Robin Sonnabend's avatar
Robin Sonnabend committed
36
37
38
39
40
    else:
        if (protocoltype is not None
        and protocoltype.calendar is not None
        and protocoltype.calendar != ""):
            choices.append((protocoltype.calendar, protocoltype.calendar))
41
42
43
44
    choices.insert(0, ("", "Kein Kalender"))
    return choices

def get_printer_choices():
45
46
47
    choices = []
    if config.PRINTING_PRINTERS is not None:
        choices = list(zip(config.PRINTING_PRINTERS, config.PRINTING_PRINTERS))
48
49
50
51
52
    choices.insert(0, ("", "Nicht drucken"))
    return choices

def get_group_choices():
    user = current_user()
Robin Sonnabend's avatar
Robin Sonnabend committed
53
54
    groups = sorted(user.groups)
    choices = list(zip(groups, groups))
55
56
    choices.insert(0, ("", "Keine Gruppe"))
    return choices
Robin Sonnabend's avatar
Robin Sonnabend committed
57

58
59
60
61
62
63
def coerce_todostate(key):
    if isinstance(key, str):
        class_part, key_part = key.split(".")
        key = TodoState[key_part]
    return key

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class IPNetworkField(Field):
    widget = widgets.TextInput()

    def __init__(self, label=None, validators=None, **kwargs):
        super().__init__(label, validators, **kwargs)

    def _value(self):
        if self.raw_data:
            return " ".join(self.raw_data)
        else:
            return self.data and str(self.data) or ""

    def process_formdata(self, valuelist):
        if valuelist:
            data_str = valuelist[0]
            result_parts = []
            try:
                for part in data_str.split(","):
                    part = part.strip()
                    if len(part) > 0:
                        network = ipaddress.ip_network(part)
                        result_parts.append(network)
            except ValueError as exc:
                print(exc)
                self.data = None
                raise ValueError(self.gettext("Not a valid IP Network: {}".format(str(exc))))
            self.data = ",".join(map(str, result_parts))

92
class LoginForm(FlaskForm):
Robin Sonnabend's avatar
Robin Sonnabend committed
93
94
95
96
97
98
99
    username = StringField("Benutzer", validators=[InputRequired("Bitte gib deinen Benutzernamen ein.")])
    password = PasswordField("Passwort", validators=[InputRequired("Bitte gib dein Passwort ein.")])

class ProtocolTypeForm(FlaskForm):
    name = StringField("Name", validators=[InputRequired("Du musst einen Namen angeben.")])
    short_name = StringField("Abkürzung", validators=[InputRequired("Du musst eine Abkürzung angebene.")])
    organization = StringField("Organisation", validators=[InputRequired("Du musst eine zugehörige Organisation angeben.")])
Robin Sonnabend's avatar
Robin Sonnabend committed
100
    usual_time = DateTimeField("Üblicher Beginn", validators=[InputRequired("Bitte gib die Zeit an, zu der die Sitzung beginnt.")], format="%H:%M")
Robin Sonnabend's avatar
Robin Sonnabend committed
101
    is_public = BooleanField("Öffentlich sichtbar")
102
    modify_group = SelectField("Bearbeitungsgruppe", choices=[])
103
104
    private_group = SelectField("Interne Gruppe", choices=[])
    public_group = SelectField("Öffentliche Gruppe", choices=[])
Robin Sonnabend's avatar
Robin Sonnabend committed
105
106
    private_mail = StringField("Interner Verteiler")
    public_mail = StringField("Öffentlicher Verteiler")
Robin Sonnabend's avatar
Robin Sonnabend committed
107
108
109
    wiki_category = StringField("Wiki-Kategorie")
    use_wiki = BooleanField("Wiki benutzen")
    wiki_only_public = BooleanField("Wiki ist öffentlich")
110
    printer = SelectField("Drucker", choices=[])
111
    calendar = SelectField("Kalender", choices=[])
112
113
    restrict_networks = BooleanField("Netzwerke einschränken")
    allowed_networks = IPNetworkField("Erlaubte Netzwerke")
114
115

    def __init__(self, **kwargs):
116
        super().__init__(**kwargs)
Robin Sonnabend's avatar
Robin Sonnabend committed
117
118
        protocoltype = kwargs["obj"] if "obj" in kwargs else None
        self.calendar.choices = get_calendar_choices(protocoltype=protocoltype)
119
120
        self.printer.choices = get_printer_choices()
        group_choices = get_group_choices()
121
        self.modify_group.choices = group_choices
122
123
        self.private_group.choices = group_choices
        self.public_group.choices = group_choices
Robin Sonnabend's avatar
Robin Sonnabend committed
124
125
126
127

class DefaultTopForm(FlaskForm):
    name = StringField("Name", validators=[InputRequired("Du musst einen Namen angeben.")])
    number = IntegerField("Nummer", validators=[InputRequired("Du musst eine Nummer angeben.")])
128
129
130
131
132

class MeetingReminderForm(FlaskForm):
    days_before = IntegerField("Tage vor Sitzung", validators=[InputRequired("Du musst eine Dauer angeben.")])
    send_public = BooleanField("Öffentlich einladen")
    send_private = BooleanField("Intern einladen")
Robin Sonnabend's avatar
Robin Sonnabend committed
133
    additional_text = TextAreaField("Zusätzlicher Mailinhalt")
Robin Sonnabend's avatar
Robin Sonnabend committed
134
135
136
137
138
139
140

class NewProtocolForm(FlaskForm):
    protocoltype = SelectField("Typ", choices=[], coerce=int)
    date = DateField("Datum", validators=[InputRequired("Du musst ein Datum angeben.")], format="%d.%m.%Y")

    def __init__(self, protocoltypes, **kwargs):
        super().__init__(**kwargs)
Robin Sonnabend's avatar
Robin Sonnabend committed
141
        self.protocoltype.choices = get_protocoltype_choices(protocoltypes, add_all=False)
142
143

class DocumentUploadForm(FlaskForm):
Robin Sonnabend's avatar
Robin Sonnabend committed
144
    document = FileField("Datei")
145
    private = BooleanField("Intern")
Robin Sonnabend's avatar
Robin Sonnabend committed
146
147
148
149
150
151
152
153
154
155

class KnownProtocolSourceUploadForm(FlaskForm):
    source = FileField("Quellcode")

class NewProtocolSourceUploadForm(FlaskForm):
    source = FileField("Quellcode")
    protocoltype = SelectField("Typ", choices=[], coerce=int)

    def __init__(self, protocoltypes, **kwargs):
        super().__init__(**kwargs)
Robin Sonnabend's avatar
Robin Sonnabend committed
156
        self.protocoltype.choices = get_protocoltype_choices(protocoltypes, add_all=False)
Robin Sonnabend's avatar
Robin Sonnabend committed
157

158
159
160
161
162
163
164
class NewProtocolFileUploadForm(FlaskForm):
    file = FileField("Datei")
    protocoltype = SelectField("Typ", choices=[], coerce=int)
    private = BooleanField("Intern")

    def __init__(self, protocoltypes, **kwargs):
        super().__init__(**kwargs)
Robin Sonnabend's avatar
Robin Sonnabend committed
165
        self.protocoltype.choices = get_protocoltype_choices(protocoltypes, add_all=False)
166

Robin Sonnabend's avatar
Robin Sonnabend committed
167
168
class ProtocolForm(FlaskForm):
    date = DateField("Datum", validators=[InputRequired("Bitte gib das Datum des Protkolls an.")], format="%d.%m.%Y")
Robin Sonnabend's avatar
Robin Sonnabend committed
169
170
    start_time = DateTimeField("Beginn", format="%H:%M", validators=[Optional()])
    end_time = DateTimeField("Ende", format="%H:%M", validators=[Optional()])
Robin Sonnabend's avatar
Robin Sonnabend committed
171
    location = StringField("Ort")
172
    author = StringField("Protokoll")
Robin Sonnabend's avatar
Robin Sonnabend committed
173
174
    participants = StringField("Anwesende")
    done = BooleanField("Fertig")
175
    public = BooleanField("Veröffentlicht")
Robin Sonnabend's avatar
Robin Sonnabend committed
176

Robin Sonnabend's avatar
Robin Sonnabend committed
177
178
179
class TopForm(FlaskForm):
    name = StringField("TOP", validators=[InputRequired("Du musst den Namen des TOPs angeben.")])
    number = IntegerField("Sortierung", validators=[InputRequired("Du musst eine Sortierung in der Reihenfolge angebene.")])
180
    description = TextAreaField("Beschreibung")
Robin Sonnabend's avatar
Robin Sonnabend committed
181

Robin Sonnabend's avatar
Robin Sonnabend committed
182
183
184
185
186
187
class SearchForm(FlaskForm):
    search = StringField("Suchbegriff")
    protocoltype = SelectField("Typ", choices=[], coerce=int)

    def __init__(self, protocoltypes, **kwargs):
        super().__init__(**kwargs)
Robin Sonnabend's avatar
Robin Sonnabend committed
188
189
190
191
192
193
        self.protocoltype.choices = get_protocoltype_choices(protocoltypes)

class NewTodoForm(FlaskForm):
    protocoltype_id = SelectField("Typ", choices=[], coerce=int)
    who = StringField("Person", validators=[InputRequired("Bitte gib an, wer das Todo erledigen soll.")])
    description = StringField("Aufgabe", validators=[InputRequired("Bitte gib an, was erledigt werden soll.")])
194
    state = SelectField("Status", choices=[], coerce=coerce_todostate, validators=[CheckTodoDateByState()])
195
    date = DateField("Datum", format="%d.%m.%Y", validators=[Optional()])
Robin Sonnabend's avatar
Robin Sonnabend committed
196
197
198
199
    
    def __init__(self, protocoltypes, **kwargs):
        super().__init__(**kwargs)
        self.protocoltype_id.choices = get_protocoltype_choices(protocoltypes, add_all=False)
200
        self.state.choices = get_todostate_choices()
Robin Sonnabend's avatar
Robin Sonnabend committed
201
202

class TodoForm(FlaskForm):
Robin Sonnabend's avatar
Robin Sonnabend committed
203
    who = StringField("Person")
Robin Sonnabend's avatar
Robin Sonnabend committed
204
    description = StringField("Aufgabe", validators=[InputRequired("Bitte gib an, was erledigt werden soll.")])
205
    state = SelectField("Status", choices=[], coerce=coerce_todostate, validators=[CheckTodoDateByState()])
206
207
    date = DateField("Datum", format="%d.%m.%Y", validators=[Optional()])

208
209
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
210
        self.state.choices = get_todostate_choices()
Robin Sonnabend's avatar
Robin Sonnabend committed
211
212
213
214

class TodoMailForm(FlaskForm):
    name = StringField("Name", validators=[InputRequired("Du musst den Namen angeben, der zugeordnet werden soll.")])
    mail = StringField("Mail", validators=[InputRequired("Du musst die Mailadresse angeben, die zugeordnet werden soll.")])
Robin Sonnabend's avatar
Robin Sonnabend committed
215
216
217
218
219
220
221
222

class MetaForm(FlaskForm):
    name = StringField("Name", validators=[InputRequired("Bitte gib den Namen der Metadaten an.")])
    value = StringField("Wert")

class DefaultMetaForm(FlaskForm):
    key = StringField("Key", validators=[InputRequired("Bitte gib den Protokoll-Syntax-Schlüssel der Metadaten an.")])
    name = StringField("Name", validators=[InputRequired("Bitte gib den Namen der Metadaten an.")])
223
224
225
226
227
228
229
230

class MergeTodosForm(FlaskForm):
    todo1 = IntegerField("todo 1", validators=[InputRequired()])
    todo2 = IntegerField("todo 2", validators=[InputRequired()])

    def __init__(self, todo=None):
        if todo is not None:
            self.todo1.data = todo.id