Repository: ciudadanointeligente/votainteligente-portal-electoral

Issue:
Que le avise a los staff cuando hay alguien actualiza su propuesta



Discussion:

Resolving patch:
diff --git a/popular_proposal/forms/forms.py b/popular_proposal/forms/forms.py
--- a/popular_proposal/forms/forms.py
+++ b/popular_proposal/forms/forms.py
@@ -9,6 +9,7 @@
 from .form_texts import TEXTS, TOPIC_CHOICES, WHEN_CHOICES
 from popolo.models import Area
 from collections import OrderedDict
+from votainteligente.send_mails import send_mails_to_staff
 
 
 class TextsFormMixin():
@@ -288,6 +289,7 @@ def save(self):
         self.temporary_data.overall_comments = self.overall_comments
         self.temporary_data.status = ProposalTemporaryData.Statuses.InOurSide
         self.temporary_data.save()
+        send_mails_to_staff({'temporary_data': self.temporary_data}, 'notify_staff_new_proposal_update')
         return self.temporary_data
 
     def get_overall_comments(self):
diff --git a/popular_proposal/views.py b/popular_proposal/views.py
--- a/popular_proposal/views.py
+++ b/popular_proposal/views.py
@@ -26,6 +26,7 @@
 from popular_proposal.forms import ProposalAreaFilterForm
 from popular_proposal.filters import ProposalAreaFilter
 from votainteligente.view_mixins import EmbeddedViewBase
+from votainteligente.send_mails import send_mails_to_staff
 
 
 class ProposalCreationView(FormView):
@@ -169,6 +170,7 @@ def done(self, form_list, **kwargs):
                                                       area=self.area,
                                                       data=data)
         t_data.notify_new()
+        send_mails_to_staff({'temporary_data': t_data}, 'notify_staff_new_proposal')
         return render_to_response('popular_proposal/wizard/done.html', {
             'popular_proposal': t_data,
             'area': self.area
@@ -209,6 +211,7 @@ def done(self, form_list, **kwargs):
         context.update({'popular_proposal': temporary_data,
                         'area': area
                         })
+        send_mails_to_staff({'temporary_data': temporary_data}, 'notify_staff_new_proposal')
         return render_to_response('popular_proposal/wizard/done.html',
                                   context)
 
diff --git a/votainteligente/send_mails.py b/votainteligente/send_mails.py
--- a/votainteligente/send_mails.py
+++ b/votainteligente/send_mails.py
@@ -2,6 +2,7 @@
 from django.template.loader import get_template
 from django.core.mail import EmailMessage
 from django.template import Context
+from django.contrib.auth.models import User
 from django.conf import settings
 
 def send_mail(context_dict, template_prefix, to=[], reply_to=None, from_email=settings.DEFAULT_FROM_EMAIL):
@@ -9,11 +10,17 @@ def send_mail(context_dict, template_prefix, to=[], reply_to=None, from_email=se
     template_prefix_dict = {'template_prefix': template_prefix}
     template_body = get_template('mails/%(template_prefix)s_body.html' % template_prefix_dict)
     body = template_body.render(context)
-    template_subject= get_template('mails/%(template_prefix)s_subject.html' % template_prefix_dict)
+    template_subject = get_template('mails/%(template_prefix)s_subject.html' % template_prefix_dict)
     subject = template_subject.render(context).replace('\n', '').replace('\r', '')
-    email = EmailMessage(subject, body, from_email,
-                        to)
+    email = EmailMessage(subject, body, from_email, to)
     if reply_to is not None:
         email.reply_to = [reply_to]
     email.send()
 
+
+def send_mails_to_staff(context_dict, template_prefix):
+    to = []
+    for u in User.objects.filter(is_staff=True):
+        to.append(u.email)
+    send_mail(context_dict, template_prefix, to=to)
+


Test patch:
diff --git a/backend_citizen/tests/views_tests.py b/backend_citizen/tests/views_tests.py
--- a/backend_citizen/tests/views_tests.py
+++ b/backend_citizen/tests/views_tests.py
@@ -9,6 +9,7 @@
 from backend_citizen.forms import UserChangeForm
 from backend_citizen.tests import BackendCitizenTestCaseBase, PASSWORD
 from backend_citizen.models import Organization
+from django.core import mail
 
 
 class BackendCitizenViewsTests(BackendCitizenTestCaseBase):
@@ -63,6 +64,13 @@ def test_temporary_promise_detail_view(self):
         temporary_data = ProposalTemporaryData.objects.get(id=temporary_data.id)
         self.assertEquals(temporary_data.data['solution'], data['solution'])
 
+        self.assertEquals(len(mail.outbox), 1)
+        the_mail = mail.outbox[0]
+        self.assertIn(self.feli.email, the_mail.to)
+        self.assertIn(self.feli.email, the_mail.to)
+        self.assertIn(str(temporary_data.id), the_mail.body)
+        self.assertIn(temporary_data.get_title(), the_mail.body)
+
     def test_brings_all_the_proposals_that_are_in_my_side(self):
         t_d1 = ProposalTemporaryData.objects.create(proposer=self.fiera,
                                                     area=self.arica,
diff --git a/backend_staff/tests/mails_for_staff_tests.py b/backend_staff/tests/mails_for_staff_tests.py
new file mode 100644
--- /dev/null
+++ b/backend_staff/tests/mails_for_staff_tests.py
@@ -0,0 +1,51 @@
+# coding=utf-8
+from popular_proposal.tests import ProposingCycleTestCaseBase as TestCase
+from votainteligente.send_mails import send_mails_to_staff
+from django.contrib.auth.models import User
+from popular_proposal.models import ProposalTemporaryData, PopularProposal
+from django.core import mail
+
+
+class MailsForStaffTestCase(TestCase):
+    def setUp(self):
+        super(MailsForStaffTestCase, self).setUp()
+        self.staff1 = User.objects.create_superuser(username='staff1',
+                                                    password='perrito',
+                                                    email='staff1@staffs.cl')
+        self.staff2 = User.objects.create_superuser(username='staff2',
+                                                    password='perrito',
+                                                    email='staff2@staffs.cl')
+        self.temporary_data = ProposalTemporaryData.objects.create(proposer=self.fiera,
+                                                                   area=self.arica,
+                                                                   data=self.data)
+        self.proposal = PopularProposal.objects.create(proposer=self.fiera,
+                                                       area=self.arica,
+                                                       data=self.data,
+                                                       title=u'This is a title',
+                                                       clasification=u'education'
+                                                       )
+
+    def test_send_mails_to_staff_when_new_comments(self):
+        context = {
+            'temporary_data': self.temporary_data,
+        }
+        send_mails_to_staff(context, 'notify_staff_new_proposal_update')
+        self.assertEquals(len(mail.outbox), 1)
+        the_mail = mail.outbox[0]
+        self.assertIn(self.staff1.email, the_mail.to)
+        self.assertIn(self.staff2.email, the_mail.to)
+        self.assertIn(str(self.temporary_data.id), the_mail.body)
+        self.assertIn(self.temporary_data.get_title(), the_mail.body)
+
+    def test_send_mails_to_staff_when_new_proposal(self):
+        context = {
+            'temporary_data': self.temporary_data,
+        }
+        send_mails_to_staff(context, 'notify_staff_new_proposal')
+        self.assertEquals(len(mail.outbox), 1)
+        the_mail = mail.outbox[0]
+        self.assertIn(self.staff1.email, the_mail.to)
+        self.assertIn(self.staff2.email, the_mail.to)
+        self.assertIn(str(self.proposal.id), the_mail.body)
+        self.assertIn(self.temporary_data.get_title(), the_mail.body)
+        self.assertIn(self.temporary_data.area.name, the_mail.subject)
diff --git a/popular_proposal/tests/wizard_tests.py b/popular_proposal/tests/wizard_tests.py
--- a/popular_proposal/tests/wizard_tests.py
+++ b/popular_proposal/tests/wizard_tests.py
@@ -148,7 +148,14 @@ def test_post_data_to_the_wizard(self):
         self.assertEquals(response.context['area'], self.arica)
         self.assertEquals(temporary_data.proposer, self.feli)
         self.assertEquals(temporary_data.area, self.arica)
-        self.assertEquals(len(mail.outbox), original_amount + 1)
+        self.assertEquals(len(mail.outbox), original_amount + 2)
+
+        the_mail = mail.outbox[original_amount + 1]
+        self.assertIn(self.fiera.email, the_mail.to)
+        self.assertIn(self.feli.email, the_mail.to)
+        self.assertIn(str(temporary_data.id), the_mail.body)
+        self.assertIn(temporary_data.get_title(), the_mail.body)
+        self.assertIn(temporary_data.area.name, the_mail.subject)
 
     def test_user_should_accept_terms_and_conditions(self):
         list_ = get_form_list()
@@ -194,4 +201,11 @@ def test_full_wizard(self):
         self.assertEquals(response.context['area'], self.arica)
         self.assertEquals(temporary_data.proposer, self.feli)
         self.assertEquals(temporary_data.area, self.arica)
-        self.assertEquals(len(mail.outbox), original_amount + 1)
+        self.assertEquals(len(mail.outbox), original_amount + 2)
+
+        the_mail = mail.outbox[original_amount + 1]
+        self.assertIn(self.fiera.email, the_mail.to)
+        self.assertIn(self.feli.email, the_mail.to)
+        self.assertIn(str(temporary_data.id), the_mail.body)
+        self.assertIn(temporary_data.get_title(), the_mail.body)
+        self.assertIn(temporary_data.area.name, the_mail.subject)
\ No newline at end of file