Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IfSuppressedFile generator #5

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion ultraviolet_permissions/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@
from invenio_records_permissions.generators import Generator
from flask_login import current_user

from invenio_files_rest.models import Location
from invenio_rdm_records.proxies import current_rdm_records


SUPPRESSED_NAME = "suppressed"


def get_files_from_pid(record_pid):
record = current_rdm_records.records_service.read(
system_identity, record_pid
)
record = current_rdm_records.records_service.record_cls.pid.resolve(
record_pid
)
file_manager = record.files
for file_key in file_manager:
pprint(file_key)
pprint(record.files[file_key].file.uri)
pprint(record.files[file_key].__dir__())
pprint(record.files[file_key].metadata)


def get_suppressed_location_uri():
return Location.get_by_name(SUPPRESSED_NAME).uri


def is_suppressed(record_file):
return record_file.uri.startswith(get_suppressed_location_uri())


def get_roles(record, user_role):
# roles = []
Expand Down Expand Up @@ -198,4 +227,29 @@ def needs(self, record=None, **kwargs):
def query_filter(self, **kwargs):
"""Filters for current identity as super user."""
# TODO: Implement with new permissions metadata
return dsl.Q('match_all')
return dsl.Q('match_all')


class IfSuppressedFile(Generator):
"""Conditional generator for suppressed files."""

def _condition(self, record, file_key=None, **kwargs):
is_file_suppressed = False
if file_key:
file_record = record.files.get(file_key)
file = file_record.file if file_record is not None else None
is_file_suppressed = file and is_suppressed(file)
else:
# files_records = record.files.entries
# for file_record in file_records:
# file = file_record.file
# if file and not is_suppressed(file):
# is_file_suppressed = False
# break
is_suppressed = all(
[
file_record.file and is_suppressed(file_record.file)
for file_record in file_records
]
)
return is_file_suppressed