diff --git a/services/sources/publisher_backlist_service.py b/services/sources/publisher_backlist_service.py index ed41311794..82e687a7f6 100644 --- a/services/sources/publisher_backlist_service.py +++ b/services/sources/publisher_backlist_service.py @@ -35,29 +35,34 @@ def delete_records( for json_dict in array_json_records: if json_dict['records']: for records_value in json_dict['records']: - try: + if records_value['fields']: record_metadata_dict = records_value['fields'] self.delete_manifest(record_metadata_dict) - except Exception: - logger.exception(f'Delete manifest method has failed for {records_value}') def delete_manifest(self, record_metadata_dict): try: record = self.session.query(Record).filter(Record.source_id == record_metadata_dict['DRB Record_ID']).first() - key_format = f"{self.prefix}{record.source}" - - if record_metadata_dict['File ID 1']: - file_title = record_metadata_dict['File ID 1'] - self.s3_manager.s3Client.delete_object(Bucket= self.s3_bucket, Key= f'{key_format}{file_title}.json') - elif record_metadata_dict['File ID 2']: - file_title = record_metadata_dict['File ID 2'] - self.s3_manager.s3Client.delete_object(Bucket= self.s3_bucket, Key= f'{key_format}{file_title}.json') - elif record_metadata_dict['Hathi ID']: - file_title = record_metadata_dict['Hathi ID'] - self.s3_manager.s3Client.delete_object(Bucket= self.s3_bucket, Key= f'{key_format}{file_title}.json') + if record: + key_name = self.get_metadata_file_name(record, record_metadata_dict) + self.s3_manager.s3Client.delete_object(Bucket= self.s3_bucket, Key= key_name) except Exception: logger.exception(f'Failed to delete manifest for record: {record.source_id}') + def get_metadata_file_name(self, record, record_metadata_dict): + key_format = f"{self.prefix}{record.source}" + + if record_metadata_dict['File ID 1']: + file_title = record_metadata_dict['File ID 1'] + elif record_metadata_dict['File ID 2']: + file_title = record_metadata_dict['File ID 2'] + elif record_metadata_dict['Hathi ID']: + file_title = record_metadata_dict['Hathi ID'] + else: + raise Exception + + key_name = f'{key_format}{file_title}.json' + return key_name + def get_records( self, full_import: bool=False,