-
Notifications
You must be signed in to change notification settings - Fork 47
/
ext.py
155 lines (126 loc) · 4.9 KB
/
ext.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016-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.
"""Module for depositing record metadata and uploading files."""
from __future__ import absolute_import, print_function
from collections import defaultdict
from invenio_records_rest import utils
from werkzeug.utils import cached_property
from . import config
from .receivers import index_deposit_after_publish
from .signals import post_action
from .views import rest, ui
class _DepositState(object):
"""Deposit state."""
def __init__(self, app):
"""Initialize state."""
self.app = app
@cached_property
def jsonschemas(self):
"""Load deposit JSON schemas."""
_jsonschemas = {
k: v['jsonschema']
for k, v in self.app.config['DEPOSIT_RECORDS_UI_ENDPOINTS'].items()
if 'jsonschema' in v
}
return defaultdict(
lambda: self.app.config['DEPOSIT_DEFAULT_JSONSCHEMA'], _jsonschemas
)
@cached_property
def schemaforms(self):
"""Load deposit schema forms."""
_schemaforms = {
k: v['schemaform']
for k, v in self.app.config['DEPOSIT_RECORDS_UI_ENDPOINTS'].items()
if 'schemaform' in v
}
return defaultdict(
lambda: self.app.config['DEPOSIT_DEFAULT_SCHEMAFORM'], _schemaforms
)
class InvenioDeposit(object):
"""Invenio-Deposit extension."""
def __init__(self, app=None):
"""Extension initialization."""
if app:
self.init_app(app)
def init_app(self, app):
"""Flask application initialization.
Initialize the UI endpoints. Connect all signals if
`DEPOSIT_REGISTER_SIGNALS` is ``True``.
:param app: An instance of :class:`flask.Flask`.
"""
self.init_config(app)
app.register_blueprint(ui.create_blueprint(
app.config['DEPOSIT_RECORDS_UI_ENDPOINTS']
))
app.extensions['invenio-deposit'] = _DepositState(app)
if app.config['DEPOSIT_REGISTER_SIGNALS']:
post_action.connect(index_deposit_after_publish, sender=app,
weak=False)
def init_config(self, app):
"""Initialize configuration.
:param app: An instance of :class:`flask.Flask`.
"""
app.config.setdefault(
'DEPOSIT_BASE_TEMPLATE',
app.config.get('BASE_TEMPLATE',
'invenio_deposit/base.html'))
for k in dir(config):
if k.startswith('DEPOSIT_'):
app.config.setdefault(k, getattr(config, k))
class InvenioDepositREST(object):
"""Invenio-Deposit REST extension."""
def __init__(self, app=None):
"""Extension initialization.
:param app: An instance of :class:`flask.Flask`.
"""
if app:
self.init_app(app)
def init_app(self, app):
"""Flask application initialization.
Initialize the REST endpoints. Connect all signals if
`DEPOSIT_REGISTER_SIGNALS` is True.
:param app: An instance of :class:`flask.Flask`.
"""
self.init_config(app)
blueprint = rest.create_blueprint(
app.config['DEPOSIT_REST_ENDPOINTS']
)
# FIXME: This is a temporary fix. This means that
# invenio-records-rest's endpoint_prefixes cannot be used before
# the first request or in other processes, ex: Celery tasks.
@app.before_first_request
def extend_default_endpoint_prefixes():
"""Extend redirects between PID types."""
endpoint_prefixes = utils.build_default_endpoint_prefixes(
dict(app.config['DEPOSIT_REST_ENDPOINTS'])
)
current_records_rest = app.extensions['invenio-records-rest']
overlap = set(endpoint_prefixes.keys()) & set(
current_records_rest.default_endpoint_prefixes
)
if overlap:
raise RuntimeError(
'Deposit wants to override endpoint prefixes {0}.'.format(
', '.join(overlap)
)
)
current_records_rest.default_endpoint_prefixes.update(
endpoint_prefixes
)
app.register_blueprint(blueprint)
app.extensions['invenio-deposit-rest'] = _DepositState(app)
if app.config['DEPOSIT_REGISTER_SIGNALS']:
post_action.connect(index_deposit_after_publish, sender=app,
weak=False)
def init_config(self, app):
"""Initialize configuration.
:param app: An instance of :class:`flask.Flask`.
"""
for k in dir(config):
if k.startswith('DEPOSIT_'):
app.config.setdefault(k, getattr(config, k))