Confirm email addresses

This commit is contained in:
Peter J. Holzer 2022-03-28 00:33:37 +02:00 committed by Peter J. Holzer
parent 7cba1e5dea
commit ea2a984d24
5 changed files with 135 additions and 10 deletions

View File

@ -1,9 +1,12 @@
import email.message
import json
import logging import logging
import smtplib
import uuid import uuid
import psycopg2 import psycopg2
import psycopg2.extras import psycopg2.extras
from flask import Flask, request, render_template, render_template_string, session, g from flask import Flask, request, render_template, render_template_string, session, g, url_for
from flask_babel import Babel, _ from flask_babel import Babel, _
logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s %(lineno)d | %(message)s", level=logging.DEBUG) logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s %(lineno)d | %(message)s", level=logging.DEBUG)
@ -135,20 +138,34 @@ def result(public_id):
stats["band_rank"] = r["rank"] stats["band_rank"] = r["rank"]
stats["band_count"] = r["c"] stats["band_count"] = r["c"]
ask_mail = session["uuid"].endswith(public_id) uuid = session.get("uuid", "")
log.debug("uuid = %s, public_id=%s, ask_mail=%s", session["uuid"], public_id, ask_mail) ask_mail = uuid.endswith(public_id)
log.debug("uuid = %s, public_id=%s, ask_mail=%s", uuid, public_id, ask_mail)
return render_template("result.html", public_id=public_id, stats=stats, ask_mail=ask_mail) return render_template("result.html", public_id=public_id, stats=stats, ask_mail=ask_mail)
@app.route("/add-email", methods=["POST"]) @app.route("/add-email", methods=["POST"])
def add_email(): def add_email():
csr = get_cursor() csr = get_cursor()
csr.execute("update users set email=%s where uuid = %s", (request.form["email"], session["uuid"],)) csr.execute("update users set email=%s, lang=%s, confirmation_sent_ts=now() where uuid = %s",
(request.form["email"], get_locale(), session["uuid"],))
if csr.rowcount: if csr.rowcount:
send_mail(request.form["email"], session["uuid"])
return render_template_string(_("Saved. Expect mail at {{email}}"), return render_template_string(_("Saved. Expect mail at {{email}}"),
email=request.form["email"]) email=request.form["email"])
else: else:
return _("<span>You don't exist. Go away!</span>") return _("<span>You don't exist. Go away!</span>")
@app.route("/confirm-email/<uuid>", methods=["GET"])
def confirm_email_ask(uuid):
return render_template("confirm.html", uuid=uuid)
@app.route("/confirm-email/<uuid>", methods=["POST"])
def confirm_email_do(uuid):
csr = get_cursor()
csr.execute("update users set confirmation_received_ts=now(), confirmation_info=%s where uuid=%s",
(json.dumps({"ip": request.remote_addr, "ua": request.user_agent.string}), uuid))
return "<p>" + _("Thanks!") + "</p>"
# Middleware # Middleware
@babel.localeselector @babel.localeselector
@ -188,10 +205,34 @@ def teardown_db(exception):
db.close() db.close()
def new_user(): def new_user():
lang = get_locale()
user_uuid = str(uuid.uuid4()) user_uuid = str(uuid.uuid4())
user_public_id = user_uuid[-12:] user_public_id = user_uuid[-12:]
csr = get_cursor() csr = get_cursor()
csr.execute("insert into users(uuid, public_id) values(%s, %s)", (user_uuid, user_public_id,)) csr.execute("insert into users(uuid, public_id, lang) values(%s, %s, %s)",
(user_uuid, user_public_id, lang))
return user_uuid, user_public_id return user_uuid, user_public_id
def send_mail(mailaddress, uuid):
msg = email.message.EmailMessage()
msg["From"] ="i12e@hjp.at"
msg["To"] = mailaddress
msg["Subject"] = _("The Musical Internatiionale: Confirm mail address")
confirmation_url = url_for("confirm_email_ask", uuid=uuid, _external=True)
body = _(
"Hello,\n"
"\n"
"somebody requested news about new developments at https://i12e.hjp.at/ to be sent to\n"
"{mailaddress}.\n"
"\n"
"To confirm that that this was you, please visit this url:\n"
"{url}\n"
"\n"
"With musical greetings\n"
" I12E Bot\n"
"").format(mailaddress=mailaddress, url=confirmation_url)
msg.set_content(body)
mta = smtplib.SMTP(host="localhost")
mta.send_message(msg)
# vim: tw=99 # vim: tw=99

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PROJECT VERSION\n" "Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2022-03-20 23:24+0100\n" "POT-Creation-Date: 2022-03-28 00:28+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -17,22 +17,49 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.9.1\n" "Generated-By: Babel 2.9.1\n"
#: app.py:22 #: app.py:25
msgid "hello" msgid "hello"
msgstr "" msgstr ""
#: app.py:147 #: app.py:153
msgid "Saved. Expect mail at {{email}}" msgid "Saved. Expect mail at {{email}}"
msgstr "" msgstr ""
#: app.py:150 #: app.py:156
msgid "<span>You don't exist. Go away!</span>" msgid "<span>You don't exist. Go away!</span>"
msgstr "" msgstr ""
#: templates/home.html:6 templates/result.html:6 #: app.py:167
msgid "Thanks!"
msgstr ""
#: app.py:220
msgid "The Musical Internatiionale: Confirm mail address"
msgstr ""
#: app.py:222
msgid ""
"Hello,\n"
"\n"
"somebody requested news about new developments at https://i12e.hjp.at/ to"
" be sent to\n"
"{mailaddress}.\n"
"\n"
"To confirm that that this was you, please visit this url:\n"
"{url}\n"
"\n"
"With musical greetings\n"
" I12E Bot\n"
msgstr ""
#: templates/confirm.html:6 templates/home.html:6 templates/result.html:6
msgid "The Musical Internatiionale" msgid "The Musical Internatiionale"
msgstr "" msgstr ""
#: templates/confirm.html:14
msgid "I confirm that I want to receive mails about new developments at this site"
msgstr ""
#: templates/home.html:20 #: templates/home.html:20
msgid "" msgid ""
"All the countries in the world — which bands or solo musicians from these" "All the countries in the world — which bands or solo musicians from these"

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>
{{ _("The Musical Internatiionale") }}
</title>
<script src='https://unpkg.com/htmx.org@1.7.0'></script>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<main>
<button class="btn" hx-post="{{url_for('confirm_email_do', uuid=uuid)}}" hx-swap="outerHTML">
{{_("I confirm that I want to receive mails about new developments at this site")}}
</button>
</main>
</body>
</html>

View File

@ -30,11 +30,50 @@ msgstr "Gespeichert. Du wirst Mail an {{email}} bekommen."
msgid "<span>You don't exist. Go away!</span>" msgid "<span>You don't exist. Go away!</span>"
msgstr "Du existierst nicht. Verschwinde!" msgstr "Du existierst nicht. Verschwinde!"
#: app.py:167
msgid "Thanks!"
msgstr "Danke!"
#: app.py:206
msgid "The Musical Internatiionale: Confirm mail address"
msgstr "Die Musik-Internationale: Bestätigung der Mail-Adresse"
#: app.py:207
msgid ""
"Hello,\n"
"\n"
"somebody requested news about new developments at https://i12e.hjp.at/ to"
" be sent to\n"
"{mailaddress}.\n"
"\n"
"To confirm that that this was you, please visit this url:\n"
"{url}\n"
"\n"
"With musical greetings\n"
" I12E Bot\n"
msgstr ""
"Hallo!\n"
"\n"
"Jemand hat darum gebeten, dass Informationen über neue Entwicklungen "
"auf https://i12e.hjp.at/ an\n"
"{mailaddress}.\n"
"geschickt werden.\n"
"Um zu bestätigen, dass das Du warst, besuche bitte diesen URL:\n"
"{url}\n"
"\n"
"With musical greetings\n"
" I12E Bot\n"
#: templates/home.html:6 #: templates/home.html:6
msgid "The Musical Internatiionale" msgid "The Musical Internatiionale"
msgstr "Die Musik-Internationale" msgstr "Die Musik-Internationale"
#: templates/home.html:21 #: templates/home.html:21
msgid "I confirm that I want to receive mails about new developments at this site"
msgstr "Ich bestätige, dass im Mails über neue Entwicklungen auf dieser "
"Website erhalen möchte"
#: templates/home.html:20
msgid "" msgid ""
"All the countries in the world — which bands or solo musicians from these" "All the countries in the world — which bands or solo musicians from these"
" countries do you know?" " countries do you know?"