Skip to content

Commit

Permalink
feat(utils): update create_object_from_uri 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.
  • Loading branch information
b1rger committed Nov 29, 2024
1 parent 6fd2177 commit e20b06d
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions apis_core/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ def create_object_from_uri(uri: str, model: object, raise_on_fail=False) -> obje
if uri.startswith("http"):
try:
uri = Uri.objects.get(uri=uri)
return uri.root_object
return uri.content_object or uri.root_object
except Uri.DoesNotExist:
Importer = get_importer_for_model(model)
importer = Importer(uri, model)
instance = importer.create_instance()
uri = Uri.objects.create(uri=importer.get_uri, root_object=instance)
content_type = ContentType.objects.get_for_model(instance)
uri = Uri.objects.create(uri=importer.get_uri, root_object=instance, content_type=content_type, object_id=instance.id)
return instance
if raise_on_fail:
content_type = ContentType.objects.get_for_model(model)
Expand Down

0 comments on commit e20b06d

Please sign in to comment.