Skip to content

Commit

Permalink
records: files update on publishing edited
Browse files Browse the repository at this point in the history
Signed-off-by: Parth Shandilya <[email protected]>
  • Loading branch information
ParthS007 committed May 11, 2023
1 parent 309f3bb commit 4fca403
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cap/modules/deposit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/deposits/test_edit_published_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

0 comments on commit 4fca403

Please sign in to comment.