From 1d7a968863e58824f10d88adb62f4f132a99ec5c Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Tue, 7 May 2024 10:25:39 -0400 Subject: [PATCH] Allow overwriting resources re #10798 --- arches/app/etl_modules/base_import_module.py | 25 +++++++++++-------- arches/app/etl_modules/jsonld_importer.py | 10 +++++++- .../migrations/10798_jsonld_importer.py | 2 +- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/arches/app/etl_modules/base_import_module.py b/arches/app/etl_modules/base_import_module.py index 9cb7414fc05..b8046e29c43 100644 --- a/arches/app/etl_modules/base_import_module.py +++ b/arches/app/etl_modules/base_import_module.py @@ -175,22 +175,13 @@ def get_node_lookup(self, nodes): return lookup def run_load_task(self, userid, files, summary, result, temp_dir, loadid): + self.loadid = loadid # currently redundant, but be certain try: with connection.cursor() as cursor: self.stage_files(files, summary, cursor) - cursor.execute("""CALL __arches_check_tile_cardinality_violation_for_load(%s)""", [loadid]) - cursor.execute( - """ - INSERT INTO load_errors (type, source, error, loadid, nodegroupid) - SELECT 'tile', source_description, error_message, loadid, nodegroupid - FROM load_staging - WHERE loadid = %s AND passes_validation = false AND error_message IS NOT null - """, - [loadid], - ) + self.check_tile_cardinality(cursor) result["validation"] = self.validate(loadid) if len(result["validation"]["data"]) == 0: - self.loadid = loadid # currently redundant, but be certain save_to_tiles(userid, loadid) cursor.execute("""CALL __arches_update_resource_x_resource_with_graphids();""") cursor.execute("""SELECT __arches_refresh_spatial_views();""") @@ -229,6 +220,18 @@ def stage_files(self, files, summary, cursor): def stage_excel_file(self, file, summary, cursor): pass + def check_tile_cardinality(self, cursor): + cursor.execute("""CALL __arches_check_tile_cardinality_violation_for_load(%s)""", [self.loadid]) + cursor.execute( + """ + INSERT INTO load_errors (type, source, error, loadid, nodegroupid) + SELECT 'tile', source_description, error_message, loadid, nodegroupid + FROM load_staging + WHERE loadid = %s AND passes_validation = false AND error_message IS NOT null + """, + [self.loadid], + ) + ### Actions ### def validate(self, loadid): diff --git a/arches/app/etl_modules/jsonld_importer.py b/arches/app/etl_modules/jsonld_importer.py index a307df7cbf5..2c3a553dec2 100644 --- a/arches/app/etl_modules/jsonld_importer.py +++ b/arches/app/etl_modules/jsonld_importer.py @@ -7,12 +7,13 @@ from django.core.files import File from django.core.files.storage import default_storage from django.core.management import call_command +from django.db import transaction from django.utils.translation import gettext as _ from arches.app.utils.data_management.resources.formats.rdffile import ValueErrorWithNodeInfo from arches.app.etl_modules.base_import_module import BaseImportModule, FileValidationError from arches.app.etl_modules.decorators import load_data_async -from arches.app.models.models import GraphModel, LoadErrors, LoadEvent, LoadStaging, Node +from arches.app.models.models import GraphModel, LoadErrors, LoadEvent, LoadStaging, Node, ResourceInstance from arches.app.models.system_settings import settings from arches.app.utils.file_validator import FileValidator @@ -263,6 +264,13 @@ def save_validation_errors(self, validation_errors, tile, source_value, datatype le.clean_fields() le.save() + def check_tile_cardinality(self, cursor): + with transaction.atomic(): + ResourceInstance.objects.filter( + pk__in=LoadStaging.objects.filter(load_event_id=self.loadid).values("resourceid") + ).delete() + return super().check_tile_cardinality(cursor) + @load_data_async def run_load_task_async(self, request): raise NotImplementedError diff --git a/arches/app/models/migrations/10798_jsonld_importer.py b/arches/app/models/migrations/10798_jsonld_importer.py index 1af13a9cd3a..31fdb0020b1 100644 --- a/arches/app/models/migrations/10798_jsonld_importer.py +++ b/arches/app/models/migrations/10798_jsonld_importer.py @@ -33,7 +33,7 @@ def add_jsonld_module(apps, schema_editor): "modulename": "jsonld_importer.py", "classname": "JSONLDImporter", "config": {"bgColor": "#726a5b", "circleColor": "#9f9580", "show": True}, - "reversible": True, + "reversible": False, # does not support un-overwriting a resource "slug": "jsonld-importer", "description": "Import a zip archive of JSON-LD resources to Arches", "helptemplate": "jsonld-importer-help",