From 469ce1c6e049865ed1c83c22f7f8d965ef28c270 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 20 Oct 2022 12:51:20 +0200 Subject: [PATCH] pass emails to templates --- ereuse_devicehub/forms.py | 24 ++++++++----------- ereuse_devicehub/mail/sender.py | 5 +--- .../ereuse_devicehub/email_admin_new_user.txt | 4 ++++ .../ereuse_devicehub/email_validation.txt | 2 ++ .../ereuse_devicehub/user_registration.html | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) create mode 100644 ereuse_devicehub/templates/ereuse_devicehub/email_admin_new_user.txt create mode 100644 ereuse_devicehub/templates/ereuse_devicehub/email_validation.txt diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index daa0760f..2b2bce2d 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -1,5 +1,5 @@ from flask import current_app as app -from flask import g +from flask import g, render_template from flask_wtf import FlaskForm from werkzeug.security import generate_password_hash from wtforms import ( @@ -188,25 +188,21 @@ class UserNewRegisterForm(FlaskForm): host = app.config.get('HOST') token = self._token url = f'https://{ host }/validate_user/{ token }' - message = """ - Hello, you are register in Usody.com - Please for activate your account click in the next address: """ - message += url + template = 'ereuse_devicehub/email_validation.txt' + message = render_template(template, url=url) subject = "Validate email for register in Usody.com" send_email(subject, [self.email.data], message) def send_mail_admin(self, user): person = next(iter(user.individuals)) - email = person.email - name = person.name - telephone = person.telephone - - message = f"""A new user has been registered. These are your data" - Name: {name} - Telephone: {telephone} - Email: {email} - """ + context = { + 'email': person.email, + 'name': person.name, + 'telephone': person.telephone, + } + template = 'ereuse_devicehub/email_admin_new_user.txt' + message = render_template(template, **context) subject = "New Register" email_admin = app.config.get("EMAIL_ADMIN") diff --git a/ereuse_devicehub/mail/sender.py b/ereuse_devicehub/mail/sender.py index 9d99e80f..3b099cba 100644 --- a/ereuse_devicehub/mail/sender.py +++ b/ereuse_devicehub/mail/sender.py @@ -28,7 +28,4 @@ def send_email( if html_body: msg.html = html_body - Thread( - target=_send_async_email, - args=(app._get_current_object(), msg) - ).start() + Thread(target=_send_async_email, args=(app._get_current_object(), msg)).start() diff --git a/ereuse_devicehub/templates/ereuse_devicehub/email_admin_new_user.txt b/ereuse_devicehub/templates/ereuse_devicehub/email_admin_new_user.txt new file mode 100644 index 00000000..96759431 --- /dev/null +++ b/ereuse_devicehub/templates/ereuse_devicehub/email_admin_new_user.txt @@ -0,0 +1,4 @@ +A new user has been registered. These are your data +Name: {{ name }} +Telephone: {{ telephone }} +Email: {{ email }} diff --git a/ereuse_devicehub/templates/ereuse_devicehub/email_validation.txt b/ereuse_devicehub/templates/ereuse_devicehub/email_validation.txt new file mode 100644 index 00000000..daeaf7fd --- /dev/null +++ b/ereuse_devicehub/templates/ereuse_devicehub/email_validation.txt @@ -0,0 +1,2 @@ +Hello, you are register in Usody.com +Please for activate your account click in the next address: {{ url }} diff --git a/ereuse_devicehub/templates/ereuse_devicehub/user_registration.html b/ereuse_devicehub/templates/ereuse_devicehub/user_registration.html index a2dfafbf..1e969a97 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/user_registration.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/user_registration.html @@ -91,7 +91,7 @@

- You have account? Create an account + You have account? do Login