From 9312aa321f6a122b9ec73f8a3fcf6ba4da927082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20Jer=C5=A1e?= Date: Mon, 13 Jan 2025 16:01:18 +0100 Subject: [PATCH] Fix error when deleting observer subsciption --- docs/CHANGELOG.rst | 9 +++++++++ resolwe/observers/models.py | 17 ++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/docs/CHANGELOG.rst b/docs/CHANGELOG.rst index f98848a9f..7a405d2e2 100644 --- a/docs/CHANGELOG.rst +++ b/docs/CHANGELOG.rst @@ -6,6 +6,15 @@ All notable changes to this project are documented in this file. This project adheres to `Semantic Versioning `_. +========== +Unreleased +========== + +Fixed +----- +- Fix error when deleting observer subscription + + =================== 42.2.0 - 2025-01-13 =================== diff --git a/resolwe/observers/models.py b/resolwe/observers/models.py index c2ca8c2a1..48a44942d 100644 --- a/resolwe/observers/models.py +++ b/resolwe/observers/models.py @@ -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(