-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
306 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2019 CERN. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Invenio-Records-Files configuration.""" | ||
|
||
|
||
RECORDS_FILES_RESOURCE_NAME = 'files' | ||
"""/records/<pid>/<RECORDS_FILES_RESOURCE_NAME>.""" | ||
|
||
RECORDS_FILES_REST_ENDPOINTS = {} | ||
"""REST endpoints which will have files mounted. | ||
If we wanted to attach files to ``recid``'s and ``depid``'s the configuration | ||
would be the following: | ||
.. code-block:: | ||
RECORDS_FILES_REST_ENDPOINTS = { | ||
'RECORDS_REST_ENDPOINTS': [ | ||
'recid', | ||
], | ||
'DEPOSIT_REST_ENDPOINTS': [ | ||
'depid', | ||
] | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2019 CERN. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Flask extension for the Invenio-Records-Files.""" | ||
|
||
|
||
from __future__ import absolute_import, print_function | ||
|
||
from invenio_records_files import config | ||
|
||
|
||
class InvenioRecordsFiles(object): | ||
"""Invenio-Records-Files extension.""" | ||
|
||
def __init__(self, app=None, **kwargs): | ||
"""Extension initialization.""" | ||
if app: | ||
self.init_app(app, **kwargs) | ||
|
||
def init_app(self, app): | ||
"""Flask application initialization.""" | ||
self.init_config(app) | ||
app.extensions['invenio-records-files'] = self | ||
|
||
def init_config(self, app): | ||
"""Initialize configuration.""" | ||
for k in dir(config): | ||
if k.startswith('RECORDS_FILES_'): | ||
app.config.setdefault(k, getattr(config, k)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2019 CERN. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Invenio-Records-Files REST integration.""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
from flask import Blueprint, abort, g | ||
from invenio_files_rest.views import bucket_view, object_view | ||
|
||
from invenio_records_files.models import RecordsBuckets | ||
|
||
|
||
def create_blueprint_from_app(app): | ||
"""Create Invenio-Records-Files blueprint from a Flask application. | ||
:params app: A Flask application. | ||
:returns: Configured blueprint. | ||
""" | ||
records_files_blueprint = Blueprint( | ||
'invenio_records_files', | ||
__name__, | ||
url_prefix='') | ||
|
||
for config_name, rec_types in \ | ||
app.config['RECORDS_FILES_REST_ENDPOINTS'].iteritems(): | ||
files_resource_name = app.config['RECORDS_FILES_RESOURCE_NAME'] | ||
for rec_type in rec_types: | ||
record_item_path = app.config[config_name][rec_type]['item_route'] | ||
records_files_blueprint.add_url_rule( | ||
'{record_item_path}/{files_resource_name}'.format(**locals()), | ||
view_func=bucket_view, | ||
) | ||
|
||
records_files_blueprint.add_url_rule( | ||
'{record_item_path}/{files_resource_name}/<path:key>'.format( | ||
**locals()), | ||
view_func=object_view, | ||
) | ||
|
||
@records_files_blueprint.url_value_preprocessor | ||
def resolve_pid_to_bucket_id(endpoint, values): | ||
"""Flask URL preprocessor to resolve pid to Bucket ID. | ||
In the ``records_files_blueprint`` we are gluing together Records-REST | ||
and Files-REST APIs. Records-REST knows about PIDs but Files-REST does | ||
not, this function will pre-process the URL so the PID is removed from | ||
the URL and resolved to bucket ID which is injected into Files-REST | ||
view calls: | ||
/api/<record_type>/<pid_value>/files/<key> -> /files/<bucket>/<key>. | ||
""" | ||
uuid = str(values['pid_value'].data[0].object_uuid) | ||
g.pid = values.pop('pid_value') | ||
bucket = RecordsBuckets.query.filter_by(record_id=uuid).first() | ||
if bucket: | ||
values['bucket_id'] = str(bucket.bucket_id) | ||
else: | ||
abort(404, 'The requested record does not have files associated') | ||
|
||
@records_files_blueprint.url_defaults | ||
def restore_pid_to_url(endpoint, values): | ||
"""Put ``pid_value`` back to the URL after matching Files-REST views. | ||
Since we are computing the URL more than one times, we need the | ||
original values of the request to be unchanged so that it can be | ||
reproduced. | ||
""" | ||
values['pid_value'] = g.pid | ||
|
||
return records_files_blueprint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.