Skip to content

Commit

Permalink
Remove superfluous error flashes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Mar 15, 2024
1 parent 4c29973 commit d3f0f57
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 64 deletions.
19 changes: 7 additions & 12 deletions sipa/blueprints/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
from sqlalchemy.exc import DatabaseError

from sipa.backends.exceptions import BackendError
from sipa.forms import flash_formerrors, LoginForm, AnonymousContactForm, \
OfficialContactForm, PasswordRequestResetForm, PasswordResetForm
from sipa.forms import (
LoginForm,
AnonymousContactForm,
OfficialContactForm,
PasswordRequestResetForm,
PasswordResetForm,
)
from sipa.mail import send_official_contact_mail, send_contact_mail
from sipa.backends.extension import backends
from sipa.model import pycroft
Expand Down Expand Up @@ -154,8 +159,6 @@ def login():
logger.info('Authentication successful',
extra={'tags': {'user': username}})
flash(gettext("Anmeldung erfolgreich!"), "success")
elif form.is_submitted():
flash_formerrors(form)

if current_user.is_authenticated:
# `url_redirect` would not be bad here because this would allow for URL
Expand Down Expand Up @@ -198,8 +201,6 @@ def request_password_reset():
"Falls du die Nachricht nicht erhälst, wende dich an den Support."), "success")

return redirect(url_for('.login'))
elif form.is_submitted():
flash_formerrors(form)

return render_template('generic_form.html', page_title=gettext("Passwort zurücksetzen"),
form_args={'form': form, 'cancel_to': url_for('.login')})
Expand All @@ -222,8 +223,6 @@ def reset_password(token):
flash(gettext("Dein Passwort wurde geändert."), "success")

return redirect(url_for('.login'))
elif form.is_submitted():
flash_formerrors(form)

return render_template('generic_form.html', page_title=gettext("Passwort zurücksetzen"),
form_args={'form': form, 'cancel_to': url_for('.login')})
Expand Down Expand Up @@ -330,8 +329,6 @@ def contact():
flash(gettext("Es gab einen Fehler beim Versenden der Nachricht."),
'error')
return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)

return render_template('anonymous_contact.html', form=form)

Expand All @@ -354,8 +351,6 @@ def contact_official():
flash(gettext("Es gab einen Fehler beim Versenden der Nachricht."),
'error')
return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)

return render_template(
'official_contact.html',
Expand Down
13 changes: 3 additions & 10 deletions sipa/blueprints/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from werkzeug.local import LocalProxy

from sipa.backends.extension import backends, _dorm_summary
from sipa.forms import flash_formerrors, RegisterIdentifyForm, RegisterRoomForm, RegisterFinishForm
from sipa.forms import RegisterIdentifyForm, RegisterRoomForm, RegisterFinishForm
from sipa.model.pycroft.api import PycroftApi, PycroftApiError
from sipa.model.pycroft.exc import PycroftBackendError
from sipa.utils import parse_date
Expand Down Expand Up @@ -167,9 +167,6 @@ def identify(reg_state: RegisterState):
except PycroftBackendError as e:
handle_backend_error(e)

elif form.is_submitted():
flash_formerrors(form)

return render_template('register/identify.html', title=gettext('Identifizierung'), form=form,
skip_verification=suggest_skip)

Expand All @@ -181,9 +178,7 @@ def room(reg_state: RegisterState):
if form.validate_on_submit():
reg_state.room_confirmed = 'wrong_room' not in request.form
return goto_step('data')
elif form.is_submitted():
flash_formerrors(form)
else:
elif not form.is_submitted():
form.building.data = reg_state.building
form.room.data = reg_state.room
form.move_in_date.data = reg_state.move_in_date
Expand Down Expand Up @@ -236,9 +231,7 @@ def data(reg_state: RegisterState):
except PycroftBackendError as e:
handle_backend_error(e)

elif form.is_submitted():
flash_formerrors(form)
else:
elif not form.is_submitted():
form.member_begin_date.data = max(reg_state.move_in_date, date.today()) \
if reg_state.move_in_date is not None else date.today()

Expand Down
43 changes: 13 additions & 30 deletions sipa/blueprints/usersuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
from flask_wtf import FlaskForm
from markupsafe import Markup

from sipa.forms import ContactForm, ChangeMACForm, ChangeMailForm, \
ChangePasswordForm, flash_formerrors, HostingForm, \
PaymentForm, ActivateNetworkAccessForm, TerminateMembershipForm, \
TerminateMembershipConfirmForm, ContinueMembershipForm
from sipa.forms import (
ContactForm,
ChangeMACForm,
ChangeMailForm,
ChangePasswordForm,
HostingForm,
PaymentForm,
ActivateNetworkAccessForm,
TerminateMembershipForm,
TerminateMembershipConfirmForm,
ContinueMembershipForm,
)
from sipa.mail import send_usersuite_contact_mail
from sipa.model.fancy_property import ActiveProperty
from sipa.utils import password_changeable, subscribe_to_status_page
Expand Down Expand Up @@ -109,7 +117,6 @@ def index():
months = payment_form.months.data
else:
months = payment_form.months.default
flash_formerrors(payment_form)

datasource = current_user.datasource
context = dict(rows=rows,
Expand Down Expand Up @@ -168,8 +175,6 @@ def contact():
.format(current_user.datasource.support_mail),
'error')
return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)

form.email.default = current_user.mail.raw_value

Expand Down Expand Up @@ -276,8 +281,6 @@ def change_password():
else:
flash(gettext("Passwort wurde geändert"), "success")
return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)

return render_template("generic_form.html", page_title=gettext("Passwort ändern"),
form_args={'form': form, 'reset_button': True, 'cancel_to': url_for('.index')})
Expand Down Expand Up @@ -309,9 +312,7 @@ def change_mail():
else:
flash(gettext("E-Mail-Adresse wurde geändert"), "success")
return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)
else:
elif not form.is_submitted():
form.email.data = current_user.mail.raw_value
form.forwarded.data = current_user.mail_forwarded.raw_value

Expand All @@ -338,8 +339,6 @@ def resend_confirm_mail():
flash(gettext('Versenden der Bestätigungs-E-Mail ist fehlgeschlagen!'), 'error')

return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)

form_args = {
'form': form,
Expand Down Expand Up @@ -384,9 +383,6 @@ def change_mac():

return redirect(url_for('.index'))

elif form.is_submitted():
flash_formerrors(form)

form.mac.default = current_user.mac.value

return render_template('usersuite/change_mac.html',
Expand Down Expand Up @@ -428,9 +424,6 @@ def activate_network_access():

return redirect(url_for('.index'))

elif form.is_submitted():
flash_formerrors(form)

return render_template('generic_form.html', page_title=gettext("Netzwerkanschluss aktivieren"),
form_args={'form': form, 'cancel_to': url_for('.index')})

Expand All @@ -457,8 +450,6 @@ def hosting(action=None):
flash(gettext("Deine Datenbank wurde erstellt."), 'success')
else:
current_user.userdb.change_password(form.password.data)
elif form.is_submitted():
flash_formerrors(form)

try:
user_has_db = current_user.userdb.has_db
Expand Down Expand Up @@ -495,8 +486,6 @@ def terminate_membership():

return redirect(url_for('.terminate_membership_confirm',
end_date=end_date))
elif form.is_submitted():
flash_formerrors(form)

form_args = {
'form': form,
Expand Down Expand Up @@ -553,8 +542,6 @@ def terminate_membership_confirm():
flash(gettext("Deine Mitgliedschaft wird zum angegebenen Datum beendet."), 'success')

return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)

form_args = {
'form': form,
Expand Down Expand Up @@ -595,8 +582,6 @@ def continue_membership():
flash(gettext("Deine Mitgliedschaft wird fortgesetzt."), 'success')

return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)

form_args = {
'form': form,
Expand Down Expand Up @@ -631,8 +616,6 @@ def reset_wifi_password():
flash(Markup("{}:<pre>{}</pre>".format(gettext("Es wurde ein neues WLAN Passwort generiert"), new_password)), 'success')

return redirect(url_for('.index'))
elif form.is_submitted():
flash_formerrors(form)

form_args = {
'form': form,
Expand Down
12 changes: 0 additions & 12 deletions sipa/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from operator import itemgetter

from flask_babel import gettext, lazy_gettext
from flask import flash
from flask_login import current_user
from wtforms_widgets.base_form import BaseForm as FlaskForm
from werkzeug.local import LocalProxy
Expand Down Expand Up @@ -618,14 +617,3 @@ class RegisterFinishForm(FlaskForm):
"Bitte bestätige deine Zustimmung zu der Datenschutzbelehrung."))
]
)


def flash_formerrors(form):
"""If a form is submitted but could not be validated, the routing passes
the form and this method returns all form errors (form.errors)
as flash messages.
"""
for _field, errors in list(form.errors.items()):
for e in errors:
flash(e, "error")

1 change: 1 addition & 0 deletions sipa/templates/macros/forms.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
{% endif %}{%- endfor %}

{# Here can things like notes or alerts be placed #}
{# TODO print all nonspecific errors (not belonging to a specific field) at some good location #}
{{ caller() }}

<div class="form-group">
Expand Down

0 comments on commit d3f0f57

Please sign in to comment.