added form to submit questions via mail

This commit is contained in:
gpkvt 2023-06-06 14:36:24 +02:00
parent cf59ff986f
commit c19ded10b1
7 changed files with 116 additions and 17 deletions

View File

@ -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 = <p>Bitte gib deine Fragenvorschläge in das untenstehende Feld ein.</p><p>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.</p><p>Danke!</p>
submit_thanks_title = Danke!
submit_thanks = <p>Vielen Dank für die Einsendung.</p>

49
game.py
View File

@ -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():

View File

@ -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;

View File

@ -7,9 +7,6 @@
<head>
<title>{{ title }}</title>
<script type="text/javascript" src="/js/toggle.js?t={{ epoch }}"></script>
<script type="text/javascript" src="/js/autoReload.js?t={{ epoch }}"></script>
<script type="text/javascript" src="/js/shortcut.js?t={{ epoch }}"></script>
<link rel="stylesheet" type="text/css" href="/css/base.css?t={{ epoch }}">
<link rel="stylesheet" type="text/css" href="/css/colors.css?t={{ epoch }}">
{%- if config.animations == "1" or config.animations == "true" %}
@ -41,11 +38,6 @@
{%- endblock %}
</div>
</div>
<script type="text/javascript">
initToggle();
autoReload();
monitorRadio();
</script>
</body>
</html>

View File

@ -1,6 +1,10 @@
{% extends 'base.html' %}
{% block main %}
<script type="text/javascript" src="/js/toggle.js?t={{ epoch }}"></script>
<script type="text/javascript" src="/js/autoReload.js?t={{ epoch }}"></script>
<script type="text/javascript" src="/js/shortcut.js?t={{ epoch }}"></script>
{%- set id = namespace(value=0) %}
{%- for question in content %}
{%- set id.value = id.value + 1 %}
@ -17,7 +21,9 @@
<div id="questions">
{{ i18n.questions_prefix }} {{ num_lines }} {{ i18n.questions_suffix }}
<br>
<a href="mailto:{{ config.mailto }}">{{ i18n.mailtext }}</a> -
{%- if config.mailform == "true" %}
<a href="/form">{{ i18n.mailtext }}</a> -
{%- endif %}
<a href="#">{{ i18n.helptext }}
<div id="help">
{{ i18n.help|safe }}
@ -30,4 +36,10 @@
<button id="next" type="submit">{{ i18n.more }}</button>
</form>
</div>
<script type="text/javascript">
initToggle();
autoReload();
monitorRadio();
</script>
{% endblock %}

22
templates/mailform.html Normal file
View File

@ -0,0 +1,22 @@
{% extends 'base.html' %}
{% block main %}
<form action="/send" method="post">
<input type="hidden" name="t" value="{{ epoch }}">
<div class="ab">
<div class="form">
<div><label for="questions">{{ i18n.submit_questions | safe }}</label></div>
<div><textarea name="questions" required></textarea></div>
</div>
</div>
<div id="footer">
<div id="questions">
{{ i18n.questions_prefix }} {{ num_lines }} {{ i18n.questions_suffix }}
<br>
<a href="/">Home</a>
</div>
{% include 'toggle.html' %}
<button id="next" type="submit">{{ i18n.submit }}</button>
</div>
</form>
{% endblock %}

16
templates/thanks.html Normal file
View File

@ -0,0 +1,16 @@
{% extends 'base.html' %}
{% block main %}
<div class="ab">
<div class="a">
<h1>{{ i18n.submit_thanks_title }}</h1>
{{ i18n.submit_thanks | safe }}
<p>[<a href="/">Home</a>]</p>
</div>
</div>
<div id="footer">
<div class="box">&nbsp;</div>
{% include 'toggle.html' %}
<div class="box">&nbsp;</div>
</div>
{% endblock %}