Skip to content

Commit

Permalink
feat: file transfer registry update
Browse files Browse the repository at this point in the history
* Implementation of RFC 0072 - Pluggable Transfer Types for Record Files

* Needs inveniosoftware/invenio-records-resources#604
  • Loading branch information
mesemus committed Jan 21, 2025
1 parent 7428e58 commit 308e80b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,4 @@ dmypy.json

# IDE
.idea/
.vscode/
11 changes: 8 additions & 3 deletions invenio_drafts_resources/services/records/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

"""Base class for service components."""
from invenio_i18n import gettext as _
from invenio_records_resources.services.files.transfer import TransferType
from invenio_records_resources.proxies import current_transfer_registry
from invenio_records_resources.services.files.transfer.base import TransferStatus
from invenio_records_resources.services.records.components import (
BaseRecordFilesComponent as _BaseRecordFilesComponent,
)
Expand Down Expand Up @@ -236,8 +237,12 @@ def _check_file_completed(self, file_record):
has_attached_object = file_record.file is not None
if not has_attached_object:
return False
transfer = TransferType(file_record.file.storage_class)
if transfer.is_completed:
transfer = current_transfer_registry.get_transfer(
record=file_record.record,
file_record=file_record,
file_service=self.service.draft_files,
)
if transfer.status == TransferStatus.COMPLETED:
return True

def publish(self, identity, draft=None, record=None):
Expand Down
6 changes: 4 additions & 2 deletions tests/mock_module/permissions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Example of a permission policy."""

from invenio_records_permissions.generators import AnyUser
from invenio_records_permissions.generators import AnyUser, SystemProcess

from invenio_drafts_resources.services.records.permissions import RecordPermissionPolicy

Expand Down Expand Up @@ -33,7 +33,9 @@ class PermissionPolicy(RecordPermissionPolicy):
can_draft_media_update_files = [AnyUser()]
can_draft_media_delete_files = [AnyUser()]

can_draft_create_files = [AnyUser()]
# SystemProcess is needed for metadata extraction -
# there is a 'create' action check there
can_draft_create_files = [AnyUser(), SystemProcess()]
can_draft_set_content_files = [AnyUser()]
can_draft_get_content_files = [AnyUser()]
can_draft_commit_files = [AnyUser()]
Expand Down
16 changes: 10 additions & 6 deletions tests/services/test_record_service_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import pytest
from invenio_files_rest.errors import BucketLockedError, InvalidOperationError
from invenio_files_rest.models import Bucket, FileInstance, ObjectVersion
from invenio_records_resources.services.files.transfer import TransferType
from invenio_records_resources.services.files.transfer.constants import (
FETCH_TRANSFER_TYPE,
)
from marshmallow.exceptions import ValidationError
from mock_module.models import DraftMetadata, FileDraftMetadata, FileRecordMetadata
from mock_module.permissions import PermissionPolicy
Expand Down Expand Up @@ -486,17 +488,19 @@ def test_update_draft_set_default_file_preview_reports_error(
assert draft.data["files"] == {"enabled": True}


@patch("invenio_records_resources.services.files.transfer.fetch_file")
@patch("invenio_records_resources.services.files.tasks.requests.get")
def test_publish_with_fetch_files(
p_fetch_file, app, service, draft_file_service, input_data, identity_simple
p_response_raw, app, service, draft_file_service, input_data, identity_simple
):
"""Tests wether it is possible to submit a record if the file isn't fully downloaded."""
draft = service.create(identity_simple, input_data) # 1
file_to_initialise = [
{
"key": "article.txt",
"uri": "https://inveniordm.test/files/article.txt",
"storage_class": "F",
"transfer": {
"type": FETCH_TRANSFER_TYPE,
"url": "https://inveniordm.test/files/article.txt",
},
}
]

Expand All @@ -505,7 +509,7 @@ def test_publish_with_fetch_files(
)

for file_record in files.entries:
assert file_record["storage_class"] == TransferType.FETCH
assert file_record["transfer"]["type"] == FETCH_TRANSFER_TYPE

with pytest.raises(ValidationError):
service.publish(identity_simple, draft.id)
Expand Down

0 comments on commit 308e80b

Please sign in to comment.