Use get_or_create for editor page subscriptions

Two processes loading the edit view of a page without a subscription could result in an integrity error.
See https://docs.djangoproject.com/en/4.2/ref/models/querysets/#get-or-create
Fixes 
pull/11076/head
Alex Tomkins 2023-10-07 21:14:16 +01:00 zatwierdzone przez LB (Ben Johnston)
rodzic 9bf7d88869
commit 7131a85532
4 zmienionych plików z 14 dodań i 8 usunięć
docs/releases
wagtail/admin
views/pages

Wyświetl plik

@ -68,6 +68,7 @@ Changelog
* Fix: Ensure very long words can wrap when viewing saved comments (Chiemezuo Akujobi)
* Fix: Avoid forgotten password link text conflicting with the supplied aria-label (Thibaud Colas)
* Fix: Fix log message to record the correct restriction type when removing a page view restriction (Rohit Sharma, Hazh. M. Adam)
* Fix: Avoid potential race condition with new Page subscriptions on the edit view (Alex Tomkins)
* Docs: Document `WAGTAILADMIN_BASE_URL` on "Integrating Wagtail into a Django project" page (Shreshth Srivastava)
* Docs: Replace incorrect screenshot for authors listing on tutorial (Shreshth Srivastava)
* Docs: Add documentation for building non-model-based choosers using the _queryish_ library (Matt Westcott)

Wyświetl plik

@ -92,6 +92,7 @@ This feature was developed by Paarth Agarwal and Thibaud Colas as part of the Go
* Ensure very long words can wrap when viewing saved comments (Chiemezuo Akujobi)
* Avoid forgotten password link text conflicting with the supplied aria-label (Thibaud Colas)
* Fix log message to record the correct restriction type when removing a page view restriction (Rohit Sharma, Hazh. M. Adam)
* Avoid potential race condition with new Page subscriptions on the edit view (Alex Tomkins)
### Documentation

Wyświetl plik

@ -3105,6 +3105,11 @@ class TestPageSubscriptionSettings(WagtailTestUtils, TestCase):
response,
'<input type="checkbox" name="comment_notifications" id="id_comment_notifications">',
)
self.assertTrue(
PageSubscription.objects.filter(
page=self.child_page, user=self.user, comment_notifications=False
).exists()
)
def test_commment_notifications_switched_on(self):
PageSubscription.objects.create(

Wyświetl plik

@ -376,14 +376,13 @@ class EditView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View):
if response:
return response
try:
self.subscription = PageSubscription.objects.get(
page=self.page, user=self.request.user
)
except PageSubscription.DoesNotExist:
self.subscription = PageSubscription(
page=self.page, user=self.request.user, comment_notifications=False
)
self.subscription, created = PageSubscription.objects.get_or_create(
page=self.page,
user=self.request.user,
defaults={
"comment_notifications": False,
},
)
self.edit_handler = self.page_class.get_edit_handler()
self.form_class = self.edit_handler.get_form_class()