From fce0e8d59558bd417ef38e0f7c51caa2cbc8258f Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Mon, 24 Oct 2022 19:10:35 +0200 Subject: [PATCH 1/2] Auto-register user when creating thread without account --- main.py | 35 +++++++++++++++++++++-------------- templates/new_thread.html | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index 4a6ec54..52f791a 100644 --- a/main.py +++ b/main.py @@ -185,23 +185,30 @@ def user_info(user_id): @app.route('/forum//new/', methods = ['GET', 'POST']) def new_thread(forum_id): user_id = session.get('user_id') - if user_id is None: + if user_id is None and not config.registration_enabled: + # Can't create a thread without an account return redirect(url_for('login')) if request.method == 'POST': - title, text = request.form['title'].strip(), trim_text(request.form['text']) - title = title.strip() - if title == '' or text == '': - flash('Title and text may not be empty', 'error') - return redirect(url_for('forum', forum_id = forum_id)) - id = db.add_thread(user_id, forum_id, title, text, time.time_ns()) - if id is None: - flash('Failed to create thread', 'error') - return redirect(url_for('forum', forum_id = forum_id)) - else: - id, = id - flash('Created thread', 'success') - return redirect(url_for('thread', thread_id = id)) + if user_id is None: + # Attempt to create a user account first + if register_user(True): + user_id = session['user_id'] + + if user_id is not None: + title, text = request.form['title'].strip(), trim_text(request.form['text']) + title = title.strip() + if title == '' or text == '': + flash('Title and text may not be empty', 'error') + return redirect(url_for('forum', forum_id = forum_id)) + id = db.add_thread(user_id, forum_id, title, text, time.time_ns()) + if id is None: + flash('Failed to create thread', 'error') + return redirect(url_for('forum', forum_id = forum_id)) + else: + id, = id + flash('Created thread', 'success') + return redirect(url_for('thread', thread_id = id)) return render_template( 'new_thread.html', diff --git a/templates/new_thread.html b/templates/new_thread.html index b2d9999..13cafce 100644 --- a/templates/new_thread.html +++ b/templates/new_thread.html @@ -1,17 +1,45 @@ {% extends 'base.html' %} {% block content %} +{%- if user is none -%} +
+ {#- + Using the password generator for usernames should be sufficient to ensure it is unique. + If not, it means the password generator is broken and *must* be fixed. + -#} + + + {%- set q, a = gen_captcha() -%} + + + + + + + + + + + + + + +
Title
Text
{{ q }}
+

(I already have an account)

+
+{%- else -%}
- + - +
Title
Text

+{%- endif -%} {% endblock %} From 5610b262208890cc1d1e5861ab0b2d2e9d056a18 Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Mon, 24 Oct 2022 19:11:27 +0200 Subject: [PATCH 2/2] Show borders for table.form elements --- static/theme.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/theme.css b/static/theme.css index 426effe..3ab2203 100644 --- a/static/theme.css +++ b/static/theme.css @@ -46,6 +46,7 @@ table { } tr:not(:last-child) { + /* FIXME this is sometimes invisible depending on zoom level in Firefox. */ border-bottom: 1px solid; } @@ -83,7 +84,6 @@ form.form { } table.form { - border-collapse: unset; width: 100%; }