From 87fdb9806c3335beaa875a68a0eb99d79b545cc8 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Fri, 29 Nov 2024 10:23:20 +0100 Subject: [PATCH] feat(generic): update Enrichview & GenericModel to work with generic uri The `Uri` model now uses a generic foreign key instead of the direct foreign key to the `RootObject`. This commit updates all occurences of `root_object` to use the generic foreign key instead. --- apis_core/generic/abc.py | 6 +++++- apis_core/generic/views.py | 12 +++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/apis_core/generic/abc.py b/apis_core/generic/abc.py index 4cf185c09..7610b7cca 100644 --- a/apis_core/generic/abc.py +++ b/apis_core/generic/abc.py @@ -165,7 +165,11 @@ def merge_with(self, entities): for s in getattr(ent, f.name).all(): if s not in sl: getattr(self, f.name).add(s) - Uri.objects.filter(root_object=ent).update(root_object=self) + self_content_type = ContentType.objects.get_for_model(self) + ent_content_type = ContentType.objects.get_for_model(ent) + Uri.objects.filter(content_type=ent_content_type, object_id=ent.id).update( + content_type=self_content_type, object_id=self.id + ) for ent in entities: self.merge_fields(ent) diff --git a/apis_core/generic/views.py b/apis_core/generic/views.py index c0ca5d3db..60dc74436 100644 --- a/apis_core/generic/views.py +++ b/apis_core/generic/views.py @@ -6,6 +6,7 @@ from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import PermissionRequiredMixin +from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ImproperlyConfigured from django.forms import modelform_factory from django.forms.utils import pretty_name @@ -458,12 +459,12 @@ def get(self, *args, **kwargs): return redirect(self.object.get_merge_url(self.uri)) try: uriobj = Uri.objects.get(uri=self.uri) - if uriobj.root_object.id != self.object.id: + if uriobj.object_id != self.object.id: messages.info( self.request, f"Object with URI {self.uri} already exists, you were redirected to the merge form.", ) - return redirect(self.object.get_merge_url(uriobj.root_object.id)) + return redirect(self.object.get_merge_url(uriobj.object_id)) except Uri.DoesNotExist: pass return super().get(*args, **kwargs) @@ -500,7 +501,12 @@ def form_valid(self, form): importer = self.importer_class(self.uri, self.model) importer.import_into_instance(self.object, fields=update_fields) messages.info(self.request, f"Updated fields {update_fields}") - uri, created = Uri.objects.get_or_create(uri=self.uri, root_object=self.object) + content_type = ContentType.objects.get_for_model(self.model) + uri, created = Uri.objects.get_or_create( + uri=self.uri, + content_type=content_type, + object_id=self.object.id, + ) if created: messages.info(self.request, f"Added uri {self.uri} to {self.object}") return super().form_valid(form)