Commit 301fe12a authored by Damien Lespiau's avatar Damien Lespiau
Browse files

testresult: Add To: and Cc: lists



This commit adds a list of emails to always add to To: and Cc: fields
when sending out result emails.

There's also an option that will only send test result emails to people
in the To: list.
Signed-off-by: default avatarDamien Lespiau <damien.lespiau@intel.com>
parent cb1346f1
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('patchwork', '0009_test_results_mail'),
]
operations = [
migrations.AddField(
model_name='test',
name='mail_cc_list',
field=models.CharField(help_text=b'Comma separated list of emails', max_length=255, null=True, blank=True),
),
migrations.AddField(
model_name='test',
name='mail_to_list',
field=models.CharField(help_text=b'Comma separated list of emails', max_length=255, null=True, blank=True),
),
migrations.AlterField(
model_name='test',
name='mail_recipient',
field=models.SmallIntegerField(default=0, choices=[(0, b'none'), (1, b'submitter'), (2, b'mailing list'), (3, b'recipient list')]),
),
]
......@@ -64,6 +64,8 @@ class Person(models.Model):
verbose_name_plural = 'People'
def get_comma_separated_field(value):
if not value:
return []
tags = [v.strip() for v in value.split(',')]
tags = [tag for tag in tags if tag]
return tags
......@@ -583,10 +585,13 @@ class Test(models.Model):
RECIPIENT_SUBMITTER = 1
# send mail to submitter and mailing-list in Cc
RECIPIENT_MAILING_LIST = 2
# send mail to the addresses listed in the mail_to_list field only
RECIPIENT_TO_LIST = 3
RECIPIENT_CHOICES = (
(RECIPIENT_NONE, 'none'),
(RECIPIENT_SUBMITTER, 'submitter'),
(RECIPIENT_MAILING_LIST, 'mailing list'),
(RECIPIENT_TO_LIST, 'recipient list'),
)
# send result mail on any state (but pending)
......@@ -602,12 +607,24 @@ class Test(models.Model):
name = models.CharField(max_length=255)
mail_recipient = models.SmallIntegerField(choices=RECIPIENT_CHOICES,
default=RECIPIENT_NONE)
# email addresses in these lists are always added to the To: and Cc:fields,
# unless we don't want to send any email at all.
mail_to_list = models.CharField(max_length=255, blank=True, null=True,
help_text='Comma separated list of emails')
mail_cc_list = models.CharField(max_length=255, blank=True, null=True,
help_text='Comma separated list of emails')
mail_condition = models.SmallIntegerField(choices=CONDITION_CHOICES,
default=CONDITION_ALWAYS)
class Meta:
unique_together = [('project', 'name')]
def get_to_list(self):
return get_comma_separated_field(self.mail_to_list)
def get_cc_list(self):
return get_comma_separated_field(self.mail_cc_list)
def __unicode__(self):
return self.name
......
......@@ -454,7 +454,8 @@ class TestResultTest(APITestBase):
self.assertEqual(len(mail.outbox), 0)
self._cleanup_tests()
def _configure_test(self, url, test_name, recipient, condition):
def _configure_test(self, url, test_name, recipient, condition,
to_list=None, cc_list=None):
"""Create test_name and configure it"""
self._post_result(url, test_name, 'pending')
tests = Test.objects.all()
......@@ -462,6 +463,8 @@ class TestResultTest(APITestBase):
test = tests[0]
test.mail_recipient = recipient
test.mail_condition = condition
test.mail_to_list = to_list
test.mail_cc_list = cc_list
test.save()
def testMailHeaders(self):
......@@ -509,6 +512,93 @@ class TestResultTest(APITestBase):
self.assertEqual(email.to, [self.series.submitter.email_name()])
self.assertEqual(email.cc, [self.project.listemail])
mail.outbox = []
to_list = 'Damien Lespiau <damien.lespiau@intel.com>,'\
'Daniel Vetter <daniel@ffwll.ch>'
self._configure_test(url, 'super test',
Test.RECIPIENT_NONE, Test.CONDITION_ALWAYS,
to_list=to_list)
self._post_result(url, 'super test', 'success')
self.assertEqual(len(mail.outbox), 0)
self._configure_test(url, 'super test',
Test.RECIPIENT_SUBMITTER, Test.CONDITION_ALWAYS,
to_list=to_list)
self._post_result(url, 'super test', 'success')
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEqual(email.to, [self.series.submitter.email_name()] +
to_list.split(','))
self.assertEqual(email.cc, [])
mail.outbox = []
self._configure_test(url, 'super test',
Test.RECIPIENT_MAILING_LIST, Test.CONDITION_ALWAYS,
to_list=to_list)
self._post_result(url, 'super test', 'success')
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEqual(email.to, [self.series.submitter.email_name()] +
to_list.split(','))
self.assertEqual(email.cc, [self.project.listemail])
mail.outbox = []
self._configure_test(url, 'super test',
Test.RECIPIENT_TO_LIST, Test.CONDITION_ALWAYS,
to_list=to_list)
self._post_result(url, 'super test', 'success')
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEqual(email.to, to_list.split(','))
self.assertEqual(email.cc, [])
mail.outbox = []
cc_list = 'ville.syrjala@linux.intel.com'
self._configure_test(url, 'super test',
Test.RECIPIENT_NONE, Test.CONDITION_ALWAYS,
cc_list=cc_list)
self._post_result(url, 'super test', 'success')
self.assertEqual(len(mail.outbox), 0)
self._configure_test(url, 'super test',
Test.RECIPIENT_SUBMITTER, Test.CONDITION_ALWAYS,
cc_list=cc_list)
self._post_result(url, 'super test', 'success')
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEqual(email.to, [self.series.submitter.email_name()])
self.assertEqual(email.cc, cc_list.split(','))
mail.outbox = []
self._configure_test(url, 'super test',
Test.RECIPIENT_MAILING_LIST, Test.CONDITION_ALWAYS,
cc_list=cc_list)
self._post_result(url, 'super test', 'success')
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEqual(email.to, [self.series.submitter.email_name()])
self.assertEqual(email.cc, [self.project.listemail] +
cc_list.split(','))
mail.outbox = []
self._configure_test(url, 'super test',
Test.RECIPIENT_TO_LIST, Test.CONDITION_ALWAYS,
to_list=to_list, cc_list=cc_list)
self._post_result(url, 'super test', 'success')
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEqual(email.to, to_list.split(','))
self.assertEqual(email.cc, cc_list.split(','))
mail.outbox = []
self._cleanup_tests()
def testMailCondition(self):
......
......@@ -274,6 +274,11 @@ class ResultMixin(object):
to.append(check_obj.submitter.email_name())
cc.append(check_obj.project.listemail)
if instance.test.mail_recipient != Test.RECIPIENT_NONE and \
(instance.test.mail_to_list or instance.test.mail_cc_list):
to += instance.test.get_to_list()
cc += instance.test.get_cc_list()
if to:
# never send mail on pending
if instance.state == TestResult.STATE_PENDING:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment