From 86c4ceb1a8f6e7778bfcf6a23838c3f1f8f78954 Mon Sep 17 00:00:00 2001 From: Arkadiy Korotaev Date: Thu, 29 Jul 2021 13:14:42 +0200 Subject: [PATCH] fix(emails): Avoid 'value too long for type character varying(200) ' error when incoming message has too long subject --- helpdesk/models.py | 3 +++ helpdesk/tests/test_files/blank-body-with-attachment.eml | 2 +- helpdesk/tests/test_get_email.py | 8 +++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/helpdesk/models.py b/helpdesk/models.py index dbed2a56..f111ed88 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -788,6 +788,9 @@ class Ticket(models.Model): self.modified = timezone.now() + if len(self.title) > 200: + self.title = self.title[:197] + "..." + super(Ticket, self).save(*args, **kwargs) @staticmethod diff --git a/helpdesk/tests/test_files/blank-body-with-attachment.eml b/helpdesk/tests/test_files/blank-body-with-attachment.eml index bab4f184..3683da7d 100644 --- a/helpdesk/tests/test_files/blank-body-with-attachment.eml +++ b/helpdesk/tests/test_files/blank-body-with-attachment.eml @@ -1,6 +1,6 @@ To: helpdesk@auto-mat.cz From: Timothy Hobbs -Subject: Attachment without body +Subject: Attachment without body - and a looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong subject Openpgp: preference=signencrypt Message-ID: Date: Fri, 15 Feb 2019 13:58:05 +0100 diff --git a/helpdesk/tests/test_get_email.py b/helpdesk/tests/test_get_email.py index fe8750cd..c113b04f 100644 --- a/helpdesk/tests/test_get_email.py +++ b/helpdesk/tests/test_get_email.py @@ -53,7 +53,13 @@ class GetEmailCommonTests(TestCase): with open(os.path.join(THIS_DIR, "test_files/blank-body-with-attachment.eml")) as fd: test_email = fd.read() ticket = helpdesk.email.object_from_message(test_email, self.queue_public, self.logger) - self.assertEqual(ticket.title, "Attachment without body") + + # title got truncated because of max_lengh of the model.title field + assert ticket.title == ( + "Attachment without body - and a loooooooooooooooooooooooooooooooooo" + "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo..." + ) self.assertEqual(ticket.description, "") def test_email_with_quoted_printable_body(self):