From c508f7743eb1634f11f310252a20922e115393d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Luiz=20Lorencetti?= Date: Thu, 16 Feb 2017 22:51:14 -0200 Subject: [PATCH] An email will be sent to the user with a notice about the pet registration deactivation --- pets/meupet/models.py | 3 +++ pets/meupet/services.py | 14 ++++++++++++ .../templates/meupet/deactivate_email.txt | 8 +++++++ pets/meupet/tests/test_model_pet.py | 10 +++++++++ pets/meupet/tests/test_services.py | 22 ++++++++++++++++++- 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 pets/meupet/templates/meupet/deactivate_email.txt diff --git a/pets/meupet/models.py b/pets/meupet/models.py index 6a6abd4..23eaa0b 100644 --- a/pets/meupet/models.py +++ b/pets/meupet/models.py @@ -180,6 +180,9 @@ def activate(self): self.save() def deactivate(self): + if not services.send_deactivate_email(self): + return + self.active = False self.save(update_modified=False) diff --git a/pets/meupet/services.py b/pets/meupet/services.py index ee5eed2..121d8aa 100644 --- a/pets/meupet/services.py +++ b/pets/meupet/services.py @@ -34,3 +34,17 @@ def send_request_action_email(pet): } return send_email(subject, to, template_name, context) + + +def send_deactivate_email(pet): + subject = _('Deactivation of pet registration') + to = pet.owner.email + template_name = 'meupet/deactivate_email.txt' + current_site = Site.objects.get_current() + + context = { + 'username': pet.owner.first_name, + 'site_name': current_site.name, + } + + return send_email(subject, to, template_name, context) diff --git a/pets/meupet/templates/meupet/deactivate_email.txt b/pets/meupet/templates/meupet/deactivate_email.txt new file mode 100644 index 0000000..5fc5f47 --- /dev/null +++ b/pets/meupet/templates/meupet/deactivate_email.txt @@ -0,0 +1,8 @@ +{% load i18n %}{% blocktrans %}Hello, {{ username }}.{% endblocktrans %} + +{% blocktrans %}Your pet registration didn't received any updates in the last few months, so it's been deactivated.{% endblocktrans %} + +{% blocktrans %}You can make a new registration if necessary.{% endblocktrans %} + +{% trans "Best regards," %} +{{ site_name }} \ No newline at end of file diff --git a/pets/meupet/tests/test_model_pet.py b/pets/meupet/tests/test_model_pet.py index 9b99dc8..b4da380 100644 --- a/pets/meupet/tests/test_model_pet.py +++ b/pets/meupet/tests/test_model_pet.py @@ -69,6 +69,16 @@ def test_request_action_from_user(self, mock_send_email): mock_send_email.assert_called_once_with(pet) + @patch('meupet.services.send_deactivate_email') + def test_deactivate_send_email(self, mock_send_email): + """ + Deactivate should call the send_deactivate_email method + """ + pet = mommy.make(Pet) + pet.deactivate() + + mock_send_email.assert_called_once_with(pet) + def test_request_sent_saved(self): """Should set the request_sent date in the pet and keep modified date""" pet = mommy.make(Pet) diff --git a/pets/meupet/tests/test_services.py b/pets/meupet/tests/test_services.py index 16c92fa..327434a 100644 --- a/pets/meupet/tests/test_services.py +++ b/pets/meupet/tests/test_services.py @@ -11,7 +11,9 @@ class ServicesTest(TestCase): def test_request_action_from_user(self): - """Validate the information sent in the email""" + """ + Validate the information sent in the email + """ pet = mommy.make(Pet) pet.request_action() email = mail.outbox[0] @@ -32,3 +34,21 @@ def test_request_action_from_user(self): for expected in contents: with self.subTest(): self.assertIn(expected, email.body) + + def test_pet_deactivated(self): + """ + Validates the information present in the email + """ + pet = mommy.make(Pet) + pet.deactivate() + email = mail.outbox[0] + current_site = Site.objects.get_current() + + contents = [ + pet.owner.first_name, + current_site.name, + ] + + for expected in contents: + with self.subTest(): + self.assertIn(expected, email.body)