Skip to content

Commit

Permalink
manager: improe copy func
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Nov 3, 2023
1 parent 12dccad commit b962ceb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion invenio_records_resources/records/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def object_version_id(cls):
@declared_attr
def object_version(cls):
"""Object version connected to the record file."""
return db.relationship(ObjectVersion)
return db.relationship(ObjectVersion) # Can we cache this?

@declared_attr
def __table_args__(cls):
Expand Down
22 changes: 13 additions & 9 deletions invenio_records_resources/records/systemfields/files/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,22 @@ def copy(self, src_files, copy_obj=True):
if not self.enabled:
return

for key, rf in src_files.items():
# Copy object version of link existing?
if copy_obj:
dst_obj = rf.object_version.copy(bucket=self.bucket)
else:
dst_obj = rf.object_version
object_version_ids = [rf.object_version_id for rf in src_files.values() if rf.object_version_id]

if copy_obj:
object_versions = ObjectVersion.copy_many(object_version_ids, self.bucket)
else:
object_versions = ObjectVersion.query.filter(
ObjectVersion.version_id.in_(object_version_ids)
).all()

key_object_versions = {obj.key: obj for obj in object_versions}

# Copy file record
for key, rf in src_files.items():
if rf.metadata is not None:
self[key] = dst_obj, rf.metadata
self[key] = key_object_versions.get(key), rf.metadata
else:
self[key] = dst_obj
self[key] = key_object_versions.get(key)

self.default_preview = src_files.default_preview
self.order = src_files.order
Expand Down
2 changes: 1 addition & 1 deletion tests/records/test_systemfield_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def test_record_files_dump(base_app, db, location):
"entries": [
{
"uuid": str(rf.id),
"version_id": 3,
"version_id": 2,
"metadata": {"description": "Test file"},
"checksum": "md5:8bc944dbd052ef51652e70a5104492e3",
"key": "f1.txt",
Expand Down

0 comments on commit b962ceb

Please sign in to comment.