Skip to content

Commit

Permalink
Use get_or_create for editor page subscriptions
Browse files Browse the repository at this point in the history
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 #11016
  • Loading branch information
tomkins authored and lb- committed Oct 18, 2023
1 parent 9bf7d88 commit 7131a85
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/releases/5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions wagtail/admin/tests/pages/test_edit_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3105,6 +3105,11 @@ def test_commment_notifications_switched_off(self):
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(
Expand Down
15 changes: 7 additions & 8 deletions wagtail/admin/views/pages/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,13 @@ def dispatch(self, request, page_id):
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()
Expand Down

0 comments on commit 7131a85

Please sign in to comment.