Files
python-HyperKitty/fix-django41.patch

68 lines
2.7 KiB
Diff

From 94194e0f5eeacf1be2a88a28bfed62dbc6c5d5a2 Mon Sep 17 00:00:00 2001
From: Daniel Garcia Moreno <daniel.garcia@suse.com>
Date: Thu, 6 Oct 2022 12:53:56 +0200
Subject: [PATCH] Fix django4.1 compatibility issues with related fields
In Django4.1 a ValueError exception is raised when trying to access to
foreign keys for unsaved model instances:
https://docs.djangoproject.com/en/4.1/releases/4.1/#reverse-foreign-key-changes-for-unsaved-model-instances
This patch ensures that everything is saved before trying to use related
models and handles the exception correctly in the Thread model pre_save.
---
hyperkitty/models/thread.py | 8 ++++++++
hyperkitty/tests/models/test_email.py | 6 ++++++
2 files changed, 14 insertions(+)
diff --git a/hyperkitty/models/thread.py b/hyperkitty/models/thread.py
index b550cdb8..e49954a7 100644
--- a/hyperkitty/models/thread.py
+++ b/hyperkitty/models/thread.py
@@ -148,10 +148,18 @@ class Thread(models.Model):
from .email import Email # circular import
if self.starting_email is not None:
return
+
try:
self.starting_email = self.emails.get(parent_id__isnull=True)
except Email.DoesNotExist:
self.starting_email = self.emails.order_by("date").first()
+ except ValueError:
+ # If the Thread is not created yet, the self.emails will raise a
+ # ValueError exception. This happens at creation time because this
+ # method is called by on_pre_save
+ #
+ # https://docs.djangoproject.com/en/4.1/releases/4.1/#reverse-foreign-key-changes-for-unsaved-model-instances
+ return
def on_pre_save(self):
self.find_starting_email()
diff --git a/hyperkitty/tests/models/test_email.py b/hyperkitty/tests/models/test_email.py
index 7bcac390..f194e2b1 100644
--- a/hyperkitty/tests/models/test_email.py
+++ b/hyperkitty/tests/models/test_email.py
@@ -195,6 +195,9 @@ class EmailTestCase(TestCase):
subject="This is a folded\n subject",
in_reply_to="<msg1.example.com>\n <msg2.example.com>",
content="Dummy message")
+ sender.save()
+ mlist.save()
+ email.save()
msg = email.as_message()
self.assertEqual(msg["Subject"], "This is a folded subject")
@@ -210,6 +213,9 @@ class EmailTestCase(TestCase):
mailinglist=mlist,
subject="Message subject",
content="Dummy message")
+ sender.save()
+ mlist.save()
+ email.save()
msg = email.as_message()
self.assertEqual(msg['from'], '"Team: J.Q. Doe" <dummy@example.com>')
--
2.37.3