Send an email when the project is created via the API. (#938)

This commit is contained in:
Alexis Metaireau 2021-11-25 00:49:17 +01:00 committed by GitHub
parent 1698841f6d
commit ef3944ccad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 21 deletions

View file

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

18
ihatemoney/emails.py Normal file
View file

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

View file

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

View file

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

View file

@ -100,9 +100,15 @@ class APITestCase(IhatemoneyTestCase):
)
# create it
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")

View file

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

View file

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