Skip to content

Commit

Permalink
Send an email when the project is created via the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
almet committed Nov 23, 2021
1 parent 68552a6 commit cbc3f36
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
3 changes: 3 additions & 0 deletions ihatemoney/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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

Expand Down Expand Up @@ -55,6 +56,7 @@ def post(self):
project = form.save()
db.session.add(project)
db.session.commit()
send_creation_email(project)
return project.id, 201
return form.errors, 400

Expand All @@ -75,6 +77,7 @@ def put(self, project):
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

Expand Down
18 changes: 18 additions & 0 deletions ihatemoney/emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from flask_babel import gettext as _
from flask_mail import Message
from flask import g

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)
6 changes: 3 additions & 3 deletions ihatemoney/templates/reminder_mail.en.j2
Original file line number Diff line number Diff line change
@@ -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,
6 changes: 3 additions & 3 deletions ihatemoney/templates/reminder_mail.fr.j2
Original file line number Diff line number Diff line change
@@ -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 !
10 changes: 8 additions & 2 deletions ihatemoney/tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,14 @@ def test_project(self):
)

# 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")
Expand Down
3 changes: 2 additions & 1 deletion ihatemoney/tests/budget_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ def test_project_creation(self):
},
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(
Expand Down
14 changes: 2 additions & 12 deletions ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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,
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit cbc3f36

Please sign in to comment.