-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathviews.py
86 lines (76 loc) · 3.62 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
#
# This file is part of CERN Analysis Preservation Framework.
# Copyright (C) 2016 CERN.
#
# CERN Analysis Preservation Framework is free software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# CERN Analysis Preservation Framework 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CERN Analysis Preservation Framework; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""Extra deposit views."""
from __future__ import absolute_import, print_function
from flask import Blueprint, g
from invenio_files_rest.views import bucket_view, object_view
from sqlalchemy.orm.exc import NoResultFound
from .api import CAPDeposit
def create_deposit_files_blueprint(app):
"""Create blueprint from a Flask application.
:params app: A Flask application.
:returns: Configured blueprint.
"""
cap_deposit_files_bp = Blueprint('cap_deposit_files',
__name__,
url_prefix='')
deposit_item_path = app.config['DEPOSIT_REST_ENDPOINTS']['depid'][
'item_route']
cap_deposit_files_bp.add_url_rule('{}/files'.format(deposit_item_path),
view_func=bucket_view)
cap_deposit_files_bp.add_url_rule(
'{}/files/<path:key>'.format(deposit_item_path), view_func=object_view)
@cap_deposit_files_bp.url_value_preprocessor
def resolve_pid_to_bucket_id(endpoint, values):
"""Flask URL preprocessor to resolve pid to Bucket ID.
In the ``cap_deposit_bp`` 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>``.
"""
# Remove the 'pid_value' in order to match the Files-REST bucket view
# signature. Store the value in Flask global request object for later
# usage.
g.pid = values.pop('pid_value')
pid, _ = g.pid.data
try:
deposit = CAPDeposit.get_record(pid.object_uuid)
values['bucket_id'] = str(deposit.files.bucket)
except (AttributeError, NoResultFound):
# Hack, to make invenio_files_rest.views.as_uuid throw a
# ValueError instead of a TypeError if we set the value to None.
values['bucket_id'] = ''
@cap_deposit_files_bp.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.
"""
# Value here has been saved in above method (resolve_pid_to_bucket_id)
values['pid_value'] = g.pid
return cap_deposit_files_bp