Parcourir la source

Send a notification email on contribution saving

Jocelyn Delande il y a 9 ans
Parent
commit
a3a8ffa3c7

+ 1 - 0
wifiwithme/apps/contribmap/templates/contribmap/mails/new_contrib_notice.subject

@@ -0,0 +1 @@
+[wifi-with-me] nouvelle demande de {{ contrib.name }}

+ 7 - 0
wifiwithme/apps/contribmap/templates/contribmap/mails/new_contrib_notice.txt

@@ -0,0 +1,7 @@
+Nouvelle demande de la part de {{ contrib.name }}
+
+À retrouver sur {{ site_url }}
+
+Bien à toi,
+
+Wifi-with-me

+ 33 - 1
wifiwithme/apps/contribmap/tests.py

@@ -1,7 +1,8 @@
 import json
 
+from django.core import mail
 from django.contrib.auth.models import User
-from django.test import TestCase, Client
+from django.test import TestCase, Client, override_settings
 
 from contribmap.models import Contrib
 
@@ -100,6 +101,37 @@ class TestViews(APITestCase):
         response = self.client.get('/map/private.json')
         self.assertEqual(response.status_code, 200)
 
+    @override_settings(NOTIFICATION_EMAILS=['foo@example.com'])
+    def test_add_contrib_sends_email(self):
+        response = self.client.post('/map/contribute', {
+            'roof': True,
+            'privacy_place_details': True,
+            'privacy_coordinates': True,
+            'phone': '0202020202',
+            'orientations': 'N',
+            'orientations': 'NO',
+            'orientations': 'O',
+            'orientations': 'SO',
+            'orientations': 'S',
+            'orientations': 'SE',
+            'orientations': 'E',
+            'orientations': 'NE',
+            'orientation': 'all',
+            'name': 'JohnCleese',
+            'longitude': -1.553621,
+            'latitude': 47.218371,
+            'floor_total': '2',
+            'floor': 1,
+            'email': 'coucou@example.com',
+            'contrib_type': 'connect',
+            'connect_local': 'on',
+        })
+        self.assertEqual(response.status_code, 302)
+
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertIn('JohnCleese', mail.outbox[0].subject)
+        self.assertIn('JohnCleese', mail.outbox[0].body)
+
 
 class TestDataImport(TestCase):
     fixtures = ['bottle_data.yaml']

+ 23 - 1
wifiwithme/apps/contribmap/views.py

@@ -1,6 +1,10 @@
+from django.conf import settings
+
 from django.core.urlresolvers import reverse
+from django.core.mail import send_mail
 from django.http import JsonResponse, HttpResponseForbidden
 from django.shortcuts import render, redirect
+from django.template.loader import get_template
 from django.views.generic import View
 
 from .forms import PublicContribForm
@@ -13,7 +17,25 @@ def add_contrib(request):
     elif request.method == 'POST':
         form = PublicContribForm(request.POST)
         if form.is_valid():
-            form.save()
+            contrib = form.save()
+
+            # Send notification email
+            if len(settings.NOTIFICATION_EMAILS) > 0:
+                context = {
+                    'site_url': settings.SITE_URL,
+                    'contrib': contrib,
+                }
+                subject = get_template(
+                    'contribmap/mails/new_contrib_notice.subject')
+                body = get_template(
+                    'contribmap/mails/new_contrib_notice.txt')
+                send_mail(
+                    subject.render(context),
+                    body.render(context),
+                    settings.DEFAULT_FROM_EMAIL,
+                    settings.NOTIFICATION_EMAILS,
+                )
+
             return redirect(reverse('thanks'))
     return render(request, 'contribmap/wifi-form.html', {
         'form': form,

+ 6 - 0
wifiwithme/settings/base.py

@@ -129,3 +129,9 @@ STATIC_URL = URL_PREFIX + 'assets/'
 STATICFILES_DIRS = [
     os.path.join(BASE_DIR, "static"),
 ]
+
+# Where to send notification after a point is added via the form.
+
+NOTIFICATION_EMAILS = []
+
+SITE_URL = 'http://example.com/wifi'