From a5110a5e4eac423e1a6240a96e9bc1205755439f Mon Sep 17 00:00:00 2001 From: Abram Booth Date: Wed, 23 Aug 2023 19:58:36 -0400 Subject: [PATCH] fix: handle surprising file metadata states - osfstorage FileVersion with null creator - osfstorage file without versions (fix bug in `_hashes`) --- addons/osfstorage/models.py | 10 ++++++---- osf/metadata/osf_gathering.py | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/addons/osfstorage/models.py b/addons/osfstorage/models.py index 28b7e1bc4153..bbac5b36a605 100644 --- a/addons/osfstorage/models.py +++ b/addons/osfstorage/models.py @@ -232,8 +232,9 @@ class OsfStorageFile(OsfStorageFileNode, File): @property def _hashes(self): - latest_version = self.versions.latest('identifier') - if not latest_version: + try: + latest_version = self.versions.latest('identifier') + except FileVersion.DoesNotExist: return None return { 'sha1': latest_version.metadata.get('sha1', ''), @@ -243,8 +244,9 @@ def _hashes(self): @property def last_known_metadata(self): - latest_version = self.versions.latest('identifier') - if not latest_version: + try: + latest_version = self.versions.latest('identifier') + except FileVersion.DoesNotExist: size = None else: size = latest_version.size diff --git a/osf/metadata/osf_gathering.py b/osf/metadata/osf_gathering.py index 5812d1857e9b..590539ce6727 100644 --- a/osf/metadata/osf_gathering.py +++ b/osf/metadata/osf_gathering.py @@ -578,7 +578,8 @@ def gather_versions(focus): def _gather_fileversion(fileversion, fileversion_iri): yield (fileversion_iri, RDF.type, OSF.FileVersion) - yield (fileversion_iri, DCTERMS.creator, OsfFocus(fileversion.creator)) + if fileversion.creator is not None: + yield (fileversion_iri, DCTERMS.creator, OsfFocus(fileversion.creator)) yield (fileversion_iri, DCTERMS.created, fileversion.created) yield (fileversion_iri, DCTERMS.modified, fileversion.modified) yield (fileversion_iri, DCTERMS['format'], fileversion.content_type)