From 009d592936866f3cbfd5e82e30f59b68460f8274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Schr=C3=B6der?= Date: Fri, 22 Nov 2024 20:13:21 +0100 Subject: [PATCH] fix: remove views if catalog is changed # Conflicts: # rdmo/projects/handlers.py --- rdmo/projects/handlers.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 rdmo/projects/handlers.py diff --git a/rdmo/projects/handlers.py b/rdmo/projects/handlers.py new file mode 100644 index 000000000..7014bedfc --- /dev/null +++ b/rdmo/projects/handlers.py @@ -0,0 +1,25 @@ +from django.db.models.signals import post_save +from django.dispatch import receiver + +from rdmo.projects.models import Project +from rdmo.views.models import View + + +@receiver(post_save, sender=Project) +def update_views_on_catalog_change(sender, instance, **kwargs): + # remove views that are no longer available + view_candidates = instance.views.exclude(catalogs__in=[instance.catalog]) \ + .exclude(catalogs=None) + + for view in view_candidates: + instance.views.remove(view) + + # add views that are now available + view_candidates = View.objects.exclude(id__in=[v.id for v in instance.views.all()]) \ + .filter_current_site() \ + .filter_catalog(instance.catalog) + # .filter_group(self.request.user) \ + # .filter_availability(self.request.user).exists() + + for view in view_candidates: + instance.views.add(view)