From b4e04a201b8f2ef3368ca807933d424b3ca79199 Mon Sep 17 00:00:00 2001
From: sistason <c3infra@sistason.de>
Date: Tue, 24 May 2022 14:30:04 +0000
Subject: [PATCH] Use flash with categories, replacing error-strings

---
 uffd/invite/views.py                      | 17 +++++++++-----
 uffd/signup/templates/signup/confirm.html |  3 ---
 uffd/signup/templates/signup/start.html   |  5 ----
 uffd/signup/views.py                      | 28 +++++++++++++++--------
 uffd/templates/base.html                  |  4 ++--
 uffd/templates/base_narrow.html           |  4 ++--
 6 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/uffd/invite/views.py b/uffd/invite/views.py
index 820d4ff9..7e8074c7 100644
--- a/uffd/invite/views.py
+++ b/uffd/invite/views.py
@@ -172,14 +172,17 @@ def signup_submit(invite_id, token):
 	if not invite or not secrets.compare_digest(invite.token, token):
 		abort(404)
 	if request.form['password1'] != request.form['password2']:
-		return render_template('signup/start.html', error=_('Passwords do not match'))
+		flash(_('Passwords do not match'), 'error')
+		return render_template('signup/start.html')
 	signup_delay = signup_ratelimit.get_delay(request.form['mail'])
 	host_delay = host_ratelimit.get_delay()
 	if signup_delay and signup_delay > host_delay:
-		return render_template('signup/start.html', error=_('Too many signup requests with this mail address! Please wait %(delay)s.',
-		                                                    delay=format_delay(signup_delay)))
+		flash(_('Too many signup requests with this mail address! Please wait %(delay)s.',
+		        delay=format_delay(signup_delay)), 'error')
+		return render_template('signup/start.html')
 	if host_delay:
-		return render_template('signup/start.html', error=_('Too many requests! Please wait %(delay)s.', delay=format_delay(host_delay)))
+		flash(_('Too many requests! Please wait %(delay)s.', delay=format_delay(host_delay)), 'error')
+		return render_template('signup/start.html')
 	host_ratelimit.log()
 	signup = InviteSignup(invite=invite, loginname=request.form['loginname'],
 	                      displayname=request.form['displayname'],
@@ -187,11 +190,13 @@ def signup_submit(invite_id, token):
 	                      password=request.form['password1'])
 	valid, msg = signup.validate()
 	if not valid:
-		return render_template('signup/start.html', error=msg)
+		flash(msg, 'error')
+		return render_template('signup/start.html')
 	db.session.add(signup)
 	db.session.commit()
 	sent = sendmail(signup.mail, 'Confirm your mail address', 'signup/mail.txt', signup=signup)
 	if not sent:
-		return render_template('signup/start.html', error=_('Could not send mail'))
+		flash(_('Could not send mail'), 'error')
+		return render_template('signup/start.html')
 	signup_ratelimit.log(request.form['mail'])
 	return render_template('signup/submitted.html', signup=signup)
diff --git a/uffd/signup/templates/signup/confirm.html b/uffd/signup/templates/signup/confirm.html
index ffd13dd4..087ef8b0 100644
--- a/uffd/signup/templates/signup/confirm.html
+++ b/uffd/signup/templates/signup/confirm.html
@@ -5,9 +5,6 @@
 	<div class="col-12">
 		<h2 class="text-center">{{_('Complete Registration')}}</h2>
 	</div>
-	{% if error %}
-	<div class="alert alert-danger" role="alert">{{ error }}</div>
-	{% endif %}
 	<div class="form-group col-12">
 		<label for="user-password1">{{_('Please enter your password to complete the account registration')}}</label>
 		<input type="password" class="form-control" id="user-password1" name="password" required="required">
diff --git a/uffd/signup/templates/signup/start.html b/uffd/signup/templates/signup/start.html
index 3238fffe..1c72f469 100644
--- a/uffd/signup/templates/signup/start.html
+++ b/uffd/signup/templates/signup/start.html
@@ -5,11 +5,6 @@
 	<div class="col-12">
 		<h2 class="text-center">{{_('Account Registration')}}</h2>
 	</div>
-	{% if error %}
-	<div class="form-group col-12">
-		<div class="alert alert-danger" role="alert">{{ error }}</div>
-	</div>
-	{% endif %}
 	<div class="form-group col-12">
 		<label for="user-loginname">{{_('Login Name')}}</label>
 		<div class="js-only-input-group">
diff --git a/uffd/signup/views.py b/uffd/signup/views.py
index a14d0ef0..752cc587 100644
--- a/uffd/signup/views.py
+++ b/uffd/signup/views.py
@@ -46,14 +46,17 @@ def signup_check():
 @signup_enabled
 def signup_submit():
 	if request.form['password1'] != request.form['password2']:
-		return render_template('signup/start.html', error=_('Passwords do not match'))
+		flash(_('Passwords do not match'), 'error')
+		return render_template('signup/start.html')
 	signup_delay = signup_ratelimit.get_delay(request.form['mail'])
 	host_delay = host_ratelimit.get_delay()
 	if signup_delay and signup_delay > host_delay:
-		return render_template('signup/start.html', error=_('Too many signup requests with this mail address! Please wait %(delay)s.',
-		                                                    delay=format_delay(signup_delay)))
+		flash(_('Too many signup requests with this mail address! Please wait %(delay)s.',
+		        delay=format_delay(signup_delay)), 'error')
+		return render_template('signup/start.html')
 	if host_delay:
-		return render_template('signup/start.html', error=_('Too many requests! Please wait %(delay)s.', delay=format_delay(host_delay)))
+		flash(_('Too many requests! Please wait %(delay)s.', delay=format_delay(host_delay)), 'error')
+		return render_template('signup/start.html')
 	host_ratelimit.log()
 	signup = Signup(loginname=request.form['loginname'],
 	                displayname=request.form['displayname'],
@@ -64,12 +67,14 @@ def signup_submit():
 	signup.set_password(request.form['password1'])
 	valid, msg = signup.validate()
 	if not valid:
-		return render_template('signup/start.html', error=msg)
+		flash(msg, 'error')
+		return render_template('signup/start.html')
 	db.session.add(signup)
 	db.session.commit()
 	sent = sendmail(signup.mail, 'Confirm your mail address', 'signup/mail.txt', signup=signup)
 	if not sent:
-		return render_template('signup/start.html', error=_('Could not send mail'))
+		flash(_('Could not send mail'), 'error')
+		return render_template('signup/start.html')
 	signup_ratelimit.log(request.form['mail'])
 	return render_template('signup/submitted.html', signup=signup)
 
@@ -91,16 +96,19 @@ def signup_confirm_submit(signup_id, token):
 	confirm_delay = confirm_ratelimit.get_delay(token)
 	host_delay = host_ratelimit.get_delay()
 	if confirm_delay and confirm_delay > host_delay:
-		return render_template('signup/confirm.html', signup=signup, error=_('Too many failed attempts! Please wait %(delay)s.', delay=format_delay(confirm_delay)))
+		flash(_('Too many failed attempts! Please wait %(delay)s.', delay=format_delay(confirm_delay)), 'error')
+		return render_template('signup/confirm.html', signup=signup)
 	if host_delay:
-		return render_template('signup/confirm.html', signup=signup, error=_('Too many requests! Please wait %(delay)s.', delay=format_delay(host_delay)))
+		return render_template('signup/confirm.html', signup=signup)
 	if not signup.password.verify(request.form['password']):
 		host_ratelimit.log()
 		confirm_ratelimit.log(token)
-		return render_template('signup/confirm.html', signup=signup, error=_('Wrong password'))
+		flash(_('Wrong password'), 'error')
+		return render_template('signup/confirm.html', signup=signup)
 	user, msg = signup.finish(request.form['password'])
 	if user is None:
-		return render_template('signup/confirm.html', signup=signup, error=msg)
+		flash(msg, 'error')
+		return render_template('signup/confirm.html', signup=signup)
 	db.session.commit()
 	set_session(user, skip_mfa=True)
 	flash(_('Your account was successfully created'))
diff --git a/uffd/templates/base.html b/uffd/templates/base.html
index 5a319101..d3a8b138 100644
--- a/uffd/templates/base.html
+++ b/uffd/templates/base.html
@@ -117,9 +117,9 @@
 		{% block main %}
 		<main role="main" class="container mt-3">
 			<div class="row">
-				{% for message in get_flashed_messages() %}
+				{% for category, message in get_flashed_messages(with_categories=true) %}
 				<div class="col-12">
-					<div class="alert alert-primary" role="alert">{{ message }}</div>
+					<div class="alert alert-{{ 'danger' if category == 'error' else 'warning' if category == 'warning' else 'primary' }}" role="alert">{{ message }}</div>
 				</div>
 				{% endfor %}
 			</div>
diff --git a/uffd/templates/base_narrow.html b/uffd/templates/base_narrow.html
index 2077185b..e4d2389b 100644
--- a/uffd/templates/base_narrow.html
+++ b/uffd/templates/base_narrow.html
@@ -5,9 +5,9 @@
 	<div class="row justify-content-center">
 		<div class="col-lg-6 col-md-10 px-0">
 			<div class="row">
-				{% for message in get_flashed_messages() %}
+				{% for category, message in get_flashed_messages(with_categories=true) %}
 				<div class="col-12">
-					<div class="alert alert-primary" role="alert">{{ message }}</div>
+					<div class="alert alert-{{ 'danger' if category == 'error' else 'warning' if category == 'warning' else 'primary' }}" role="alert">{{ message }}</div>
 				</div>
 				{% endfor %}
 			</div>
-- 
GitLab