Commit 77d415de authored by Robin Sonnabend's avatar Robin Sonnabend
Browse files

Improve views/*.py code quality

parent 60ff6a9d
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, HiddenField, IntegerField, SelectField, FileField, DateTimeField, TextAreaField, Field, widgets, FormField
from wtforms import (
StringField, PasswordField, BooleanField, IntegerField, SelectField,
FileField, DateTimeField, TextAreaField, Field, FormField, widgets)
from wtforms.fields.html5 import DateField
from wtforms.validators import InputRequired, Optional
......@@ -11,6 +13,7 @@ from shared import current_user
import config
def get_protocoltype_choices(protocoltypes, add_all=True):
choices = [
(protocoltype.id, protocoltype.short_name)
......@@ -21,6 +24,7 @@ def get_protocoltype_choices(protocoltypes, add_all=True):
choices.insert(0, (-1, "Alle Typen"))
return choices
def get_category_choices(categories, add_all=True):
choices = [
(category.id, category.name)
......@@ -31,12 +35,14 @@ def get_category_choices(categories, add_all=True):
choices.insert(0, (-1, "Alle Kategorien"))
return choices
def get_todostate_choices():
return [
(state, state.get_name())
for state in TodoState
]
def get_calendar_choices(protocoltype=None):
from calendarpush import Client as CalendarClient
calendars = CalendarClient().get_calendars()
......@@ -46,19 +52,21 @@ def get_calendar_choices(protocoltype=None):
choices = list(zip(calendars, calendars))
else:
if (protocoltype is not None
and protocoltype.calendar is not None
and protocoltype.calendar != ""):
and protocoltype.calendar is not None
and protocoltype.calendar != ""):
choices.append((protocoltype.calendar, protocoltype.calendar))
choices.insert(0, ("", "Kein Kalender"))
return choices
def get_printer_choices():
choices = []
if config.PRINTING_PRINTERS is not None:
choices = list(zip(config.PRINTING_PRINTERS, config.PRINTING_PRINTERS))
choices.insert(0, ("", "Nicht drucken"))
return choices
def get_latex_template_choices():
choices = []
_latex_templates = getattr(config, "LATEX_TEMPLATES", None)
......@@ -71,6 +79,7 @@ def get_latex_template_choices():
choices.insert(0, ("", "Standardvorlage"))
return choices
def get_group_choices():
user = current_user()
groups = sorted(user.groups)
......@@ -78,12 +87,14 @@ def get_group_choices():
choices.insert(0, ("", "Keine Gruppe"))
return choices
def coerce_todostate(key):
if isinstance(key, str):
class_part, key_part = key.split(".")
key = TodoState[key_part]
return key
class IPNetworkField(Field):
widget = widgets.TextInput()
......@@ -109,30 +120,53 @@ class IPNetworkField(Field):
except ValueError as exc:
print(exc)
self.data = None
raise ValueError(self.gettext("Not a valid IP Network: {}".format(str(exc))))
raise ValueError(
self.gettext("Not a valid IP Network: {}".format(
str(exc))))
self.data = ",".join(map(str, result_parts))
class FocusedStringField(StringField):
def __call__(self, **kwargs):
kwargs['autofocus'] = True
return super().__call__(**kwargs)
class LoginForm(FlaskForm):
username = FocusedStringField("Benutzer", validators=[InputRequired("Bitte gib deinen Benutzernamen ein.")])
password = PasswordField("Passwort", validators=[InputRequired("Bitte gib dein Passwort ein.")])
username = FocusedStringField(
"Benutzer",
validators=[InputRequired("Bitte gib deinen Benutzernamen ein.")])
password = PasswordField(
"Passwort",
validators=[InputRequired("Bitte gib dein Passwort ein.")])
permanent = BooleanField("Eingeloggt bleiben?")
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.")])
usual_time = DateTimeField("Üblicher Beginn", validators=[InputRequired("Bitte gib die Zeit an, zu der die Sitzung beginnt.")], format="%H:%M")
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.")
])
usual_time = DateTimeField(
"Üblicher Beginn",
validators=[
InputRequired("Bitte gib die Zeit an, zu der die Sitzung beginnt.")
],
format="%H:%M")
is_public = BooleanField("Öffentlich sichtbar")
publish_group = SelectField("Verwaltungsgruppe", choices=[])
modify_group = SelectField("Bearbeitungsgruppe", choices=[])
private_group = SelectField("Interne Gruppe", choices=[])
public_group = SelectField("Öffentliche Gruppe", choices=[])
non_reproducible_pad_links = BooleanField("nicht nachvollziehbare Etherpad-Links")
non_reproducible_pad_links = BooleanField(
"nicht nachvollziehbare Etherpad-Links")
private_mail = StringField("Interner Verteiler")
public_mail = StringField("Öffentlicher Verteiler")
wiki_category = StringField("Wiki-Kategorie")
......@@ -156,44 +190,65 @@ class ProtocolTypeForm(FlaskForm):
self.private_group.choices = group_choices
self.public_group.choices = group_choices
class DefaultTopForm(FlaskForm):
name = StringField("Name", validators=[InputRequired("Du musst einen Namen angeben.")])
number = IntegerField("Nummer", validators=[InputRequired("Du musst eine Nummer angeben.")])
name = StringField(
"Name",
validators=[InputRequired("Du musst einen Namen angeben.")])
number = IntegerField(
"Priorität",
validators=[InputRequired("Du musst eine Priorität angeben.")])
description = TextAreaField("Standardinhalt")
class MeetingReminderForm(FlaskForm):
days_before = IntegerField("Tage vor Sitzung", validators=[InputRequired("Du musst eine Dauer angeben.")])
days_before = IntegerField(
"Tage vor Sitzung",
validators=[InputRequired("Du musst eine Dauer angeben.")])
send_public = BooleanField("Öffentlich einladen")
send_private = BooleanField("Intern einladen")
additional_text = TextAreaField("Zusätzlicher Mailinhalt")
class NewProtocolForm(FlaskForm):
protocoltype_id = SelectField("Typ", choices=[], coerce=int)
date = DateField("Datum", validators=[InputRequired("Du musst ein Datum angeben.")])
start_time = DateTimeField("Uhrzeit (HH:MM, optional)", validators=[Optional()], format="%H:%M")
date = DateField(
"Datum",
validators=[InputRequired("Du musst ein Datum angeben.")])
start_time = DateTimeField(
"Uhrzeit (HH:MM, optional)",
validators=[Optional()],
format="%H:%M")
def __init__(self, protocoltypes, **kwargs):
super().__init__(**kwargs)
self.protocoltype_id.choices = get_protocoltype_choices(protocoltypes, add_all=False)
self.protocoltype_id.choices = get_protocoltype_choices(
protocoltypes, add_all=False)
class DocumentEditForm(FlaskForm):
name = StringField("Dateiname")
is_private = BooleanField("Intern")
class DocumentUploadForm(FlaskForm):
document = FileField("Datei")
is_private = BooleanField("Intern")
class KnownProtocolSourceUploadForm(FlaskForm):
source = FileField("Quellcode")
class NewProtocolSourceUploadForm(FlaskForm):
source = FileField("Quellcode")
protocoltype_id = SelectField("Typ", choices=[], coerce=int)
def __init__(self, protocoltypes, **kwargs):
super().__init__(**kwargs)
self.protocoltype_id.choices = get_protocoltype_choices(protocoltypes, add_all=False)
self.protocoltype_id.choices = get_protocoltype_choices(
protocoltypes, add_all=False)
class NewProtocolFileUploadForm(FlaskForm):
file = FileField("Datei")
......@@ -202,31 +257,46 @@ class NewProtocolFileUploadForm(FlaskForm):
def __init__(self, protocoltypes, **kwargs):
super().__init__(**kwargs)
self.protocoltype_id.choices = get_protocoltype_choices(protocoltypes, add_all=False)
self.protocoltype_id.choices = get_protocoltype_choices(
protocoltypes, add_all=False)
def generate_protocol_form(protocol):
class ProtocolMetasForm(FlaskForm):
pass
for meta in protocol.metas:
setattr(ProtocolMetasForm, meta.name, StringField(meta.name))
class ProtocolForm(FlaskForm):
date = DateField("Datum", validators=[InputRequired("Bitte gib das Datum des Protkolls an.")])
start_time = DateTimeField("Beginn (%H:%M)", format="%H:%M", validators=[Optional()])
end_time = DateTimeField("Ende (%H:%M)", format="%H:%M", validators=[Optional()])
date = DateField(
"Datum",
validators=[
InputRequired("Bitte gib das Datum des Protkolls an.")
])
start_time = DateTimeField(
"Beginn (%H:%M)", format="%H:%M", validators=[Optional()])
end_time = DateTimeField(
"Ende (%H:%M)", format="%H:%M", validators=[Optional()])
metas = FormField(ProtocolMetasForm)
done = BooleanField("Fertig")
public = BooleanField("Veröffentlicht")
return ProtocolForm
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.")])
name = StringField(
"TOP",
validators=[InputRequired("Du musst den Namen des TOPs angeben.")])
number = IntegerField(
"Priorität",
validators=[InputRequired("Du musst eine Priorität angeben.")])
description = TextAreaField("Beschreibung")
class LocalTopForm(FlaskForm):
description = TextAreaField("Beschreibung")
class SearchForm(FlaskForm):
search = StringField("Suchbegriff")
protocoltype_id = SelectField("Typ", choices=[], coerce=int)
......@@ -235,6 +305,7 @@ class SearchForm(FlaskForm):
super().__init__(**kwargs)
self.protocoltype_id.choices = get_protocoltype_choices(protocoltypes)
class DecisionSearchForm(SearchForm):
decisioncategory_id = SelectField("Kategorie", choices=[], coerce=int)
......@@ -242,52 +313,104 @@ class DecisionSearchForm(SearchForm):
super().__init__(protocoltypes=protocoltypes, **kwargs)
self.decisioncategory_id.choices = get_category_choices(categories)
class ProtocolSearchForm(SearchForm):
state_open = SelectField("Offen", choices=[(-1, "Alle"), (0, "Geplant"), (1, "Fertig")], coerce=int)
state_open = SelectField(
"Offen",
choices=[(-1, "Alle"), (0, "Geplant"), (1, "Fertig")],
coerce=int)
class TodoSearchForm(SearchForm):
state_open = SelectField("Offen", choices=[(-1, "Alle"), (0, "Offen"), (1, "Erledigt")], coerce=int)
state_open = SelectField(
"Offen",
choices=[(-1, "Alle"), (0, "Offen"), (1, "Erledigt")],
coerce=int)
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.")])
state = SelectField("Status", choices=[], coerce=coerce_todostate, validators=[CheckTodoDateByState()])
date = DateField("Datum)", validators=[Optional()])
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.")
])
state = SelectField(
"Status",
choices=[],
coerce=coerce_todostate,
validators=[CheckTodoDateByState()])
date = DateField("Datum", validators=[Optional()])
def __init__(self, protocoltypes, **kwargs):
super().__init__(**kwargs)
self.protocoltype_id.choices = get_protocoltype_choices(protocoltypes, add_all=False)
self.protocoltype_id.choices = get_protocoltype_choices(
protocoltypes, add_all=False)
self.state.choices = get_todostate_choices()
class TodoForm(FlaskForm):
who = StringField("Person")
description = StringField("Aufgabe", validators=[InputRequired("Bitte gib an, was erledigt werden soll.")])
state = SelectField("Status", choices=[], coerce=coerce_todostate, validators=[CheckTodoDateByState()])
description = StringField(
"Aufgabe",
validators=[InputRequired("Bitte gib an, was erledigt werden soll.")])
state = SelectField(
"Status",
choices=[],
coerce=coerce_todostate,
validators=[CheckTodoDateByState()])
date = DateField("Datum", validators=[Optional()])
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.state.choices = get_todostate_choices()
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.")])
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.")])
class MetaForm(FlaskForm):
name = StringField("Name", validators=[InputRequired("Bitte gib den Namen der Metadaten an.")])
name = StringField(
"Name",
validators=[InputRequired("Bitte gib den Namen der Metadaten an.")])
value = StringField("Wert")
internal = BooleanField("Intern")
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.")])
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.")])
value = StringField("Standardwert")
internal = BooleanField("Intern")
prior = BooleanField("Planungsrelevant")
class DecisionCategoryForm(FlaskForm):
name = StringField("Name", validators=[InputRequired("Bitte gib den Namen der Kategorie an.")])
name = StringField(
"Name",
validators=[InputRequired("Bitte gib den Namen der Kategorie an.")])
class MergeTodosForm(FlaskForm):
todo1 = IntegerField("todo 1", validators=[InputRequired()])
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment