diff --git a/ihatemoney/api/common.py b/ihatemoney/api/common.py index 923a5391..bc35ac93 100644 --- a/ihatemoney/api/common.py +++ b/ihatemoney/api/common.py @@ -5,6 +5,7 @@ from flask_restful import Resource, abort from werkzeug.security import check_password_hash from wtforms.fields.core import BooleanField +from ihatemoney.emails import send_creation_email from ihatemoney.forms import EditProjectForm, MemberForm, ProjectForm, get_billform_for from ihatemoney.models import Bill, Person, Project, db @@ -55,6 +56,7 @@ class ProjectsHandler(Resource): project = form.save() db.session.add(project) db.session.commit() + send_creation_email(project) return project.id, 201 return form.errors, 400 @@ -75,6 +77,7 @@ class ProjectHandler(Resource): if form.validate() and current_app.config.get("ALLOW_PUBLIC_PROJECT_CREATION"): form.update(project) db.session.commit() + send_creation_email(project) return "UPDATED" return form.errors, 400 diff --git a/ihatemoney/emails.py b/ihatemoney/emails.py new file mode 100644 index 00000000..46d996dc --- /dev/null +++ b/ihatemoney/emails.py @@ -0,0 +1,18 @@ +from flask import g +from flask_babel import gettext as _ +from flask_mail import Message + +from ihatemoney.utils import render_localized_template, send_email + + +def send_creation_email(project): + g.project = project + message_title = _( + "You have just created '%(project)s' " "to share your expenses", + project=project.name, + ) + + message_body = render_localized_template("reminder_mail", project=project) + + msg = Message(message_title, body=message_body, recipients=[project.contact_email]) + return send_email(msg) diff --git a/ihatemoney/templates/reminder_mail.en.j2 b/ihatemoney/templates/reminder_mail.en.j2 index 1552c37b..7ab071a9 100644 --- a/ihatemoney/templates/reminder_mail.en.j2 +++ b/ihatemoney/templates/reminder_mail.en.j2 @@ -1,9 +1,9 @@ Hi, -You have just (or someone else using your email address) created the project "{{ g.project.name }}" to share your expenses. +You have just (or someone else using your email address) created the project "{{ project.name }}" to share your expenses. -You can access it here: {{ url_for(".list_bills", _external=True) }} (the identifier is {{ g.project.id }}). +You can access it here: {{ url_for("main.list_bills", _external=True) }} (the identifier is {{ project.id }}). If you want to share this project with your friends, you can share the identifier and the shared password with them or send them invitations with the following link: -{{ url_for(".invite", _external=True) }} +{{ url_for("main.invite", _external=True) }} Enjoy, diff --git a/ihatemoney/templates/reminder_mail.fr.j2 b/ihatemoney/templates/reminder_mail.fr.j2 index 1a7a0970..11e0e149 100644 --- a/ihatemoney/templates/reminder_mail.fr.j2 +++ b/ihatemoney/templates/reminder_mail.fr.j2 @@ -1,9 +1,9 @@ Salut, -Vous venez de créer le projet "{{ g.project.name }}" pour partager vos dépenses. +Vous venez de créer le projet "{{ project.name }}" pour partager vos dépenses. -Vous pouvez y accéder ici : {{ url_for(".list_bills", _external=True) }} (l'identifiant est {{ g.project.id }}). +Vous pouvez y accéder ici : {{ url_for("main.list_bills", _external=True) }} (l'identifiant est {{ project.id }}). Si vous voulez partager ce projet avec vos amis, vous pouvez soit leur transmettre l'identifiant et le code d'accès, soit leur envoyer une invitation personnelle grâce au lien suivant : -{{ url_for(".invite", _external=True) }} +{{ url_for("main.invite", _external=True) }} Faites-en bon usage ! diff --git a/ihatemoney/tests/api_test.py b/ihatemoney/tests/api_test.py index ad01cb8f..d9f06aa4 100644 --- a/ihatemoney/tests/api_test.py +++ b/ihatemoney/tests/api_test.py @@ -100,8 +100,14 @@ class APITestCase(IhatemoneyTestCase): ) # create it - resp = self.api_create("raclette") - self.assertTrue(201, resp.status_code) + with self.app.mail.record_messages() as outbox: + + resp = self.api_create("raclette") + self.assertTrue(201, resp.status_code) + + # Check that email messages have been sent. + self.assertEqual(len(outbox), 1) + self.assertEqual(outbox[0].recipients, ["raclette@notmyidea.org"]) # create it twice should return a 400 resp = self.api_create("raclette") diff --git a/ihatemoney/tests/budget_test.py b/ihatemoney/tests/budget_test.py index 07f337aa..92618594 100644 --- a/ihatemoney/tests/budget_test.py +++ b/ihatemoney/tests/budget_test.py @@ -206,7 +206,8 @@ class BudgetTestCase(IhatemoneyTestCase): }, follow_redirects=True, ) - # an email is sent to the owner with a reminder of the password + + # An email is sent to the owner with a reminder of the password. self.assertEqual(len(outbox), 1) self.assertEqual(outbox[0].recipients, ["raclette@notmyidea.org"]) self.assertIn( diff --git a/ihatemoney/web.py b/ihatemoney/web.py index fd9bafa2..f09cb8ae 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -37,6 +37,7 @@ from werkzeug.exceptions import NotFound from werkzeug.security import check_password_hash, generate_password_hash from ihatemoney.currency_convertor import CurrencyConverter +from ihatemoney.emails import send_creation_email from ihatemoney.forms import ( AdminAuthenticationForm, AuthenticationForm, @@ -320,18 +321,7 @@ def create_project(): # send reminder email g.project = project - - message_title = _( - "You have just created '%(project)s' " "to share your expenses", - project=g.project.name, - ) - - message_body = render_localized_template("reminder_mail") - - msg = Message( - message_title, body=message_body, recipients=[project.contact_email] - ) - success = send_email(msg) + success = send_creation_email(project) if success: flash( _("A reminder email has just been sent to you"), category="success"