-
Notifications
You must be signed in to change notification settings - Fork 28
/
ext.py
90 lines (69 loc) · 2.62 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
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 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 Invenio-Records-UI."""
from __future__ import absolute_import, print_function
from . import config
from .utils import obj_or_import_string
from .views import create_blueprint
class _RecordUIState(object):
"""Record UI state."""
def __init__(self, app):
"""Initialize state.
:param app: The Flask application.
"""
self.app = app
self._permission_factory = None
self._export_formats = {}
def export_formats(self, pid_type):
"""List of export formats."""
if pid_type not in self._export_formats:
fmts = self.app.config.get('RECORDS_UI_EXPORT_FORMATS', {}).get(
pid_type, {})
self._export_formats[pid_type] = sorted(
[(k, v) for k, v in fmts.items() if v],
key=lambda x: x[1]['order'],
)
return self._export_formats[pid_type]
@property
def permission_factory(self):
"""Load default permission factory."""
if self._permission_factory is None:
imp = self.app.config['RECORDS_UI_DEFAULT_PERMISSION_FACTORY']
self._permission_factory = obj_or_import_string(imp)
return self._permission_factory
class InvenioRecordsUI(object):
"""Invenio-Records-UI extension.
The extension takes care of setting default configuration and registering
a blueprint with URL routes for the endpoints.
"""
def __init__(self, app=None):
"""Extension initialization.
:param app: The Flask application. (Default: ``None``)
"""
if app:
self.init_app(app)
def init_app(self, app):
"""Flask application initialization.
:param app: The Flask application.
"""
self.init_config(app)
# Register records blueprints
app.register_blueprint(
create_blueprint(app.config['RECORDS_UI_ENDPOINTS']))
app.extensions['invenio-records-ui'] = _RecordUIState(app)
def init_config(self, app):
"""Initialize configuration on application.
:param app: The Flask application.
"""
app.config.setdefault(
'RECORDS_UI_BASE_TEMPLATE',
app.config.get('BASE_TEMPLATE', 'invenio_records_ui/base.html')
)
for k in dir(config):
if k.startswith('RECORDS_UI_'):
app.config.setdefault(k, getattr(config, k))