Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid UUID on queries #1835

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import namespaces as ns
import requests
from databaseFunctions import insertIntoEvents
from django.core.exceptions import ValidationError
from importlib_metadata import version
from lxml import etree
from main import models
Expand Down Expand Up @@ -62,7 +63,7 @@
sip_id=sip_uuid,
currentlocation=f"%SIPDirectory%{xml_rel_path}".encode(),
)
except models.File.DoesNotExist:
except (models.File.DoesNotExist, ValidationError):

Check warning on line 66 in src/MCPClient/lib/clientScripts/archivematicaCreateMETSMetadataXML.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/archivematicaCreateMETSMetadataXML.py#L66

Added line #L66 was not covered by tests
xml_metadata_errors.append(f"No uuid for file: {xml_rel_path}")
continue
valid, errors = _validate_xml(tree, schema_uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import create_mets_v2 as createmets2
import metsrw
import namespaces as ns
from django.core.exceptions import ValidationError
from lxml import etree
from main import models

Expand Down Expand Up @@ -562,12 +563,12 @@ def update_metadata_csv(job, mets, metadata_csv, sip_uuid, sip_dir, state):
file_obj = models.File.objects.get(
sip_id=str(sip_uuid), originallocation__endswith="%" + f
)
except models.File.DoesNotExist:
except (models.File.DoesNotExist, ValidationError):
try:
file_obj = models.File.objects.get(
sip_id=sip_uuid, currentlocation__endswith="%" + f
)
except models.File.DoesNotExist:
except (models.File.DoesNotExist, ValidationError):
pass
if file_obj is not None:
fsentry = mets.get_file(file_uuid=str(file_obj.uuid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import lxml.etree as etree
import namespaces as ns
from django.core.exceptions import ValidationError
from main.models import File

# dashboard
Expand Down Expand Up @@ -64,7 +65,7 @@
mets = File.objects.get(
currentlocation=path.encode(), transfer_id=transferUUID
)
except File.DoesNotExist:
except (File.DoesNotExist, ValidationError):

Check warning on line 68 in src/MCPClient/lib/clientScripts/archivematicaCreateMETSRightsDspaceMDRef.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/archivematicaCreateMETSRightsDspaceMDRef.py#L68

Added line #L68 was not covered by tests
pass
else:
metsFileUUID = mets.uuid
Expand All @@ -91,7 +92,7 @@
f = File.objects.get(
currentlocation=path.encode(), transfer_id=transferUUID
)
except File.DoesNotExist:
except (File.DoesNotExist, ValidationError):

Check warning on line 95 in src/MCPClient/lib/clientScripts/archivematicaCreateMETSRightsDspaceMDRef.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/archivematicaCreateMETSRightsDspaceMDRef.py#L95

Added line #L95 was not covered by tests
pass
else:
metsFileUUID = f.uuid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import lxml.etree as etree
import namespaces as ns
from django.core.exceptions import ValidationError
from main.models import File

# dashboard
Expand Down Expand Up @@ -124,7 +125,7 @@
filegrpuuid=fileUUID,
filegrpuse="TRIM file metadata",
)
except File.DoesNotExist:
except (File.DoesNotExist, ValidationError):

Check warning on line 128 in src/MCPClient/lib/clientScripts/archivematicaCreateMETSTrim.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/archivematicaCreateMETSTrim.py#L128

Added line #L128 was not covered by tests
job.pyprint("no metadata for original file: ", fileUUID, file=sys.stderr)
return None
else:
Expand Down Expand Up @@ -163,7 +164,7 @@
filegrpuuid=fileUUID,
filegrpuse="TRIM file metadata",
)
except File.DoesNotExist:
except (File.DoesNotExist, ValidationError):

Check warning on line 167 in src/MCPClient/lib/clientScripts/archivematicaCreateMETSTrim.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/archivematicaCreateMETSTrim.py#L167

Added line #L167 was not covered by tests
job.pyprint("no metadata for original file: ", fileUUID, file=sys.stderr)
return None
else:
Expand Down
3 changes: 2 additions & 1 deletion src/MCPClient/lib/clientScripts/archivematica_clamscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from custom_handlers import get_script_logger
from databaseFunctions import insertIntoEvents
from main.models import Event, File
from django.core.exceptions import ValidationError

logger = get_script_logger("archivematica.mcp.client.clamscan")

Expand Down Expand Up @@ -291,7 +292,7 @@
if file_uuid != "None":
try:
return File.objects.get(uuid=file_uuid).size
except File.DoesNotExist:
except (File.DoesNotExist, ValidationError):

Check warning on line 295 in src/MCPClient/lib/clientScripts/archivematica_clamscan.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/archivematica_clamscan.py#L295

Added line #L295 was not covered by tests
pass
# Our fallback.
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from archivematicaFunctions import get_dir_uuids, format_subdir_path, str2bool

from django.db import transaction
from django.core.exceptions import ValidationError

logger = get_script_logger("archivematica.mcp.client.assignUUIDsToDirectories")

Expand Down Expand Up @@ -92,7 +93,7 @@
transfer_mdl.diruuids = True
transfer_mdl.save()
return transfer_mdl
except Transfer.DoesNotExist:
except (Transfer.DoesNotExist, ValidationError):

Check warning on line 96 in src/MCPClient/lib/clientScripts/assign_uuids_to_directories.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/assign_uuids_to_directories.py#L96

Added line #L96 was not covered by tests
logger.warning("There is no transfer with UUID %s", transfer_uuid)
raise DirsUUIDsException

Expand Down
3 changes: 2 additions & 1 deletion src/MCPClient/lib/clientScripts/bind_pids.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
django.setup()
from django.db import transaction
from lxml import etree
from django.core.exceptions import ValidationError

# dashboard
from main.models import DashboardSetting, Directory, SIP
Expand Down Expand Up @@ -102,7 +103,7 @@
def _get_sip(sip_uuid):
try:
return SIP.objects.get(uuid=sip_uuid)
except SIP.DoesNotExist:
except (SIP.DoesNotExist, ValidationError):

Check warning on line 106 in src/MCPClient/lib/clientScripts/bind_pids.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/bind_pids.py#L106

Added line #L106 was not covered by tests
raise BindPIDsException


Expand Down
3 changes: 2 additions & 1 deletion src/MCPClient/lib/clientScripts/characterize_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from dicts import replace_string_values, ReplacementDict

from lib import setup_dicts
from django.core.exceptions import ValidationError


def concurrent_instances():
Expand All @@ -43,7 +44,7 @@ def main(job, file_path, file_uuid, sip_uuid):

try:
format = FormatVersion.active.get(fileformatversion__file_uuid=file_uuid)
except FormatVersion.DoesNotExist:
except (FormatVersion.DoesNotExist, ValidationError):
rules = format = None

if format:
Expand Down
6 changes: 3 additions & 3 deletions src/MCPClient/lib/clientScripts/create_mets_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
from change_names import change_name

from bagit import Bag, BagError

from django.core.exceptions import ValidationError

SIP_DIR_VAR = r"%SIPDirectory%"

Expand Down Expand Up @@ -1070,7 +1070,7 @@
}
try:
f = File.objects.get(**kwargs)
except File.DoesNotExist:
except (File.DoesNotExist, ValidationError):

Check warning on line 1073 in src/MCPClient/lib/clientScripts/create_mets_v2.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/create_mets_v2.py#L1073

Added line #L1073 was not covered by tests
job.pyprint(
'No uuid for file: "', directoryPathSTR, '"', file=sys.stderr
)
Expand Down Expand Up @@ -1203,7 +1203,7 @@
# Derived files should be in the original file's group
try:
d = Derivation.objects.get(derived_file_id=f.uuid)
except Derivation.DoesNotExist:
except (Derivation.DoesNotExist, ValidationError):

Check warning on line 1206 in src/MCPClient/lib/clientScripts/create_mets_v2.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/create_mets_v2.py#L1206

Added line #L1206 was not covered by tests
job.pyprint(
"Fatal error: unable to locate a Derivation object"
" where the derived file is {}".format(f.uuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

# archivematicaCommon
import archivematicaFunctions
from django.core.exceptions import ValidationError


def call(jobs):
Expand Down Expand Up @@ -117,7 +118,7 @@ def call(jobs):
unituuid=transferUUID,
variable="activeAgent",
)
except UnitVariable.DoesNotExist:
except (UnitVariable.DoesNotExist, ValidationError):
unit_variable = None
if unit_variable:
try:
Expand Down
3 changes: 2 additions & 1 deletion src/MCPClient/lib/clientScripts/create_transfer_mets.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import uuid

import django
from django.core.exceptions import ValidationError
from django.db.models import Prefetch
from lxml import etree

Expand Down Expand Up @@ -85,7 +86,7 @@

try:
transfer = Transfer.objects.get(uuid=transfer_uuid)
except Transfer.DoesNotExist:
except (Transfer.DoesNotExist, ValidationError):

Check warning on line 89 in src/MCPClient/lib/clientScripts/create_transfer_mets.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/create_transfer_mets.py#L89

Added line #L89 was not covered by tests
logger.info("No record in database for transfer: %s", transfer_uuid)
raise

Expand Down
8 changes: 2 additions & 6 deletions src/MCPClient/lib/clientScripts/dip_generation_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
import django
from agentarchives import archivesspace
from custom_handlers import get_script_logger
from django.core.exceptions import ValidationError
from django.db.models import Q
from main import models

# dashboard
# archivematicaCommon
# Third party dependencies, alphabetical by import source
# initialize Django (required for Django 1.7)

django.setup()
from django.db import transaction

Expand Down Expand Up @@ -105,7 +101,7 @@ def parse_archivesspace_ids(sip_path, sip_uuid):
| Q(originallocation=b"%SIPDirectory%objects/" + filename.encode()),
sip_id=sip_uuid,
)
except models.File.DoesNotExist:
except (models.File.DoesNotExist, ValidationError):
logger.error("%s not found in database, skipping", filename)
continue
except models.File.MultipleObjectsReturned:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

django.setup()
from django.db import transaction
from django.core.exceptions import ValidationError

# dashboard
from main.models import File
Expand Down Expand Up @@ -84,7 +85,7 @@ def getFileUUIDofSourceFile(transferUUID, sourceFilePath):
transfer_id=transferUUID,
currentlocation__startswith=sourceFilePath,
).uuid
except File.DoesNotExist:
except (File.DoesNotExist, ValidationError):
return ""


Expand Down
3 changes: 2 additions & 1 deletion src/MCPClient/lib/clientScripts/index_aip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import identifier_functions
import storageService as storage_service
from custom_handlers import get_script_logger
from django.core.exceptions import ValidationError
from main.models import UnitVariable

# dashboard
Expand Down Expand Up @@ -70,7 +71,7 @@ def index_aip(job):
unittype="SIP", unituuid=sip_uuid, variable="AIPsinAIC"
)
aips_in_aic = uv.variablevalue
except UnitVariable.DoesNotExist:
except (UnitVariable.DoesNotExist, ValidationError):
pass
# Delete ES index before creating new one if reingesting
if "REIN" in sip_type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# archivematicaCommon
import databaseFunctions
import fileOperations
from django.core.exceptions import ValidationError


def main(job):
Expand Down Expand Up @@ -61,7 +62,7 @@ def main(job):
filegrpuse="original",
sip_id=SIPUUID,
)
except (File.DoesNotExist, File.MultipleObjectsReturned) as e:
except (File.DoesNotExist, File.MultipleObjectsReturned, ValidationError) as e:
# Original file was not found, or there is more than one original file with
# the same filename (differing extensions)
# Look for a CSV that will specify the mapping
Expand All @@ -84,7 +85,7 @@ def main(job):
printfn=job.pyprint,
)
if original is None:
if isinstance(e, File.DoesNotExist):
if isinstance(e, File.DoesNotExist, ValidationError):
job.print_error(
"No matching file for: {}".format(
filePath.replace(SIPDirectory, "%SIPDirectory%")
Expand All @@ -106,7 +107,7 @@ def main(job):
sip_id=SIPUUID,
)
else:
if isinstance(e, File.DoesNotExist):
if isinstance(e, File.DoesNotExist, ValidationError):
job.print_error(
"No matching file for: ",
filePath.replace(SIPDirectory, "%SIPDirectory%", 1),
Expand Down Expand Up @@ -158,7 +159,7 @@ def main(job):
"Updated the eventOutcomeDetailNote of an existing normalization"
" Event for file {}. Not creating a Derivation object".format(fileUUID)
)
except Event.DoesNotExist:
except (Event.DoesNotExist, ValidationError):
# No normalization event was created in normalize.py - probably manually
# normalized during Ingest
derivationEventUUID = str(uuid.uuid4())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

# --sipUUID "%SIPUUID%" --sipDirectory "%SIPDirectory%" --filePath "%relativeLocation%"
from optparse import OptionParser
from django.core.exceptions import ValidationError


def main(job):
Expand Down Expand Up @@ -69,9 +70,9 @@ def main(job):
}
try:
f = File.objects.get(currentlocation__startswith=filePathLike, **kwargs)
except (File.DoesNotExist, File.MultipleObjectsReturned):
except (File.DoesNotExist, File.MultipleObjectsReturned, ValidationError):
f = File.objects.get(currentlocation=filePathLike2.encode(), **kwargs)
except (File.DoesNotExist, File.MultipleObjectsReturned) as e:
except (File.DoesNotExist, File.MultipleObjectsReturned, ValidationError) as e:
# Original file was not found, or there is more than one original file with
# the same filename (differing extensions)
# Look for a CSV that will specify the mapping
Expand All @@ -90,7 +91,7 @@ def main(job):
csv_path, "access", access_file, unitIdentifier, printfn=job.pyprint
)
if original is None:
if isinstance(e, File.DoesNotExist):
if isinstance(e, File.DoesNotExist, ValidationError):
job.print_error(
"No matching file for: {}".format(
opts.filePath.replace(opts.sipDirectory, "%SIPDirectory%")
Expand All @@ -113,7 +114,7 @@ def main(job):
}
f = File.objects.get(**kwargs)
else:
if isinstance(e, File.DoesNotExist):
if isinstance(e, File.DoesNotExist, ValidationError):
job.print_error(
"No matching file for: ",
opts.filePath.replace(opts.SIPDirectory, "%SIPDirectory%", 1),
Expand Down
4 changes: 2 additions & 2 deletions src/MCPClient/lib/clientScripts/move_to_backlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from bagit import make_bag
import metsrw

from django.core.exceptions import ValidationError

logger = get_script_logger("archivematica.mcp.client.move_to_backlog")

Expand Down Expand Up @@ -134,7 +134,7 @@
var = UnitVariable.objects.get(
unittype="Transfer", unituuid=transfer_id, variable="activeAgent"
)
except UnitVariable.DoesNotExist:
except (UnitVariable.DoesNotExist, ValidationError):

Check warning on line 137 in src/MCPClient/lib/clientScripts/move_to_backlog.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/move_to_backlog.py#L137

Added line #L137 was not covered by tests
pass
else:
query |= Q(id=var.variablevalue)
Expand Down
Loading