Skip to content

Commit 0b995b7

Browse files
committed
records: files update on publishing edited
Signed-off-by: Parth Shandilya <[email protected]>
1 parent 6966db4 commit 0b995b7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

cap/modules/deposit/api.py

+28
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ def _publish_edited(self):
233233
record = super(CAPDeposit, self)._publish_edited()
234234
record._add_deposit_permissions(record, record.id)
235235

236+
with db.session.begin_nested():
237+
if self.files and record.files.bucket is not None:
238+
# Unlock the record bucket
239+
record.files.bucket.locked = False
240+
# Lock the deposit's files bucket
241+
self.files.bucket.locked = True
242+
# Update the record files with the deposit files
243+
update_record_files(self.files, record.files)
244+
# lock the record bucket after update
245+
record.files.bucket.locked = True
246+
236247
if record["_experiment"]:
237248
record._add_experiment_permissions(record, record.id)
238249

@@ -862,6 +873,23 @@ def check_data(
862873
return data
863874

864875

876+
def update_record_files(deposit_files, record_files):
877+
for key in deposit_files.keys:
878+
deposit_file = deposit_files.__getitem__(key)
879+
deposit_version_id = deposit_file.get_version()
880+
881+
try:
882+
record_file = record_files.__getitem__(key)
883+
if record_file.get('version_id', None) == deposit_version_id:
884+
continue
885+
except KeyError:
886+
record_file = None
887+
888+
with deposit_file.obj.file.storage().open() as file_stream:
889+
record_files[key] = file_stream
890+
record_files.flush()
891+
892+
865893
def has_changed(error, current, new):
866894
error_path = get_error_path(error)
867895
current_version = get_val_from_path(current, error_path) or None

tests/integration/deposits/test_edit_published_deposit.py

+25
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# or submit itself to any jurisdiction.
2525
"""Integration tests for record edit."""
2626

27+
from io import BytesIO
28+
2729

2830
###########################################
2931
# api/deposits/{pid}/actions/edit [POST]
@@ -130,3 +132,26 @@ def test_edit_record(client, create_deposit, users, auth_headers_for_superuser):
130132
.format(depid)
131133
}
132134
}
135+
136+
137+
def test_edit_record_with_uploading_new_files(client, users, auth_headers_for_user, create_deposit):
138+
owner = users['cms_user']
139+
deposit = create_deposit(owner, 'test-analysis-v0.0.1')
140+
deposit.files['file_1.txt'] = BytesIO(b'Hello world!')
141+
pid = deposit['_deposit']['id']
142+
143+
client.post('/deposits/{}/actions/publish'.format(pid),
144+
headers=auth_headers_for_user(owner))
145+
146+
client.post('/deposits/{}/actions/edit'.format(pid),
147+
headers=auth_headers_for_user(owner))
148+
149+
bucket = deposit.files.bucket
150+
client.put('/files/{}/file_2.txt'.format(bucket),
151+
input_stream=BytesIO(b'Hello brave new world!'),
152+
headers=auth_headers_for_user(owner))
153+
154+
resp = client.post('/deposits/{}/actions/publish'.format(pid),
155+
headers=auth_headers_for_user(owner))
156+
157+
assert len(resp.json['files']) == 2

0 commit comments

Comments
 (0)