-
Notifications
You must be signed in to change notification settings - Fork 27
/
app.py
191 lines (151 loc) · 5.03 KB
/
app.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- 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.
"""Minimal Flask application example for development.
SPHINX-START
Run the Redis server.
Run example development server:
.. code-block:: console
$ pip install -e .[all]
$ cd examples
$ export FLASK_APP=app.py
$ ./app-setup.sh
$ ./app-fixtures.sh
Run example development server:
.. code-block:: console
$ flask run --debugger -p 5000
View some records in your browser::
http://localhost:5000/records/1
http://localhost:5000/records/2
http://localhost:5000/records/3
http://localhost:5000/records/4
http://localhost:5000/records/5
http://localhost:5000/records/6
http://localhost:5000/records/7
http://localhost:5000/records/8
To be able to uninstall the example app:
.. code-block:: console
$ ./app-teardown.sh
SPHINX-END
"""
from __future__ import absolute_import, print_function
import os
from flask import Flask
from flask_babelex import Babel
from flask_celeryext import FlaskCeleryExt
from invenio_db import InvenioDB, db
from invenio_pidstore import InvenioPIDStore
from invenio_records import InvenioRecords
from invenio_records_ui import InvenioRecordsUI
from invenio_records_ui.views import create_blueprint_from_app
# create application's instance directory. Needed for this example only.
current_dir = os.path.dirname(os.path.realpath(__file__))
instance_dir = os.path.join(current_dir, "app")
if not os.path.exists(instance_dir):
os.makedirs(instance_dir)
# Create Flask application
app = Flask(__name__, instance_path=instance_dir)
app.config.update(
dict(
CELERY_ALWAYS_EAGER=True,
CELERY_RESULT_BACKEND="cache",
CELERY_CACHE_BACKEND="memory",
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
SQLALCHEMY_DATABASE_URI=os.environ.get(
"SQLALCHEMY_DATABASE_URI", "sqlite:///app.db"
),
# Disable access control
RECORDS_UI_DEFAULT_PERMISSION_FACTORY=None,
)
)
FlaskCeleryExt(app)
Babel(app)
InvenioDB(app)
InvenioPIDStore(app)
InvenioRecords(app)
InvenioRecordsUI(app)
app.register_blueprint(create_blueprint_from_app(app))
rec1_uuid = "deadbeef-1234-5678-ba11-b100dc0ffee5"
"""First record's UUID. It will be given PID 1."""
rec2_uuid = "deadbeef-1234-5678-ba11-b100dc0ffee6"
"""First record's UUID. It will be given PID 2."""
@app.cli.group()
def fixtures():
"""Command for working with test data."""
@fixtures.command()
def records():
"""Load test data fixture."""
import uuid
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
from invenio_records.api import Record
# Record 1 - Live record
with db.session.begin_nested():
pid1 = PersistentIdentifier.create(
"recid",
"1",
object_type="rec",
object_uuid=rec1_uuid,
status=PIDStatus.REGISTERED,
)
Record.create(
{
"title": "Registered ",
"authors": [
{"name": "Ellis Jonathan"},
{"name": "Higgs Peter"},
],
"access": "open",
"keywords": ["CERN", "higgs"],
},
id_=rec1_uuid,
)
PersistentIdentifier.create(
"recid",
"2",
object_type="rec",
object_uuid=rec2_uuid,
status=PIDStatus.REGISTERED,
)
Record.create(
{
"title": "Registered ",
"authors": [
{"name": "Ellis Jonathan"},
{"name": "Higgs Peter"},
],
"access": "closed",
"keywords": ["CERN", "higgs"],
},
id_=rec2_uuid,
)
# Record 3 - Deleted PID with record
rec3_uuid = uuid.uuid4()
pid = PersistentIdentifier.create(
"recid",
"3",
object_type="rec",
object_uuid=rec3_uuid,
status=PIDStatus.REGISTERED,
)
pid.delete()
Record.create({"title": "Live "}, id_=rec3_uuid)
# Record 4 - Deleted PID without a record
PersistentIdentifier.create("recid", "4", status=PIDStatus.DELETED)
# Record 5 - Registered PID without a record
PersistentIdentifier.create("recid", "5", status=PIDStatus.REGISTERED)
# Record 6 - Redirected PID
pid = PersistentIdentifier.create("recid", "6", status=PIDStatus.REGISTERED)
pid.redirect(pid1)
# Record 7 - Redirected non existing endpoint
doi = PersistentIdentifier.create(
"doi", "10.1234/foo", status=PIDStatus.REGISTERED
)
pid = PersistentIdentifier.create("recid", "7", status=PIDStatus.REGISTERED)
pid.redirect(doi)
# Record 8 - Unregistered PID
PersistentIdentifier.create("recid", "8", status=PIDStatus.RESERVED)
db.session.commit()