#31 API Token mail

* sends an email to the support address if a new token has been generated
This commit is contained in:
mpeltriaux 2022-01-27 11:47:27 +01:00
parent b13e67e061
commit b86202ba98
6 changed files with 89 additions and 19 deletions

View File

@ -213,6 +213,7 @@ EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location
DEFAULT_FROM_EMAIL = "service@ksp.de" # The default email address for the 'from' element
EMAIL_HOST = "localhost"
EMAIL_REPLY_TO = "ksp-servicestelle@sgdnord.rlp.de"
SUPPORT_MAIL_RECIPIENT = EMAIL_REPLY_TO
EMAIL_PORT = "25"
#EMAIL_HOST_USER = ""
#EMAIL_HOST_PASSWORD = ""

View File

@ -11,7 +11,7 @@ from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _
from konova.sub_settings.django_settings import DEFAULT_FROM_EMAIL, EMAIL_REPLY_TO
from konova.sub_settings.django_settings import DEFAULT_FROM_EMAIL, EMAIL_REPLY_TO, SUPPORT_MAIL_RECIPIENT
logger = logging.getLogger(__name__)
@ -181,3 +181,24 @@ class Mailer:
msg
)
def send_mail_verify_api_token(self, user):
""" Send a mail if a user creates a new token
Args:
user (User): The user, having a new api token
Returns:
"""
context = {
"user": user,
"EMAIL_REPLY_TO": EMAIL_REPLY_TO,
}
msg = render_to_string("email/api/verify_token.html", context)
user_mail_address = [SUPPORT_MAIL_RECIPIENT]
self.send(
user_mail_address,
_("Request for new API token"),
msg
)

Binary file not shown.

View File

@ -26,7 +26,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-01-27 11:35+0100\n"
"POT-Creation-Date: 2022-01-27 11:44+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -1804,6 +1804,10 @@ msgstr "{} - Freigegebene Daten gelöscht"
msgid "{} - Shared data checked"
msgstr "{} - Freigegebene Daten geprüft"
#: konova/utils/mailer.py:201 templates/email/api/verify_token.html:4
msgid "Request for new API token"
msgstr "Anfrage für neuen API Token"
#: konova/utils/message_templates.py:11
msgid "There was an error on this form."
msgstr "Es gab einen Fehler im Formular."
@ -1938,6 +1942,32 @@ msgstr ""
msgid "Something happened. We are working on it!"
msgstr "Irgendetwas ist passiert. Wir arbeiten daran!"
#: templates/email/api/verify_token.html:7
msgid "Hello support"
msgstr "Hallo Support"
#: templates/email/api/verify_token.html:9
msgid "you need to verify the API token for user"
msgstr "Sie müssen einen API Token für folgenden Nutzer freischalten"
#: templates/email/api/verify_token.html:13
msgid ""
"If unsure, please contact the user. The API token can not be used until you "
"activated it in the admin backend."
msgstr ""
"Falls Sie sich unsicher sind, kontaktieren Sie den Nutzer vorher. Der API Token kann so lange nicht verwendet werden, "
"wie er noch nicht von Ihnen im Admin Backend aktiviert worden ist."
#: templates/email/api/verify_token.html:16
#: templates/email/checking/shared_data_checked.html:17
#: templates/email/deleting/shared_data_deleted.html:17
#: templates/email/recording/shared_data_recorded.html:17
#: templates/email/recording/shared_data_unrecorded.html:17
#: templates/email/sharing/shared_access_given.html:18
#: templates/email/sharing/shared_access_removed.html:18
msgid "Best regards"
msgstr "Beste Grüße"
#: templates/email/checking/shared_data_checked.html:4
msgid "Shared data checked"
msgstr "Freigegebene Daten geprüft"
@ -1963,15 +1993,6 @@ msgstr ""
"Das bedeutet, dass die zuständige Zulassungsbehörde die Korrektheit des "
"Datensatzes soeben bestätigt hat."
#: templates/email/checking/shared_data_checked.html:17
#: templates/email/deleting/shared_data_deleted.html:17
#: templates/email/recording/shared_data_recorded.html:17
#: templates/email/recording/shared_data_unrecorded.html:17
#: templates/email/sharing/shared_access_given.html:18
#: templates/email/sharing/shared_access_removed.html:18
msgid "Best regards"
msgstr "Beste Grüße"
#: templates/email/deleting/shared_data_deleted.html:4
msgid "Shared data deleted"
msgstr "Freigegebene Daten gelöscht"
@ -2315,7 +2336,7 @@ msgstr "Benachrichtigungen"
#: user/templates/user/index.html:58
msgid "See or edit your API token"
msgstr ""
msgstr "API token einsehen oder neu generieren"
#: user/templates/user/index.html:61
msgid "API"
@ -2345,23 +2366,23 @@ msgstr "Token noch nicht freigeschaltet"
msgid "Valid until"
msgstr "Läuft ab am"
#: user/views.py:30
#: user/views.py:31
msgid "User settings"
msgstr "Einstellungen"
#: user/views.py:56
#: user/views.py:57
msgid "Notifications edited"
msgstr "Benachrichtigungen bearbeitet"
#: user/views.py:68
#: user/views.py:69
msgid "User notifications"
msgstr "Benachrichtigungen"
#: user/views.py:90
#: user/views.py:92
msgid "New token generated. Administrators need to validate."
msgstr "Neuer Token generiert. Administratoren sind informiert."
#: user/views.py:99
#: user/views.py:103
msgid "User API token"
msgstr "API Nutzer Token"

View File

@ -0,0 +1,23 @@
{% load i18n %}
<div>
<h2>{% trans 'Request for new API token' %}</h2>
<hr>
<article>
{% trans 'Hello support' %},
<br>
{% trans 'you need to verify the API token for user' %}:
<br>
<br>
<strong>{{user.username}}</strong>
<br>
<br>
{% trans 'If unsure, please contact the user. The API token can not be used until you activated it in the admin backend.' %}
<br>
<br>
{% trans 'Best regards' %}
<br>
KSP
</article>
</div>

View File

@ -2,6 +2,7 @@ from django.contrib import messages
from django.contrib.auth.decorators import login_required
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
from konova.utils.mailer import Mailer
from konova.utils.message_templates import FORM_INVALID
from user.models import User
from django.http import HttpRequest
@ -83,18 +84,21 @@ def api_token_view(request: HttpRequest):
"""
template = "user/token.html"
form = UserAPITokenForm(request.POST or None, instance=request.user)
user = request.user
form = UserAPITokenForm(request.POST or None, instance=user)
if request.method == "POST":
if form.is_valid():
token = form.save()
messages.info(request, _("New token generated. Administrators need to validate."))
mailer = Mailer()
mailer.send_mail_verify_api_token(user)
return redirect("user:api-token")
else:
messages.error(request, FORM_INVALID, extra_tags="danger")
elif request.method != "GET":
raise NotImplementedError
context = {
"user": request.user,
"user": user,
"form": form,
TAB_TITLE_IDENTIFIER: _("User API token"),
}