Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add configurable rules for where to send report emails.

  • Loading branch information...
commit 0c63d4ef9907842c33f6d2a8315b7634c724e943 1 parent 95900eb
@visiblegovernment authored
View
35 mainapp/emailrules.py
@@ -0,0 +1,35 @@
+
+class EmailRuleBehaviour(object):
+ def get_email(self,report,email_rule):
+ return(None)
+
+ def describe(self, email_rule):
+ return("")
+
+class ToCouncillor(EmailRuleBehaviour):
+ def get_email(self, report, email_rule):
+ return( report.ward.councillor.email )
+
+ def describe(self, email_rule ):
+ return("Send Reports to Councillor's Email Address")
+
+
+class MatchingCategoryClass(EmailRuleBehaviour):
+ def get_email(self,report, email_rule):
+ if report.category.category_class == email_rule.category_class:
+ return( email_rule.email )
+ else:
+ return( None )
+
+ def describe(self,email_rule):
+ return('Send All Reports Matching Category Class %s To %s' % (email_rule.category_class.name_en, email_rule.email))
+
+class NotMatchingCategoryClass(EmailRuleBehaviour):
+ def get_email(self,report, email_rule):
+ if report.category.category_class != email_rule.category_class:
+ return( email_rule.email )
+ else:
+ return( None )
+
+ def describe(self,email_rule):
+ return('Send All Reports Not Matching Category Class %s To %s' % (email_rule.category_class.name_en, email_rule.email))
View
53 mainapp/fixtures/test_email_rules.json
@@ -0,0 +1,53 @@
+[
+ {
+ "pk": 2,
+ "model": "mainapp.city",
+ "fields": {
+ "name": "TestCityWithoutEmail",
+ "province": 1,
+ "email": ""
+ }
+ },
+ {
+ "pk": 3,
+ "model": "mainapp.city",
+ "fields": {
+ "name": "TestCityWithEmail",
+ "province": 1,
+ "email": "311email@city.com"
+ }
+ },
+
+ {
+ "pk": 7,
+ "model": "mainapp.councillor",
+ "fields": {
+ "phone": "514-513-0999",
+ "first_name": "Angus",
+ "last_name": "Arthur-Doyle",
+ "fax": null,
+ "email": "councillor_email@testward1.com"
+ }
+ },
+ {
+ "pk": 1,
+ "model": "mainapp.ward",
+ "fields": {
+ "city": 2,
+ "name": "WardInCityWithNo311Email",
+ "councillor": 7,
+ "number": 1
+ }
+ },
+ {
+ "pk": 2,
+ "model": "mainapp.ward",
+ "fields": {
+ "city": 3,
+ "name": "WardInCityWith311Email",
+ "councillor": 7,
+ "number": 1
+ }
+ }
+
+]
View
90 mainapp/tests.py
@@ -0,0 +1,90 @@
+"""
+"""
+
+from django.test import TestCase
+from mainapp.models import Report,ReportUpdate,EmailRule, City, Ward,ReportCategory,ReportCategoryClass
+
+
+class EmailRuleTestBase(TestCase):
+ fixtures = ['test_email_rules.json']
+
+ def setUp(self):
+ # these are from the fixtures file.
+ self.test_categoryclass = ReportCategoryClass.objects.get(name_en='Parks')
+ self.test_category = ReportCategory.objects.get(name_en='Broken or Damaged Equipment/Play Structures')
+ self.not_test_category = ReportCategory.objects.get(name_en='Damaged Curb')
+
+ self.test_city = City.objects.get(name='TestCityWithoutEmail')
+ self.test_ward = Ward.objects.get(name = 'WardInCityWithNo311Email')
+ self.test_report = Report(ward=self.test_ward,category=self.test_category)
+
+class TestNoRules(EmailRuleTestBase):
+
+ def test(self):
+ self.failUnlessEqual( self.test_ward.get_emails(self.test_report), [] )
+
+
+class TestNoRulesWCityEmail(EmailRuleTestBase):
+
+ def test(self):
+ ward_w_email = Ward.objects.get(name='WardInCityWith311Email')
+ self.failUnlessEqual( ward_w_email.get_emails(self.test_report),[ ward_w_email.city.email ] )
+
+class TestToCouncillor(EmailRuleTestBase):
+
+ def test(self):
+ rule = EmailRule( rule=EmailRule.TO_COUNCILLOR, city = self.test_city )
+ rule.save()
+ self.failUnlessEqual( self.test_ward.get_emails(self.test_report), [self.test_report.ward.councillor.email] )
+
+
+
+class TestMatchingCategoryClass(EmailRuleTestBase):
+
+ def test(self):
+ email = 'parks@city.ca'
+ rule = EmailRule( rule=EmailRule.MATCHING_CATEGORY_CLASS, city = self.test_city, category_class = self.test_categoryclass, email=email )
+ rule.save()
+ self.failUnlessEqual( self.test_ward.get_emails(self.test_report), [email] )
+ report2 = Report(ward=self.test_ward,category=self.not_test_category)
+ self.failUnlessEqual( self.test_ward.get_emails(report2), [] )
+
+
+class TestNotMatchingCategoryClass(EmailRuleTestBase):
+
+ def test(self):
+ email = 'parks@city.ca'
+ rule = EmailRule( rule=EmailRule.NOT_MATCHING_CATEGORY_CLASS, city = self.test_city, category_class = self.test_categoryclass, email=email )
+ rule.save()
+ self.failUnlessEqual( self.test_ward.get_emails(self.test_report), [] )
+ report2 = Report(ward=self.test_ward,category=self.not_test_category)
+ self.failUnlessEqual( self.test_ward.get_emails(report2), [email] )
+
+
+class TestCharlottetownRules(EmailRuleTestBase):
+
+ def test(self):
+
+ # simulate Charlottetown's cases, where we want WARD councillor emails,
+ # in addition, send everything except parks to go to email_A,
+ # and all parks reports to go to email_B
+ parks_category_class = ReportCategoryClass.objects.get(name_en='Parks')
+ parks_category = ReportCategory.objects.get(name_en='Lights Malfunctioning in Park')
+ not_parks_category = ReportCategory.objects.get(name_en='Damaged Curb')
+
+ councillor_rule = EmailRule( rule=EmailRule.TO_COUNCILLOR, city = self.test_city )
+ councillor_rule.save()
+
+ not_parks_email = 'not_parks@ward1.com'
+ parks_email = 'parks@ward1.com'
+ rule1 = EmailRule( rule=EmailRule.MATCHING_CATEGORY_CLASS, city = self.test_city, category_class = parks_category_class, email=parks_email )
+ rule1.save()
+ rule2 = EmailRule( rule=EmailRule.NOT_MATCHING_CATEGORY_CLASS, city = self.test_city, category_class = parks_category_class, email=not_parks_email )
+ rule2.save()
+
+ parks_report = Report(ward=self.test_ward,category = parks_category )
+ self.failUnlessEqual( self.test_ward.get_emails(parks_report), [u"councillor_email@testward1.com", u'parks@ward1.com' ] )
+
+ not_parks_report = Report(ward=self.test_ward,category = not_parks_category )
+ self.failUnlessEqual( self.test_ward.get_emails(not_parks_report), [u"councillor_email@testward1.com", u'not_parks@ward1.com' ] )
+
Please sign in to comment.
Something went wrong with that request. Please try again.