From 1f39e37bd69c13381c1f08386b3967b622aed81f Mon Sep 17 00:00:00 2001 From: Parth Shandilya <24358501+ParthS007@users.noreply.github.com> Date: Wed, 10 May 2023 15:28:17 +0200 Subject: [PATCH] records: files update on publishing edited Signed-off-by: Parth Shandilya --- cap/modules/deposit/api.py | 11 ++++++++ .../deposits/test_edit_published_deposit.py | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/cap/modules/deposit/api.py b/cap/modules/deposit/api.py index 44d065d7f5..a971dd4abb 100644 --- a/cap/modules/deposit/api.py +++ b/cap/modules/deposit/api.py @@ -232,6 +232,17 @@ def permissions(self, pid=None): def _publish_edited(self): record = super(CAPDeposit, self)._publish_edited() record._add_deposit_permissions(record, record.id) + with db.session.begin_nested(): + if self.files and record.files.bucket is not None: + # Unlock the record bucket + record.files.bucket.locked = False + # Lock the deposit's files bucket + self.files.bucket.locked = True + # Sync the record files with the deposit files + self.files.bucket.sync(record.files.bucket) + # lock the record bucket after update + record.files.bucket.locked = True + db.session.commit() if record["_experiment"]: record._add_experiment_permissions(record, record.id) diff --git a/tests/integration/deposits/test_edit_published_deposit.py b/tests/integration/deposits/test_edit_published_deposit.py index bb140e116f..7297f12963 100644 --- a/tests/integration/deposits/test_edit_published_deposit.py +++ b/tests/integration/deposits/test_edit_published_deposit.py @@ -24,6 +24,8 @@ # or submit itself to any jurisdiction. """Integration tests for record edit.""" +from io import BytesIO + ########################################### # api/deposits/{pid}/actions/edit [POST] @@ -130,3 +132,26 @@ def test_edit_record(client, create_deposit, users, auth_headers_for_superuser): .format(depid) } } + + +def test_edit_record_with_uploading_new_files(client, users, auth_headers_for_user, create_deposit): + owner = users['cms_user'] + deposit = create_deposit(owner, 'test-analysis-v0.0.1') + deposit.files['file_1.txt'] = BytesIO(b'Hello world!') + pid = deposit['_deposit']['id'] + + client.post('/deposits/{}/actions/publish'.format(pid), + headers=auth_headers_for_user(owner)) + + client.post('/deposits/{}/actions/edit'.format(pid), + headers=auth_headers_for_user(owner)) + + bucket = deposit.files.bucket + client.put('/files/{}/file_2.txt'.format(bucket), + input_stream=BytesIO(b'Hello brave new world!'), + headers=auth_headers_for_user(owner)) + + resp = client.post('/deposits/{}/actions/publish'.format(pid), + headers=auth_headers_for_user(owner)) + + assert len(resp.json['files']) == 2