Skip to content

Commit

Permalink
Fix error when deleting observer subsciption
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed Jan 13, 2025
1 parent ccf5793 commit 9312aa3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
9 changes: 9 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to this project are documented in this file.
This project adheres to `Semantic Versioning <http://semver.org/>`_.


==========
Unreleased
==========

Fixed
-----
- Fix error when deleting observer subscription


===================
42.2.0 - 2025-01-13
===================
Expand Down
17 changes: 12 additions & 5 deletions resolwe/observers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,24 @@ def subscribe(
)
self.observers.add(observer)

@transaction.atomic
def delete(self):
"""Delete the given subscription.
Delete all observers with no remaining subscriptions.
"""
# Find related observers with only one remaining subscription
# (it must be this one) and delete them first.
Observer.objects.annotate(subs=Count("subscriptions")).filter(
subscriptions=self.pk, subs=1
).delete()
# Find observers containing this subscription as the only one.
# Delete it after deleting the subscription.
observer = (
Observer.objects.annotate(subs=Count("subscriptions"))
.filter(subscriptions=self.pk, subs=1)
.first()
)
# Delete the subscription.
super().delete()
# Delete the observer if it has no more subscriptions.
if observer is not None:
observer.delete()

@classmethod
def notify(
Expand Down

0 comments on commit 9312aa3

Please sign in to comment.