Skip to content

Commit

Permalink
Merge a90feb0 into 34c3793
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Délèze committed Dec 10, 2019
2 parents 34c3793 + a90feb0 commit a847586
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 149 deletions.
5 changes: 3 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "pypi"
[packages]
Babel = ">=2.4.0"
Flask-BabelEx = ">=0.9.3"
invenio = {version = "==3.1.0",extras = ["base", "metadata", "postgresql", "auth", "elasticsearch6" ]}
invenio = {version = "==3.2.0a9",extras = ["base", "metadata", "files", "postgresql", "auth", "elasticsearch6" ]}
# TODO: remove this contraint once it is solved in invenio
raven = {version = ">=6.0",extras = ["flask"]}
uwsgi = ">=2.0"
Expand All @@ -17,6 +17,7 @@ orcid = "*"
python-slugify = "*"
python3-saml = "*"
xmltodict = "*"
marshmallow = "<=3.0.0b6"

[dev-packages]
Flask-Debugtoolbar = ">=0.10.1"
Expand All @@ -29,7 +30,7 @@ marshmallow = ">=2.15.1,<3.0.0"
pydocstyle = ">=2.0.0"
pytest = ">=3.3.1"
pytest-cov = ">=2.5.1"
pytest-invenio = ">=1.0.2,<1.1.0"
pytest-invenio = ">=1.2.1,<1.3.0"
pytest-mock = ">=1.6.0"
pytest-pep8 = ">=1.0.6"
pytest-random-order = ">=0.5.4"
Expand Down
535 changes: 396 additions & 139 deletions Pipfile.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion scripts/setup
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ pipenv run invenio access allow admin-access role moderator
pipenv run invenio fixtures institutions import
pipenv run invenio fixtures users import $(pipenv --where)/data/users.json
pipenv run invenio fixtures documents import hevs
pipenv run invenio fixtures documents import usi
pipenv run invenio fixtures documents import usi
pipenv run invenio fixtures deposits create
6 changes: 0 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,5 @@
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Development Status :: 3 - Alpha',
],
setup_requires=[
'pytest-runner>=3.0.0,<5',
],
tests_require=[
'pytest-invenio>=1.0.0,<1.1.0',
]
)
3 changes: 3 additions & 0 deletions sonar/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,6 @@ def _(x):
REST_ENABLE_CORS = True
"""Enable CORS to make it possible to do request to API from other
applications."""

FILES_REST_PERMISSION_FACTORY = \
'sonar.modules.permissions.files_permission_factory'
2 changes: 2 additions & 0 deletions sonar/modules/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import click

from .deposits.cli import deposits
from .documents.cli import documents
from .institutions.cli import institutions
from .users.cli import users
Expand All @@ -31,3 +32,4 @@ def fixtures():
fixtures.add_command(documents)
fixtures.add_command(institutions)
fixtures.add_command(users)
fixtures.add_command(deposits)
21 changes: 21 additions & 0 deletions sonar/modules/deposits/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2019 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


"""Deposit module."""

from __future__ import absolute_import, print_function
64 changes: 64 additions & 0 deletions sonar/modules/deposits/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2019 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Deposit CLI commands."""

import shutil
from os import makedirs
from os.path import exists, join

import click
from flask import current_app
from flask.cli import with_appcontext
from invenio_db import db
from invenio_files_rest.models import Bucket, FileInstance, Location, \
ObjectVersion


@click.group()
def deposits():
"""Deposits CLI commands."""


@deposits.command('create')
@with_appcontext
def create():
"""Create a location and a bucket for uploading files."""
click.secho('Creating default location for importing files', bold=True)

# Directory where files are stored
directory = join(current_app.instance_path, 'files')

if exists(directory):
shutil.rmtree(directory)

makedirs(directory)

# Remove stored data
ObjectVersion.query.delete()
Bucket.query.delete()
FileInstance.query.delete()
Location.query.delete()
db.session.commit()

# Create location
loc = Location(name='local', uri=directory, default=True)
db.session.add(loc)
db.session.commit()

click.secho('Location #{id} created successfully'.format(id=loc.id),
fg='green')
7 changes: 7 additions & 0 deletions sonar/modules/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
superuser_access_permission = Permission(ActionNeed('superuser-access'))
admin_access_permission = Permission(RoleNeed('librarian'), RoleNeed('admin'))
moderator_access_permission = Permission(ActionNeed('admin-access'))
user_access_permission = Permission(RoleNeed('user'),
RoleNeed('librarian'), RoleNeed('admin'))


def has_admin_access():
Expand Down Expand Up @@ -89,3 +91,8 @@ def decorated_view(*args, **kwargs):
def admin_permission_factory(admin_view):
"""Admin permission factory."""
return superuser_access_permission


def files_permission_factory(*kwargs):
"""Files rest permission factory."""
return user_access_permission
11 changes: 10 additions & 1 deletion tests/ui/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from sonar.modules.documents.api import DocumentRecord
from sonar.modules.permissions import can_create_record_factory, \
can_delete_record_factory, can_list_record_factory, \
can_read_record_factory, can_update_record_factory
can_read_record_factory, can_update_record_factory, \
files_permission_factory


def test_has_admin_access(app, db, client, admin_user_fixture):
Expand Down Expand Up @@ -64,3 +65,11 @@ def test_can_list_record_factory(app, client, admin_user_fixture):
}, dbcommit=True)

assert can_read_record_factory(record)


def test_files_permission_factory(client, admin_user_fixture):
"""Test files permission factory."""
login_user_via_view(client,
email=admin_user_fixture.email,
password='123456')
assert files_permission_factory().can()

0 comments on commit a847586

Please sign in to comment.