Commit 9bf8e8e8 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

recursive xlsx import

parent 4269af0e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
__version__ = "0.1.64"
__version__ = "0.1.65"
+12 −2
Original line number Diff line number Diff line
@@ -26,7 +26,12 @@ logger = logging.getLogger(__name__)
def _flatten_resource(
    resource: "Resource", known_metadatas: dict, metadatas_labels: List
) -> dict:
    data = {"resource_pk": resource.pk, "collection_pk": None, "title": resource.title}
    data = {
        "resource_pk": resource.pk,
        "collection_pk": None,
        "cascade": "n",
        "title": resource.title,
    }
    metadata_ids = known_metadatas.keys()
    for known_metadata_label in metadatas_labels:
        data[known_metadata_label] = None
@@ -714,7 +719,12 @@ class Collection(models.Model):
    def metadatas_for_xlsx(
        self, known_metadatas: dict, metadatas_labels: List[str]
    ) -> dict:
        data = {"resource_pk": None, "collection_pk": self.pk, "title": self.title}
        data = {
            "resource_pk": None,
            "collection_pk": self.pk,
            "cascade": "n",
            "title": self.title,
        }
        metadata_ids = known_metadatas.keys()
        for known_metadata_label in metadatas_labels:
            data[known_metadata_label] = None
+18 −1
Original line number Diff line number Diff line
@@ -3204,7 +3204,7 @@ def update_collection_from_xlsx_row(user: User, collection_data: dict) -> bool:
    """
    Compound method to update collection and collection metadatas from a xlsx file row.
    """
    collection_instance = Collection.objects.filter(
    collection_instance: Collection = Collection.objects.filter(
        pk=collection_data.get("collection_pk"), deleted_at__isnull=True
    ).first()
    if not collection_instance:
@@ -3237,6 +3237,23 @@ def update_collection_from_xlsx_row(user: User, collection_data: dict) -> bool:
                        meta_collection_value.value = value.strip()
                        meta_collection_value.save()
    collection_instance.save()  # force update of collection

    # cascade values to descendant resources and collections
    if collection_data.get("cascade", "").strip().lower() == "y":
        for descendant_collection_instance in collection_instance.descendants():
            new_dict = dict(collection_data)
            new_dict["collection_pk"] = descendant_collection_instance.pk
            new_dict["title"] = descendant_collection_instance.title
            new_dict["resource_pk"] = None
            new_dict["cascade"] = "n"
            update_collection_from_xlsx_row(user, new_dict)
        for descendant_resource_instance in collection_instance.descendants_resources():
            new_dict = dict(collection_data)
            new_dict["collection_pk"] = None
            new_dict["resource_pk"] = descendant_resource_instance.pk
            new_dict["title"] = descendant_resource_instance.title
            new_dict["cascade"] = "n"
            update_resource_from_xlsx_row(user, new_dict)
    return True