From b4c0f27df2f3ee8ddcba3cbb0dcbb5123f1c71d8 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Mon, 16 Dec 2024 15:32:12 +0100 Subject: [PATCH] feat(generic): adapt importer to rdf parser list style return values --- apis_core/generic/importers.py | 2 ++ apis_core/utils/helpers.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/apis_core/generic/importers.py b/apis_core/generic/importers.py index 8b8dcc407..6acf2a299 100644 --- a/apis_core/generic/importers.py +++ b/apis_core/generic/importers.py @@ -5,6 +5,7 @@ from django.core.exceptions import ImproperlyConfigured +from apis_core.utils.helpers import flatten_if_single from apis_core.utils.normalize import clean_uri from apis_core.utils.rdf import get_definition_and_attributes_from_uri @@ -70,6 +71,7 @@ def get_data(self, drop_unknown_fields=True): # we are dropping all fields that are not part of the model modelfields = [field.name for field in self.model._meta.fields] data = {key: data[key] for key in data if key in modelfields} + data = {key: flatten_if_single(value) for key, value in data.items()} if not data: raise ImproperlyConfigured( f"Could not import {self.import_uri}. Data fetched was: {data}" diff --git a/apis_core/utils/helpers.py b/apis_core/utils/helpers.py index a65f83e48..c7481072c 100644 --- a/apis_core/utils/helpers.py +++ b/apis_core/utils/helpers.py @@ -171,3 +171,9 @@ def construct_lookup(value: str) -> tuple[str, str]: if value.startswith("*") and value.endswith("*"): value = value[1:-1] return "__icontains", value + + +def flatten_if_single(value: list): + if len(value) == 1: + return value[0] + return value