Skip to content

Commit

Permalink
feat(generic): update Enrichview & GenericModel to work with generic uri
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
b1rger committed Dec 20, 2024
1 parent 63a4031 commit 87fdb98
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion apis_core/generic/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions apis_core/generic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 87fdb98

Please sign in to comment.