#95: save CC'd emails on a comment to the Ticket for future correspondence, set to view only initially, and update tests for CC

This commit is contained in:
Garret Wassermann 2017-04-16 04:19:17 -04:00
parent 03b5e41b42
commit b2ac1fd3a5
2 changed files with 33 additions and 4 deletions

View File

@ -34,7 +34,7 @@ from django.utils import encoding, six, timezone
from helpdesk import settings
from helpdesk.lib import send_templated_mail, safe_template_context, process_attachments
from helpdesk.models import Queue, Ticket, FollowUp, IgnoreEmail
from helpdesk.models import Queue, Ticket, TicketCC, FollowUp, IgnoreEmail
import logging
@ -264,7 +264,7 @@ def decode_mail_headers(string):
def ticket_from_message(message, queue, logger):
# 'message' must be an RFC822 formatted message.
message = email.message_from_string(message) if six.PY3 else email.message_from_string(message.encode('utf-8'))
subject = message.get('subject', _('Created from e-mail'))
subject = message.get('subject', _('Comment from e-mail'))
subject = decode_mail_headers(decodeUnknown(message.get_charset(), subject))
for affix in STRIPPED_SUBJECT_STRINGS:
subject = subject.replace(affix, "")
@ -274,6 +274,14 @@ def ticket_from_message(message, queue, logger):
sender = decode_mail_headers(decodeUnknown(message.get_charset(), sender))
sender_email = email.utils.parseaddr(sender)[1]
cc = message.get_all('cc', None)
if cc:
# get_all checks if multiple CC headers, but individual emails may be comma separated too
tempcc = []
for hdr in cc:
tempcc.extend(hdr.split(','))
cc = [decode_mail_headers(decodeUnknown(message.get_charset(), x.strip())) for x in tempcc]
for ignore in IgnoreEmail.objects.filter(Q(queues=queue) | Q(queues__isnull=True)):
if ignore.test(sender_email):
if ignore.keep_in_mailbox:
@ -358,6 +366,16 @@ def ticket_from_message(message, queue, logger):
)
logger.debug("Created new ticket %s-%s" % (t.queue.slug, t.id))
if cc:
for new_cc in cc:
tcc = TicketCC(
ticket=t,
email=new_cc,
can_view=True,
can_update=False
)
tcc.save()
f = FollowUp(
ticket=t,
title=_('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}),

View File

@ -2,7 +2,7 @@
from __future__ import unicode_literals
from helpdesk.models import Queue, Ticket, FollowUp, Attachment
from helpdesk.models import Queue, Ticket, TicketCC, FollowUp, Attachment
from django.test import TestCase
from django.core.management import call_command
from django.utils import six
@ -83,9 +83,10 @@ class GetEmailParametricTemplate(object):
# example email text from Django docs: https://docs.djangoproject.com/en/1.10/ref/unicode/
test_email_from = "Arnbjörg Ráðormsdóttir <arnbjorg@example.com>"
test_email_cc = "other@example.com"
test_email_subject = "My visit to Sør-Trøndelag"
test_email_body = "Unicode helpdesk comment with an s-hat (ŝ) via email."
test_email = "To: helpdesk@example.com\nFrom: " + test_email_from + "\nSubject: " + test_email_subject + "\n\n" + test_email_body
test_email = "To: helpdesk@example.com\nCc: " + test_email_cc + "\nFrom: " + test_email_from + "\nSubject: " + test_email_subject + "\n\n" + test_email_body
test_mail_len = len(test_email)
if self.socks:
@ -142,6 +143,8 @@ class GetEmailParametricTemplate(object):
self.assertEqual(ticket1.ticket_for_url, "QQ-%s" % ticket1.id)
self.assertEqual(ticket1.title, test_email_subject)
self.assertEqual(ticket1.description, test_email_body)
cc1 = get_object_or_404(TicketCC, pk=1)
self.assertEqual(cc1.email, test_email_cc)
ticket2 = get_object_or_404(Ticket, pk=2)
self.assertEqual(ticket2.ticket_for_url, "QQ-%s" % ticket2.id)
@ -160,6 +163,9 @@ class GetEmailParametricTemplate(object):
me = "my@example.com"
you = "your@example.com"
cc_one = "other@example.com"
cc_two = "nobody@example.com"
cc = cc_one + ", " + cc_two
subject = "Link"
# Create message container - the correct MIME type is multipart/alternative.
@ -167,6 +173,7 @@ class GetEmailParametricTemplate(object):
msg['Subject'] = subject
msg['From'] = me
msg['To'] = you
msg['Cc'] = cc
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
@ -255,6 +262,10 @@ class GetEmailParametricTemplate(object):
attach1 = get_object_or_404(Attachment, pk=1)
self.assertEqual(attach1.followup.id, 1)
self.assertEqual(attach1.filename, 'email_html_body.html')
cc1 = get_object_or_404(TicketCC, pk=1)
self.assertEqual(cc1.email, cc_one)
cc2 = get_object_or_404(TicketCC, pk=2)
self.assertEqual(cc2.email, cc_two)
ticket2 = get_object_or_404(Ticket, pk=2)
self.assertEqual(ticket2.ticket_for_url, "QQ-%s" % ticket2.id)