Skip to content

Commit

Permalink
Update references in file-input SchemaResponseBlocks to match registr…
Browse files Browse the repository at this point in the history
…ation_responses post-archival (#9804)

* Update references in file-input blocks to match registration_responses post-archival


Co-authored-by: Jon Walz <jonwalz@Admins-MBP.lan>
  • Loading branch information
jwalz and Jon Walz committed Oct 15, 2021
1 parent 83f740d commit 4823da7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions osf/models/schema_response.py
Expand Up @@ -286,6 +286,28 @@ def _update_response(self, current_block, latest_response):
self.response_blocks.remove(current_block)
self.response_blocks.add(revised_block)

def _update_file_references(self, updated_responses, file_block_ids=None, save=True):
'''Update refernces in file-input responses post-archival for initial SchemaResponses.'''
if self.previous_response:
raise PreviousSchemaResponseError(
'Updating of file references only supported for initial responses'
)

if file_block_ids is None:
file_block_ids = self.schema.schema_blocks.filter(
block_type='file-input'
).values_list('id', flat=True)
if not file_block_ids:
return

file_response_blocks = self.response_blocks.filter(
source_schema_block_id__in=file_block_ids
)
for block in file_response_blocks:
block.response = updated_responses[block.schema_key]
if save:
block.save()

def delete(self, *args, **kwargs):
if self.state is not ApprovalStates.IN_PROGRESS:
raise SchemaResponseStateError(
Expand Down
13 changes: 13 additions & 0 deletions osf_tests/test_schema_responses.py
Expand Up @@ -491,6 +491,19 @@ def test_update_fails_if_state_is_invalid(self, invalid_response_state, initial_
with assert_raises(SchemaResponseStateError):
initial_response.update_responses({'q1': 'harrumph'})

def test_update_file_references(self, initial_response):
original_responses = initial_response.all_responses
new_responses = dict(
original_responses, q1='new value', q6=['some', 'file', 'metadata']
)

initial_response._update_file_references(new_responses)

updated_responses = initial_response.all_responses

assert updated_responses['q1'] == original_responses['q1']
assert updated_responses['q6'] == new_responses['q6']


@pytest.mark.django_db
class TestDeleteSchemaResponse():
Expand Down
2 changes: 2 additions & 0 deletions website/archiver/utils.py
Expand Up @@ -343,5 +343,7 @@ def migrate_file_metadata(dst, schema):

dst.registered_meta[schema._id] = metadata
dst.registration_responses = dst.flatten_registration_metadata()
if dst.root_id == dst.id: # Also fix the initial SchemaResponse for root registrations
dst.schema_responses.get()._update_file_references(dst.registration_responses)

dst.save()

0 comments on commit 4823da7

Please sign in to comment.