Skip to content

Commit

Permalink
fix(utils): add a raise_on_fail argument to the object create function
Browse files Browse the repository at this point in the history
This allows the calling function to define how it wants the
`create_object_from_uri` function to communicate errors. This is not
pretty and we might want to rethink that function in the future, but for
now it allows the two places that call that function to both keep
working.

Closes: #1405
  • Loading branch information
b1rger committed Nov 19, 2024
1 parent 3342d15 commit 16c2ef7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apis_core/generic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def get_results(self, context):
return results

def create_object(self, value):
return create_object_from_uri(value, self.queryset.model)
return create_object_from_uri(value, self.queryset.model, raise_on_fail=True)

def post(self, request, *args, **kwargs):
try:
Expand Down
12 changes: 7 additions & 5 deletions apis_core/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_importer_for_model(model: object):
raise ImproperlyConfigured(f"No suitable importer found for {model}")


def create_object_from_uri(uri: str, model: object) -> object:
def create_object_from_uri(uri: str, model: object, raise_on_fail=False) -> object:
if uri.startswith("http"):
try:
uri = Uri.objects.get(uri=uri)
Expand All @@ -82,10 +82,12 @@ def create_object_from_uri(uri: str, model: object) -> object:
instance = importer.create_instance()
uri = Uri.objects.create(uri=importer.get_uri, root_object=instance)
return instance
content_type = ContentType.objects.get_for_model(model)
raise ImproperlyConfigured(
f'Could not create {content_type.name} from string "{uri}"'
)
if raise_on_fail:
content_type = ContentType.objects.get_for_model(model)
raise ImproperlyConfigured(
f'Could not create {content_type.name} from string "{uri}"'
)
return False


def get_html_diff(a, b, show_a=True, show_b=True, shorten=0):
Expand Down

0 comments on commit 16c2ef7

Please sign in to comment.