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

PR #2861, 2862, 2865, 2866 #2867

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 18 additions & 1 deletion cap/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ def _(x):
'application/basic+json': (
'cap.modules.records.serializers' ':basic_json_v1_search'
),
'application/marcxml+xml': (
'cap.modules.records.serializers' ':record_xml_v1_search'
),
'application/csv': (
'cap.modules.records.serializers' ':record_csv_v1_search'
),
},
'read_permission_factory_imp': check_oauth2_scope(
lambda record: ReadRecordPermission(record).can(), write_scope.id
Expand Down Expand Up @@ -761,10 +767,16 @@ def _(x):
'application/basic+json': (
'cap.modules.records.serializers' ':basic_json_v1_search'
),
'application/marcxml+xml': (
'cap.modules.records.serializers' ':record_xml_v1_search'
),
'application/csv': (
'cap.modules.records.serializers' ':record_csv_v1_search'
),
},
'files_serializers': {
'application/json': (
'cap.modules.deposit.serializers:files_response'
'invenio_deposit.serializers:json_file_response'
),
},
'search_class': 'cap.modules.deposit.search:CAPDepositSearch',
Expand Down Expand Up @@ -956,3 +968,8 @@ def get_cms_stats_questionnaire_contacts():

#: Enable Prometheus flask exporter
PROMETHEUS_ENABLE_EXPORTER_FLASK = False

#: CERN E-groups
CERN_EGROUP_ACCOUNT_USERNAME = "CHANGE_ME"
CERN_EGROUP_ACCOUNT_PASSWORD = "CHANGE_ME"
CERN_EGROUP_ACCOUNT_DEFAULT_OWNER_ID = "CHANGE_ME_NUMBER"
2 changes: 0 additions & 2 deletions cap/modules/deposit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@
"""CAP Deposit module."""

from __future__ import absolute_import, print_function

from .cli import create_deposit # noqa
50 changes: 27 additions & 23 deletions cap/modules/deposit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,33 @@
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.local import LocalProxy

from cap.modules.deposit.errors import DisconnectWebhookError, FileUploadError
from cap.modules.deposit.egroups import CERNEgroupMixin
from cap.modules.deposit.errors import (
DepositValidationError,
DisconnectWebhookError,
FileUploadError,
ReviewError,
UniqueRequiredValidationError,
UpdateDepositPermissionsError,
)
from cap.modules.deposit.fetchers import cap_deposit_fetcher
from cap.modules.deposit.loaders import get_val_from_path
from cap.modules.deposit.minters import cap_deposit_minter
from cap.modules.deposit.permissions import (
AdminDepositPermission,
CloneDepositPermission,
DepositAdminActionNeed,
DepositReadActionNeed,
DepositUpdateActionNeed,
ReviewDepositPermission,
UpdateDepositPermission,
)
from cap.modules.deposit.review import Reviewable
from cap.modules.deposit.utils import perform_copying_fields
from cap.modules.deposit.validators import NoRequiredValidator
from cap.modules.deposit.validators import (
NoRequiredValidator,
get_custom_validator,
)
from cap.modules.experiments.permissions import exp_need_factory
from cap.modules.records.api import CAPRecord
from cap.modules.records.errors import get_error_path
Expand All @@ -77,33 +100,14 @@
get_existing_or_register_user,
)

from .errors import (
DepositValidationError,
ReviewError,
UniqueRequiredValidationError,
UpdateDepositPermissionsError,
)
from .fetchers import cap_deposit_fetcher
from .minters import cap_deposit_minter
from .permissions import (
AdminDepositPermission,
CloneDepositPermission,
DepositAdminActionNeed,
DepositReadActionNeed,
DepositUpdateActionNeed,
ReviewDepositPermission,
UpdateDepositPermission,
)
from .review import Reviewable
from .validators import get_custom_validator

_datastore = LocalProxy(lambda: current_app.extensions["security"].datastore)

PRESERVE_FIELDS = (
"_deposit",
"_buckets",
"_files",
"_review",
"_egroups",
"_experiment",
"_access",
"_user_edited",
Expand Down Expand Up @@ -134,7 +138,7 @@ def DEPOSIT_ACTIONS_NEEDS(id):
}


class CAPDeposit(Deposit, Reviewable):
class CAPDeposit(Deposit, Reviewable, CERNEgroupMixin):
"""Define API for changing deposit state."""

deposit_fetcher = staticmethod(cap_deposit_fetcher)
Expand Down
94 changes: 61 additions & 33 deletions cap/modules/deposit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

from __future__ import absolute_import, print_function

import os
import copy
import json
import os
import uuid
from datetime import datetime

Expand All @@ -42,13 +42,16 @@
from cap.modules.deposit.api import CAPDeposit
from cap.modules.deposit.fetchers import cap_deposit_fetcher
from cap.modules.deposit.minters import cap_deposit_minter
from cap.modules.deposit.utils import add_read_permission_for_egroup
from cap.modules.fixtures.cli import fixtures
from cap.modules.user.utils import get_existing_or_register_user, \
get_existing_or_register_role
from cap.modules.schemas.resolvers import resolve_schema_by_url, \
schema_name_to_url

from .utils import add_read_permission_for_egroup
from cap.modules.schemas.resolvers import (
resolve_schema_by_url,
schema_name_to_url,
)
from cap.modules.user.utils import (
get_existing_or_register_role,
get_existing_or_register_user,
)


@fixtures.command('add')
Expand Down Expand Up @@ -80,7 +83,8 @@ def add(file_path, schema, version, egroup, usermail, limit):

click.secho(
'Draft with id {} already exist!'.format(pid_value),
fg='red')
fg='red',
)

except PIDDoesNotExistError:
record_uuid = uuid.uuid4()
Expand All @@ -91,29 +95,45 @@ def add(file_path, schema, version, egroup, usermail, limit):
if egroup:
add_read_permission_for_egroup(deposit, egroup)

click.secho('Draft {} added.'.format(pid.pid_value),
fg='green')
click.secho('Draft {} added.'.format(pid.pid_value), fg='green')

db.session.commit()


@fixtures.command('create-deposit')
@click.option('--file', '-f',
type=click.Path(exists=True),
required=True,
help='JSON data file')
@click.option('--ana', '-a',
help='Type of analysis',)
@click.option('--role', '-r',
'roles', multiple=True,
help='Role with access to the record')
@click.option('--user', '-u',
'users', multiple=True,
help='User with access to the record')
@click.option('--owner', '-o',
help='Owner of the record')
@click.option('--save-errors-to', '-e', 'save_errors',
help="Provide a filename, that wrong records will be saved to.")
@click.option(
'--file',
'-f',
type=click.Path(exists=True),
required=True,
help='JSON data file',
)
@click.option(
'--ana',
'-a',
help='Type of analysis',
)
@click.option(
'--role',
'-r',
'roles',
multiple=True,
help='Role with access to the record',
)
@click.option(
'--user',
'-u',
'users',
multiple=True,
help='User with access to the record',
)
@click.option('--owner', '-o', help='Owner of the record')
@click.option(
'--save-errors-to',
'-e',
'save_errors',
help="Provide a filename, that wrong records will be saved to.",
)
@with_appcontext
def create_deposit(file, ana, roles, users, owner, save_errors):
"""Create a new deposit through the CLI.
Expand Down Expand Up @@ -146,8 +166,7 @@ def save_errors_to_json(save_errors, errors):
"""Saves the wrong records to a specified file."""
timestamp = datetime.now().strftime("%d-%b-%Y-%H:%M:%S")
wrong_records_path = os.path.join(
os.getcwd(),
f'{save_errors}_errors_{timestamp}.json'
os.getcwd(), f'{save_errors}_errors_{timestamp}.json'
)

with open(wrong_records_path, 'w') as _json:
Expand All @@ -165,13 +184,18 @@ def check_and_update_data_with_schema(data, ana):
if not schema and not ana:
click.secho(
'You need to provide the --ana/-a parameter OR '
'add the $schema field in your JSON', fg='red')
'add the $schema field in your JSON',
fg='red',
)
return False

try:
if schema:
if ana:
click.secho("Your data already provide a $schema, --ana will not be used.") # noqa
click.secho(
"Your data already provide a $schema,"
" --ana will not be used."
)
resolve_schema_by_url(schema)
elif ana:
data['$schema'] = schema_name_to_url(ana)
Expand Down Expand Up @@ -201,12 +225,14 @@ def create_deposit_with_permissions(data, roles, users, owner, ana, errors):
for role in roles:
_role = get_existing_or_register_role(role.strip())
deposit._add_egroup_permissions(
_role, ['deposit-read'], db.session)
_role, ['deposit-read'], db.session
)
if users:
for user in users:
_user = get_existing_or_register_user(user.strip())
deposit._add_user_permissions(
_user, ['deposit-read'], db.session)
_user, ['deposit-read'], db.session
)

deposit.commit()
except ValidationError as err:
Expand All @@ -215,4 +241,6 @@ def create_deposit_with_permissions(data, roles, users, owner, ana, errors):
return

db.session.commit()
click.secho(f"Created deposit with id: {deposit['_deposit']['id']}", fg='green') # noqa
click.secho(
f"Created deposit with id: {deposit['_deposit']['id']}", fg='green'
) # noqa
Loading
Loading