From c19ded10b1dc47f722d62d697f7fd924876d948a Mon Sep 17 00:00:00 2001 From: gpkvt Date: Tue, 6 Jun 2023 14:36:24 +0200 Subject: [PATCH] added form to submit questions via mail --- config.ini | 10 ++++++++- game.py | 49 ++++++++++++++++++++++++++++++++++++----- static/css/base.css | 12 ++++++++++ templates/base.html | 8 ------- templates/index.html | 16 ++++++++++++-- templates/mailform.html | 22 ++++++++++++++++++ templates/thanks.html | 16 ++++++++++++++ 7 files changed, 116 insertions(+), 17 deletions(-) create mode 100644 templates/mailform.html create mode 100644 templates/thanks.html diff --git a/config.ini b/config.ini index e262d44..3905c70 100644 --- a/config.ini +++ b/config.ini @@ -3,7 +3,11 @@ port = 5000 bind = 0.0.0.0 base_url = https://ab.21x9.org timezone = Europe/Berlin -mail = abgame@21x9.org +mailform = false +mailserver = localhost:25 +mailto = abgame@21x9.org +mailfrom = abgame@21x9.org +mailsubject = A/B Game Questions separator_char = ; theme = dark animations = 1 @@ -20,3 +24,7 @@ questions_prefix = Es gibt derzeit questions_suffix = Fragen. send_questions = Einreichen! desc = Ein simples Entweder/Oder-Entscheidungsspiel +submit = Absenden +submit_questions =

Bitte gib deine Fragenvorschläge in das untenstehende Feld ein.

Wenn du mir einen Gefallen tun möchtest, gib die Vorschläge im Format "A;B" an und schreibe immer nur ein Fragenpaar in eine Zeile.

Danke!

+submit_thanks_title = Danke! +submit_thanks =

Vielen Dank für die Einsendung.

diff --git a/game.py b/game.py index efde033..9993b8b 100755 --- a/game.py +++ b/game.py @@ -1,8 +1,10 @@ #! /bin/python3 -from flask import Flask, render_template +from flask import Flask, render_template, request from configparser import ConfigParser from datetime import datetime +from email.message import EmailMessage +import smtplib import random import pytz @@ -25,16 +27,27 @@ i18n = { 'separator': config.get('i18n', 'separator'), 'mailtext': config.get('i18n', 'mail_link'), 'helptext': config.get('i18n', 'help_link'), - 'help': config.get('i18n', 'help') + 'help': config.get('i18n', 'help'), + 'submit': config.get('i18n', 'submit'), + 'submit_questions': config.get('i18n', 'submit_questions'), + 'submit_thanks_title': config.get('i18n', 'submit_thanks_title'), + 'submit_thanks': config.get('i18n', 'submit_thanks') } conf = { 'separator_char': config.get('main', 'separator_char'), - 'mailto': config.get('main', 'mail'), + 'mailform': config.get('main', 'mailform').lower(), + 'mailserver': config.get('main', 'mailserver'), + 'mailto': config.get('main', 'mailto'), + 'mailfrom': config.get('main', 'mailfrom'), + 'mailsubject': config.get('main', 'mailsubject'), 'url': config.get('main', 'base_url'), 'theme': config.get('main', 'theme'), 'animations': config.get('main', 'animations') } +with open("ab.txt", "r") as f: + num_lines = sum(1 for _ in f) + def getEpoch(): now = datetime.now(tz=tz) epoch = now.timestamp() @@ -52,6 +65,33 @@ def internal_server_error(e): epoch = getEpoch() return render_template('500.html', config=conf, i18n=i18n, epoch=epoch), 500 +if conf['mailform'] == "true": + @app.route("/form") + def mailform(): + epoch = getEpoch() + return render_template('mailform.html', config=conf, i18n=i18n, epoch=epoch, num_lines=num_lines) + + @app.route("/send", methods=['POST', 'GET']) + def sendmail(): + epoch = getEpoch() + + if request.method == 'POST': + mailcontent = request.form['questions'] + else: + mailcontent = request.args.get('questions') + + message = EmailMessage() + message.set_content(mailcontent) + message['Subject'] = conf['mailsubject'] + message['From'] = conf['mailfrom'] + message['To'] = conf['mailto'] + + smtp_server = smtplib.SMTP(conf['mailserver']) + smtp_server.send_message(message) + smtp_server.quit() + + return render_template('thanks.html', config=conf, i18n=i18n, epoch=epoch, questions=mailcontent) + @app.route("/") def hello(): ablines = [] @@ -69,9 +109,6 @@ def hello(): {'A': str(ab[0]), 'B': str(ab[1])} ) - with open("ab.txt", "r") as f: - num_lines = sum(1 for _ in f) - return render_template('index.html', content=ablines, config=conf, i18n=i18n, num_lines=num_lines, epoch=epoch) def getContent(): diff --git a/static/css/base.css b/static/css/base.css index 918ed30..dfb44cc 100644 --- a/static/css/base.css +++ b/static/css/base.css @@ -48,6 +48,14 @@ a:hover #help { transform: translate(-50%); } +textarea { + height: 250px; + width: 100%; + background-color: var(--bg-b); + color: var(--fg-b); + border: 1px solid var(--bg-a); +} + #background { overflow: hidden; position: absolute; @@ -98,6 +106,10 @@ a:hover #help { transform: translate(-50%, -50%); } +.form { + padding: 20px; +} + .ab { float: none; border-radius: 10px; diff --git a/templates/base.html b/templates/base.html index 09c5da4..e664790 100644 --- a/templates/base.html +++ b/templates/base.html @@ -7,9 +7,6 @@ {{ title }} - - - {%- if config.animations == "1" or config.animations == "true" %} @@ -41,11 +38,6 @@ {%- endblock %} - \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index d94cc81..562b5a3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,6 +1,10 @@ {% extends 'base.html' %} {% block main %} + + + + {%- set id = namespace(value=0) %} {%- for question in content %} {%- set id.value = id.value + 1 %} @@ -17,7 +21,9 @@
{{ i18n.questions_prefix }} {{ num_lines }} {{ i18n.questions_suffix }}
- {{ i18n.mailtext }} - + {%- if config.mailform == "true" %} + {{ i18n.mailtext }} - + {%- endif %} {{ i18n.helptext }}
{{ i18n.help|safe }} @@ -30,4 +36,10 @@
-{% endblock %} + + + {% endblock %} diff --git a/templates/mailform.html b/templates/mailform.html new file mode 100644 index 0000000..35c36d4 --- /dev/null +++ b/templates/mailform.html @@ -0,0 +1,22 @@ +{% extends 'base.html' %} + +{% block main %} +
+ +
+
+
+
+
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/thanks.html b/templates/thanks.html new file mode 100644 index 0000000..842c054 --- /dev/null +++ b/templates/thanks.html @@ -0,0 +1,16 @@ +{% extends 'base.html' %} + +{% block main %} +
+
+

{{ i18n.submit_thanks_title }}

+ {{ i18n.submit_thanks | safe }} +

[Home]

+
+
+ +{% endblock %}