Skip to content

Commit

Permalink
✨ Send mails contributions (managers and senders)
Browse files Browse the repository at this point in the history
  • Loading branch information
LePetitTim committed Jul 24, 2023
1 parent 3f41127 commit 8df6464
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
22 changes: 22 additions & 0 deletions georiviere/contribution/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import logging

from django.conf import settings
from django.contrib.gis.db import models
from django.core.mail import mail_managers
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _

from mapentity.models import MapEntityMixin
Expand All @@ -17,6 +21,8 @@
from georiviere.studies.models import Study
from georiviere.watershed.mixins import WatershedPropertiesMixin

logger = logging.getLogger(__name__)


class SeverityType(models.Model):
label = models.CharField(max_length=128, verbose_name=_("Label"), unique=True)
Expand Down Expand Up @@ -97,6 +103,22 @@ def category_display(self):
s = '<span class="badge badge-success" title="%s">&#x2606;</span> ' % _("Published") + s
return s

def send_report_to_managers(self, template_name="contribution/report_email.txt"):
subject = _("Feedback from {email}").format(email=self.email_author)
message = render_to_string(template_name, {"contribution": self})
mail_managers(subject, message, fail_silently=False)

Check warning on line 109 in georiviere/contribution/models.py

View check run for this annotation

Codecov / codecov/patch

georiviere/contribution/models.py#L109

Added line #L109 was not covered by tests

def try_send_report_to_managers(self):
try:
self.send_report_to_managers()
except Exception as e:
logger.error("Email could not be sent to managers.")
logger.exception(e) # This sends an email to admins :)

def save(self, *args, **kwargs):
super().save(*args, **kwargs) # Contribution updates should do nothing more
self.try_send_report_to_managers()


class LandingType(models.Model):
label = models.CharField(max_length=128, verbose_name=_("Label"), unique=True)
Expand Down
11 changes: 11 additions & 0 deletions georiviere/portal/tests/test_views/test_contribution.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core import mail
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
Expand Down Expand Up @@ -32,6 +33,7 @@ def test_contribution_structure(self):
self.assertSetEqual(set(response.json().keys()), {'type', 'required', 'properties', 'allOf'})

def test_contribution_landscape_element(self):
self.assertEqual(len(mail.outbox), 0)
url = reverse('api_portal:contributions-list',
kwargs={'portal_pk': self.portal.pk, 'lang': 'fr'})
response = self.client.post(url, data={"geom": "POINT(0 0)",
Expand All @@ -44,8 +46,10 @@ def test_contribution_landscape_element(self):
landscape_element = contribution.landscape_element
self.assertEqual(contribution.email_author, 'x@x.x')
self.assertEqual(landscape_element.get_type_display(), 'Doline')
self.assertEqual(len(mail.outbox), 1)

def test_contribution_quality(self):
self.assertEqual(len(mail.outbox), 0)
url = reverse('api_portal:contributions-list',
kwargs={'portal_pk': self.portal.pk, 'lang': 'fr'})
response = self.client.post(url, data={"geom": "POINT(0 0)",
Expand All @@ -60,8 +64,10 @@ def test_contribution_quality(self):
self.assertEqual(quality.nature_pollution.label, 'Baz')
self.assertEqual(contribution.email_author, 'x@x.x')
self.assertEqual(quality.get_type_display(), 'Pollution')
self.assertEqual(len(mail.outbox), 1)

def test_contribution_quantity(self):
self.assertEqual(len(mail.outbox), 0)
url = reverse('api_portal:contributions-list',
kwargs={'portal_pk': self.portal.pk, 'lang': 'fr', 'format': 'json'})
response = self.client.post(url, data={"geom": "POINT(0 0)",
Expand All @@ -74,8 +80,10 @@ def test_contribution_quantity(self):
quantity = contribution.quantity
self.assertEqual(contribution.email_author, 'x@x.x')
self.assertEqual(quantity.get_type_display(), 'A sec')
self.assertEqual(len(mail.outbox), 1)

def test_contribution_faunaflora(self):
self.assertEqual(len(mail.outbox), 0)
url = reverse('api_portal:contributions-list',
kwargs={'portal_pk': self.portal.pk, 'lang': 'fr', 'format': 'json'})
response = self.client.post(url, data={"geom": "POINT(4 43.5)",
Expand All @@ -90,8 +98,10 @@ def test_contribution_faunaflora(self):
fauna_flora = contribution.fauna_flora
self.assertEqual(contribution.email_author, 'x@x.x')
self.assertEqual(fauna_flora.get_type_display(), 'Espèce invasive')
self.assertEqual(len(mail.outbox), 1)

def test_contribution_potential_damages(self):
self.assertEqual(len(mail.outbox), 0)
url = reverse('api_portal:contributions-list',
kwargs={'portal_pk': self.portal.pk, 'lang': 'fr', 'format': 'json'})
response = self.client.post(url, data={"geom": "POINT(4 42.5)",
Expand All @@ -104,6 +114,7 @@ def test_contribution_potential_damages(self):
potential_damage = contribution.potential_damage
self.assertEqual(contribution.email_author, 'x@x.x')
self.assertEqual(potential_damage.get_type_display(), 'Éboulements')
self.assertEqual(len(mail.outbox), 1)

def test_contribution_category_does_not_exist(self):
url = reverse('api_portal:contributions-list',
Expand Down
19 changes: 19 additions & 0 deletions georiviere/portal/views/contribution.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
from PIL import Image

Expand All @@ -7,9 +8,11 @@
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.core.files import File
from django.core.mail import send_mail
from django.db.models import F, Q
from django.contrib.gis.db.models.functions import Transform
from django.utils import translation
from django.utils.translation import gettext_lazy as _

from djangorestframework_camel_case.render import CamelCaseJSONRenderer

Expand Down Expand Up @@ -115,4 +118,20 @@ def create(self, request, *args, **kwargs):
logger.error(
f"Failed to convert attachment {name}{extension} for report {response.data.get('id')}: " + str(
e))
if settings.SEND_REPORT_ACK and response.status_code == 201:
send_mail(
_("Georiviere : Signal a mistake"),
_(
"""Hello,
We acknowledge receipt of your contribution, thank you for your interest in Georiviere.
Best regards,
The Georiviere Team
http://georiviere.fr"""
),
settings.DEFAULT_FROM_EMAIL,
[json.loads(request.data.get("properties")).get('email_author'), ],
)
return response
1 change: 1 addition & 0 deletions georiviere/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def construct_relative_path_mock(current_template_name, relative_name):
PHONE_NUMBER_DOCUMENT_REPORT = ''
WEBSITE_DOCUMENT_REPORT = ''
URL_DOCUMENT_REPORT = ''
SEND_REPORT_ACK = True

# sensitivity

Expand Down

0 comments on commit 8df6464

Please sign in to comment.