Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generic): update Enrich view to work with generic uri
Browse files Browse the repository at this point in the history
The Uri model now works with a generic foreign key in addition to the a
direct foreign key to the RootObject. This commit updates all occurences
of `root_object` to also work with the generic foreign key.
b1rger committed Nov 29, 2024
1 parent 5ee9df5 commit da33e4b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions apis_core/generic/views.py
Original file line number Diff line number Diff line change
@@ -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
@@ -432,12 +433,13 @@ 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:
obj = uriobj.content_object or uriobj.root_object
if obj.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(obj.id))
except Uri.DoesNotExist:
pass
return super().get(*args, **kwargs)
@@ -474,7 +476,13 @@ 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,
root_object=self.object,
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)

0 comments on commit da33e4b

Please sign in to comment.