PEP-8 fixes for mail threading merge

This commit is contained in:
Garret Wassermann 2018-12-28 11:32:49 -05:00
parent 5f0d22a692
commit 43ed3ac773
5 changed files with 153 additions and 167 deletions

View File

@ -318,7 +318,7 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
previous_followup = queryset.first()
ticket = previous_followup.ticket
except FollowUp.DoesNotExist:
pass #play along. The header may be wrong
pass # play along. The header may be wrong
if previous_followup is None and ticket_id is not None:
try:
@ -331,12 +331,12 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
if ticket is None:
if not settings.QUEUE_EMAIL_BOX_UPDATE_ONLY:
ticket = Ticket.objects.create(
title = payload['subject'],
queue = queue,
submitter_email = sender_email,
created = now,
description = payload['body'],
priority = payload['priority'],
title=payload['subject'],
queue=queue,
submitter_email=sender_email,
created=now,
description=payload['body'],
priority=payload['priority'],
)
ticket.save()
logger.debug("Created new ticket %s-%s" % (ticket.queue.slug, ticket.id))
@ -350,12 +350,12 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
ticket.save()
f = FollowUp(
ticket = ticket,
title = _('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}),
date = now,
public = True,
comment = payload['body'],
message_id = message_id,
ticket=ticket,
title=_('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}),
date=now,
public=True,
comment=payload['body'],
message_id=message_id
)
if ticket.status == Ticket.REOPENED_STATUS:
@ -376,13 +376,13 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
new_ticket_ccs = []
new_ticket_ccs.append(create_ticket_cc(ticket, to_list + cc_list))
notifications_to_be_sent = [sender_email,]
notifications_to_be_sent = [sender_email]
if queue.enable_notifications_on_email_events and len(notifications_to_be_sent):
ticket_cc_list = TicketCC.objects.filter(ticket=ticket).all().values_list('email', flat=True)
for email in ticket_cc_list :
for email in ticket_cc_list:
notifications_to_be_sent.append(email)
# send mail to appropriate people now depending on what objects
@ -399,13 +399,13 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
context.update(comment=f.comment)
ticket.send(
{'submitter': ('newticket_submitter', context),
'assigned_to': ('updated_owner', context),},
'assigned_to': ('updated_owner', context)},
fail_silently=True,
extra_headers={'In-Reply-To': message_id},
)
if queue.enable_notifications_on_email_events:
ticket.send(
{'ticket_cc': ('updated_cc', context),},
{'ticket_cc': ('updated_cc', context)},
fail_silently=True,
extra_headers={'In-Reply-To': message_id},
)
@ -532,4 +532,3 @@ def object_from_message(message, queue, logger):
}
return create_object_from_email_message(message, ticket, payload, files, logger=logger)

View File

@ -113,9 +113,9 @@ class Queue(models.Model):
blank=True,
default=False,
help_text=_('When an email arrives to either create a ticket or to '
'interact with an existing discussion. Should email notifications be sent ? '
'Note: the new_ticket_cc and updated_ticket_cc work independently of this feature'),
)
'interact with an existing discussion. Should email notifications be sent ? '
'Note: the new_ticket_cc and updated_ticket_cc work independently of this feature'),
)
email_box_type = models.CharField(
_('E-Mail Box Type'),
@ -767,7 +767,7 @@ class FollowUp(models.Model):
null=True,
help_text=_("The Message ID of the submitter's email."),
editable=False,
)
)
objects = FollowUpManager()

View File

@ -139,32 +139,35 @@ class TicketBasicsTestCase(TestCase):
# Ensure only two e-mails were sent - submitter & updated.
self.assertEqual(email_count + 2, len(mail.outbox))
class EmailInteractionsTestCase(TestCase):
fixtures = ['emailtemplate.json']
def setUp(self):
self.queue_public = Queue.objects.create(title='Mail Queue 1',
slug='mq1',
email_address='queue-1@example.com',
allow_public_submission=True,
new_ticket_cc='new.public.with.notifications@example.com',
updated_ticket_cc='update.public.with.notifications@example.com',
enable_notifications_on_email_events=True,
)
self.queue_public = Queue.objects.create(
title='Mail Queue 1',
slug='mq1',
email_address='queue-1@example.com',
allow_public_submission=True,
new_ticket_cc='new.public.with.notifications@example.com',
updated_ticket_cc='update.public.with.notifications@example.com',
enable_notifications_on_email_events=True,
)
self.queue_public_with_notifications_disabled = Queue.objects.create(title='Mail Queue 2',
slug='mq2',
email_address='queue-2@example.com',
allow_public_submission=True,
new_ticket_cc='new.public.without.notifications@example.com',
updated_ticket_cc='update.public.without.notifications@example.com',
enable_notifications_on_email_events=False,
)
self.queue_public_with_notifications_disabled = Queue.objects.create(
title='Mail Queue 2',
slug='mq2',
email_address='queue-2@example.com',
allow_public_submission=True,
new_ticket_cc='new.public.without.notifications@example.com',
updated_ticket_cc='update.public.without.notifications@example.com',
enable_notifications_on_email_events=False,
)
self.ticket_data = {
'title': 'Test Ticket',
'description': 'Some Test Ticket',
}
'title': 'Test Ticket',
'description': 'Some Test Ticket',
}
def test_create_ticket_from_email_with_message_id(self):
@ -200,7 +203,7 @@ class EmailInteractionsTestCase(TestCase):
self.assertEqual(email_count + 1 + 2, len(mail.outbox))
# Ensure that the submitter is notified
self.assertIn(submitter_email,mail.outbox[0].to)
self.assertIn(submitter_email, mail.outbox[0].to)
def test_create_ticket_from_email_without_message_id(self):
@ -232,10 +235,9 @@ class EmailInteractionsTestCase(TestCase):
self.assertEqual(email_count + 1 + 2, len(mail.outbox))
# Ensure that the submitter is notified
self.assertIn(submitter_email,mail.outbox[0].to)
self.assertIn(submitter_email, mail.outbox[0].to)
def test_create_ticket_from_email_with_carbon_copy(self):
"""
Ensure that an instance of <TicketCC> is created for every valid element of the
"rfc_2822_cc" field when creating a <Ticket> instance.
@ -270,13 +272,12 @@ class EmailInteractionsTestCase(TestCase):
self.assertEqual(email_count + 1 + 2 + 2, len(mail.outbox))
# Ensure that the submitter is notified
self.assertIn(submitter_email,mail.outbox[0].to)
self.assertIn(submitter_email, mail.outbox[0].to)
for cc_email in cc_list:
# Ensure that contacts on cc_list will be notified on the same email (index 0)
#self.assertIn(cc_email, mail.outbox[0].to)
# self.assertIn(cc_email, mail.outbox[0].to)
# Ensure that <TicketCC> exists
ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
@ -284,7 +285,6 @@ class EmailInteractionsTestCase(TestCase):
self.assertTrue(ticket_cc.email, cc_email)
def test_create_ticket_from_email_to_multiple_emails(self):
"""
Ensure that an instance of <TicketCC> is created for every valid element of the
"rfc_2822_cc" field when creating a <Ticket> instance.
@ -319,7 +319,7 @@ class EmailInteractionsTestCase(TestCase):
self.assertEqual(email_count + 1 + 2 + 2, len(mail.outbox))
# Ensure that the submitter is notified
self.assertIn(submitter_email,mail.outbox[0].to)
self.assertIn(submitter_email, mail.outbox[0].to)
# Ensure that the queue's email was not subscribed to the event notifications.
self.assertRaises(TicketCC.DoesNotExist, TicketCC.objects.get, ticket=ticket, email=to_list[0])
@ -327,7 +327,7 @@ class EmailInteractionsTestCase(TestCase):
for cc_email in cc_list:
# Ensure that contacts on cc_list will be notified on the same email (index 0)
#self.assertIn(cc_email, mail.outbox[0].to)
# self.assertIn(cc_email, mail.outbox[0].to)
# Ensure that <TicketCC> exists
ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
@ -335,7 +335,6 @@ class EmailInteractionsTestCase(TestCase):
self.assertTrue(ticket_cc.email, cc_email)
def test_create_ticket_from_email_with_invalid_carbon_copy(self):
"""
Ensure that no <TicketCC> instance is created if an invalid element of the
"rfc_2822_cc" field is provided when creating a <Ticket> instance.
@ -375,7 +374,7 @@ class EmailInteractionsTestCase(TestCase):
for cc_email in cc_list:
# Ensure that contacts on cc_list will be notified on the same email (index 0)
#self.assertIn(cc_email, mail.outbox[0].to)
# self.assertIn(cc_email, mail.outbox[0].to)
# Ensure that <TicketCC> exists. Even if it's an invalid email.
ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
@ -383,14 +382,12 @@ class EmailInteractionsTestCase(TestCase):
self.assertTrue(ticket_cc.email, cc_email)
def test_create_followup_from_email_with_valid_message_id_with_no_initial_cc_list(self):
"""
Ensure that if a message is received with an valid In-Reply-To ID,
the expected <TicketCC> instances are created even if the there were
no <TicketCC>s so far.
"""
### Ticket and TicketCCs creation ###
# Ticket and TicketCCs creation #
msg = email.message.Message()
message_id = uuid.uuid4().hex
@ -415,13 +412,13 @@ class EmailInteractionsTestCase(TestCase):
# the new and update queues (+2)
# Ensure that the submitter is notified
self.assertIn(submitter_email,mail.outbox[0].to)
self.assertIn(submitter_email, mail.outbox[0].to)
# As we have created an Ticket from an email, we notify the sender (+1)
# and the new and update queues (+2)
expected_email_count = 1 + 2
self.assertEqual(expected_email_count, len(mail.outbox))
### end of the Ticket and TicketCCs creation ###
# end of the Ticket and TicketCCs creation #
# Reply message
reply = email.message.Message()
@ -466,21 +463,19 @@ class EmailInteractionsTestCase(TestCase):
# the new and update queues (+2)
# Ensure that the submitter is notified
#self.assertIn(submitter_email, mail.outbox[expected_email_count - 3].to)
# self.assertIn(submitter_email, mail.outbox[expected_email_count - 3].to)
# Ensure that contacts on cc_list will be notified on the same email (index 0)
#for cc_email in cc_list:
#self.assertIn(cc_email, mail.outbox[expected_email_count - 1].to)
# for cc_email in cc_list:
# self.assertIn(cc_email, mail.outbox[expected_email_count - 1].to)
def test_create_followup_from_email_with_valid_message_id_with_original_cc_list_included(self):
"""
Ensure that if a message is received with an valid In-Reply-To ID,
the expected <TicketCC> instances are created but if there's any
overlap with the previous Cc list, no duplicates are created.
"""
### Ticket and TicketCCs creation ###
# Ticket and TicketCCs creation #
msg = email.message.Message()
message_id = uuid.uuid4().hex
@ -514,13 +509,13 @@ class EmailInteractionsTestCase(TestCase):
# the new and update queues (+2)
# Ensure that the submitter is notified
self.assertIn(submitter_email,mail.outbox[0].to)
self.assertIn(submitter_email, mail.outbox[0].to)
# As we have created an Ticket from an email, we notify the sender (+1)
# and the new and update queues (+2)
expected_email_count = 1 + 2
self.assertEqual(expected_email_count, len(mail.outbox))
### end of the Ticket and TicketCCs creation ###
# end of the Ticket and TicketCCs creation #
# Reply message
reply = email.message.Message()
@ -567,14 +562,13 @@ class EmailInteractionsTestCase(TestCase):
self.assertTrue(ticket_cc.email, cc_email)
def test_create_followup_from_email_with_invalid_message_id(self):
"""
Ensure that if a message is received with an invalid In-Reply-To ID and we
can infer the original Ticket ID by the message's subject, the expected
<TicketCC> instances are created
Ensure that if a message is received with an invalid In-Reply-To
ID and we can infer the original Ticket ID by the message's subject,
the expected <TicketCC> instances are created.
"""
### Ticket and TicketCCs creation ###
# Ticket and TicketCCs creation #
msg = email.message.Message()
message_id = uuid.uuid4().hex
@ -611,19 +605,19 @@ class EmailInteractionsTestCase(TestCase):
self.assertEqual(expected_email_count, len(mail.outbox))
# Ensure that the submitter is notified
self.assertIn(submitter_email,mail.outbox[0].to)
self.assertIn(submitter_email, mail.outbox[0].to)
# Ensure that <TicketCC> is created
for cc_email in cc_list:
# Ensure that contacts on cc_list will be notified on the same email (index 0)
#self.assertIn(cc_email, mail.outbox[0].to)
# self.assertIn(cc_email, mail.outbox[0].to)
ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
self.assertTrue(ticket_cc.ticket, ticket)
self.assertTrue(ticket_cc.email, cc_email)
### end of the Ticket and TicketCCs creation ###
# end of the Ticket and TicketCCs creation #
# Reply message
reply = email.message.Message()
@ -652,7 +646,6 @@ class EmailInteractionsTestCase(TestCase):
ticket = Ticket.objects.get(id=followup.ticket.id)
self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)
# Ensure that <TicketCC> is created
for cc_email in cc_list:
# Even after 2 messages with the same cc_list, <get> MUST return only
@ -668,10 +661,10 @@ class EmailInteractionsTestCase(TestCase):
self.assertEqual(email_count + 1 + 2 + 2, len(mail.outbox))
def test_create_ticket_from_email_to_a_notification_enabled_queue(self):
"""
Ensure that when an email is sent to a Queue with notifications_enabled turned ON,
and a <Ticket> is created, all contacts n the TicketCC list are notified.
Ensure that when an email is sent to a Queue with
notifications_enabled turned ON, and a <Ticket> is created, all
contacts in the TicketCC list are notified.
"""
msg = email.message.Message()
@ -691,7 +684,6 @@ class EmailInteractionsTestCase(TestCase):
email_count = len(mail.outbox)
object_from_message(str(msg), self.queue_public, logger=logger)
followup = FollowUp.objects.get(message_id=message_id)
ticket = Ticket.objects.get(id=followup.ticket.id)
self.assertEqual(ticket.ticket_for_url, "mq1-%s" % ticket.id)
@ -709,19 +701,18 @@ class EmailInteractionsTestCase(TestCase):
for cc_email in cc_list:
# Ensure that contacts on cc_list will be notified on the same email (index 0)
#self.assertIn(cc_email, mail.outbox[0].to)
# self.assertIn(cc_email, mail.outbox[0].to)
ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
self.assertTrue(ticket_cc.ticket, ticket)
self.assertTrue(ticket_cc.email, cc_email)
def test_create_ticket_from_email_to_a_notification_disabled_queue(self):
"""
Ensure that when an email is sent to a Queue with notifications_enabled turned OFF, only the
new_ticket_cc and updated_ticket_cc contacts (if they are set) are notified. No contact
from the TicketCC list should be notified.
Ensure that when an email is sent to a Queue with notifications_enabled
turned OFF, only the new_ticket_cc and updated_ticket_cc contacts (if
they are set) are notified. No contact from the TicketCC list should
be notified.
"""
msg = email.message.Message()
@ -764,13 +755,12 @@ class EmailInteractionsTestCase(TestCase):
self.assertTrue(ticket_cc.email, cc_email)
def test_create_followup_from_email_to_a_notification_enabled_queue(self):
"""
Ensure that when an email is sent to a Queue with notifications_enabled turned ON,
and a <FollowUp> is created, all contacts n the TicketCC list are notified.
Ensure that when an email is sent to a Queue with notifications_enabled
turned ON, and a <FollowUp> is created, all contacts n the TicketCC
list are notified.
"""
### Ticket and TicketCCs creation ###
# Ticket and TicketCCs creation #
msg = email.message.Message()
message_id = uuid.uuid4().hex
@ -804,12 +794,12 @@ class EmailInteractionsTestCase(TestCase):
for cc_email in cc_list:
# Ensure that contacts on cc_list will be notified on the same email (index 0)
#self.assertIn(cc_email, mail.outbox[0].to)
# self.assertIn(cc_email, mail.outbox[0].to)
ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
self.assertTrue(ticket_cc.ticket, ticket)
self.assertTrue(ticket_cc.email, cc_email)
### end of the Ticket and TicketCCs creation ###
# end of the Ticket and TicketCCs creation #
# Reply message
reply = email.message.Message()
@ -842,7 +832,7 @@ class EmailInteractionsTestCase(TestCase):
for cc_email in cc_list:
# Ensure that contacts on cc_list will be notified on the same email (index 0)
#self.assertIn(cc_email, mail.outbox[expected_email_count - 1].to)
# self.assertIn(cc_email, mail.outbox[expected_email_count - 1].to)
ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
self.assertTrue(ticket_cc.ticket, ticket)
@ -853,8 +843,7 @@ class EmailInteractionsTestCase(TestCase):
Ensure that when an email is sent to a Queue with notifications_enabled
turned OFF, and a <FollowUp> is created, TicketCC is NOT notified.
"""
### Ticket and TicketCCs creation ###
# Ticket and TicketCCs creation #
msg = email.message.Message()
message_id = uuid.uuid4().hex
@ -883,7 +872,6 @@ class EmailInteractionsTestCase(TestCase):
expected_email_count = email_count + 1 + 2
self.assertEqual(expected_email_count, len(mail.outbox))
# Ensure that <TicketCC> is created
for cc_email in cc_list:
@ -893,7 +881,7 @@ class EmailInteractionsTestCase(TestCase):
ticket_cc = TicketCC.objects.get(ticket=ticket, email=cc_email)
self.assertTrue(ticket_cc.ticket, ticket)
self.assertTrue(ticket_cc.email, cc_email)
### end of the Ticket and TicketCCs creation ###
# end of the Ticket and TicketCCs creation #
# Reply message
reply = email.message.Message()
@ -920,16 +908,14 @@ class EmailInteractionsTestCase(TestCase):
expected_email_count += 1
self.assertEqual(expected_email_count, len(mail.outbox))
def test_create_followup_from_email_with_valid_message_id_with_original_cc_list_included(self):
"""
Ensure that if a message is received with an valid In-Reply-To ID,
the expected <TicketCC> instances are created even if the there were
no <TicketCC>s so far.
"""
### Ticket and TicketCCs creation ###
# Ticket and TicketCCs creation #
msg = email.message.Message()
message_id = uuid.uuid4().hex
@ -948,7 +934,7 @@ class EmailInteractionsTestCase(TestCase):
followup = FollowUp.objects.get(message_id=message_id)
ticket = Ticket.objects.get(id=followup.ticket.id)
### end of the Ticket and TicketCCs creation ###
# end of the Ticket and TicketCCs creation #
# Reply message
reply = email.message.Message()

View File

@ -388,6 +388,7 @@ def return_ticketccstring_and_show_subscribe(user, ticket):
return ticketcc_string, show_subscribe
def subscribe_to_ticket_updates(ticket, user=None, email=None, can_view=True, can_update=False):
if ticket is not None:
@ -400,8 +401,8 @@ def subscribe_to_ticket_updates(ticket, user=None, email=None, can_view=True, ca
if user is None and len(email) < 5:
raise ValidationError(
_('When you add somebody on Cc, you must provide either a User or a valid email. Email: %s' %email)
)
_('When you add somebody on Cc, you must provide either a User or a valid email. Email: %s' % email)
)
ticketcc = TicketCC(
ticket=ticket,