Commit 561268e8 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

implement rpc.update_collection_from_xlsx_row

parent 0ce12dc5
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -3204,6 +3204,39 @@ 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(
        pk=collection_data.get("collection_pk"), deleted_at__isnull=True
    ).first()
    if not collection_instance:
        raise ServiceException(NO_SUCH_COLLECTION)
    _check_project_permission(user, collection_instance.project, PERM_COLLECTION_UPDATE)
    new_title = collection_data.get("title", "").strip()
    if new_title:
        collection_instance.title = new_title

    for k in collection_data.keys():
        if ":" in k:  # example: Dublin Core: creator
            meta_set_name, meta_name = [x.strip() for x in k.split(":")]
            metadata_instance = Metadata.objects.filter(
                title=meta_name, set__title=meta_set_name
            ).first()
            if metadata_instance:
                MetadataCollectionValue.objects.filter(
                    metadata=metadata_instance, collection=collection_instance
                ).delete()
                if collection_data.get(k) is not None:
                    for value in [
                        x.strip()
                        for x in str(collection_data.get(k, "")).split(
                            XLSX_MULTIPLE_VALUES_SEPARATOR
                        )
                    ]:
                        meta_collection_value = MetadataCollectionValue()
                        meta_collection_value.collection = collection_instance
                        meta_collection_value.metadata = metadata_instance
                        meta_collection_value.value = value.strip()
                        meta_collection_value.save()
    collection_instance.save()  # force update of collection
    return True