-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.py
475 lines (399 loc) · 16.9 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# -*- coding: UTF-8 -*-
"""
This is the default app controller for portality.
For inclusion in your own project you should make your own version of this controller
and include the views you require, as well as writing new ones. Of course, views must
also be backed up by models, so have a look at the example models and use them / write
new ones as required too.
~~DOAJ:WebApp~~
"""
import os, sys
import tzlocal
import pytz
from flask import request, abort, render_template, redirect, send_file, url_for, jsonify, send_from_directory
from flask_login import login_user, current_user
from datetime import datetime
import portality.models as models
from portality.core import app, es_connection, initialise_index
from portality import settings
from portality.lib import edges, dates
from portality.lib.dates import FMT_DATETIME_STD, FMT_YEAR
from portality.view.account import blueprint as account
from portality.view.admin import blueprint as admin
from portality.view.publisher import blueprint as publisher
from portality.view.query import blueprint as query
from portality.view.doaj import blueprint as doaj
from portality.view.oaipmh import blueprint as oaipmh
from portality.view.openurl import blueprint as openurl
from portality.view.atom import blueprint as atom
from portality.view.editor import blueprint as editor
from portality.view.doajservices import blueprint as services
from portality.view.jct import blueprint as jct
from portality.view.apply import blueprint as apply
if 'api1' in app.config['FEATURES']:
from portality.view.api_v1 import blueprint as api_v1
if 'api2' in app.config['FEATURES']:
from portality.view.api_v2 import blueprint as api_v2
if 'api3' in app.config['FEATURES']:
from portality.view.api_v3 import blueprint as api_v3
from portality.view.status import blueprint as status
from portality.lib.normalise import normalise_doi
from portality.view.dashboard import blueprint as dashboard
from portality.view.tours import blueprint as tours
if app.config.get("DEBUG", False) and app.config.get("TESTDRIVE_ENABLED", False):
from portality.view.testdrive import blueprint as testdrive
app.register_blueprint(account, url_prefix='/account') #~~->Account:Blueprint~~
app.register_blueprint(admin, url_prefix='/admin') #~~-> Admin:Blueprint~~
app.register_blueprint(publisher, url_prefix='/publisher') #~~-> Publisher:Blueprint~~
app.register_blueprint(query, name='query', url_prefix='/query') # ~~-> Query:Blueprint~~
app.register_blueprint(query, name='admin_query', url_prefix='/admin_query')
app.register_blueprint(query, name='publisher_query', url_prefix='/publisher_query')
app.register_blueprint(query, name='editor_query', url_prefix='/editor_query')
app.register_blueprint(query, name='associate_query', url_prefix='/associate_query')
app.register_blueprint(query, name='dashboard_query', url_prefix="/dashboard_query")
app.register_blueprint(editor, url_prefix='/editor') # ~~-> Editor:Blueprint~~
app.register_blueprint(services, url_prefix='/service') # ~~-> Services:Blueprint~~
if 'api1' in app.config['FEATURES']:
app.register_blueprint(api_v1, url_prefix='/api/v1') # ~~-> APIv1:Blueprint~~
if 'api2' in app.config['FEATURES']:
app.register_blueprint(api_v2, url_prefix='/api/v2') # ~~-> APIv2:Blueprint~~
if 'api3' in app.config['FEATURES']:
app.register_blueprint(api_v3, name='api', url_prefix='/api') # ~~-> APIv3:Blueprint~~
app.register_blueprint(api_v3, name='api_v3', url_prefix='/api/v3') # ~~-> APIv3:Blueprint~~
app.register_blueprint(status, name='status', url_prefix='/status') # ~~-> Status:Blueprint~~
app.register_blueprint(status, name='_status', url_prefix='/_status')
app.register_blueprint(apply, url_prefix='/apply') # ~~-> Apply:Blueprint~~
app.register_blueprint(jct, url_prefix="/jct") # ~~-> JCT:Blueprint~~
app.register_blueprint(dashboard, url_prefix="/dashboard") #~~-> Dashboard:Blueprint~~
app.register_blueprint(tours, url_prefix="/tours") # ~~-> Tours:Blueprint~~
app.register_blueprint(oaipmh) # ~~-> OAIPMH:Blueprint~~
app.register_blueprint(openurl) # ~~-> OpenURL:Blueprint~~
app.register_blueprint(atom) # ~~-> Atom:Blueprint~~
app.register_blueprint(doaj) # ~~-> DOAJ:Blueprint~~
if app.config.get("DEBUG", False) and app.config.get("TESTDRIVE_ENABLED", False):
app.logger.warning('Enabling TESTDRIVE at /testdrive')
app.register_blueprint(testdrive, url_prefix="/testdrive") # ~~-> Testdrive:Feature ~~
# initialise the index - don't put into if __name__ == '__main__' block,
# because that does not run if gunicorn is loading the app, as opposed
# to the app being run directly by python portality/app.py
# putting it here ensures it will run under any web server
initialise_index(app, es_connection)
# serve static files from multiple potential locations
# this allows us to override the standard static file handling with our own dynamic version
# ~~-> Assets:WebRoute~~
# @app.route("/static_content/<path:filename>")
@app.route("/static/<path:filename>")
@app.route("/assets/<path:filename>")
def our_static(filename):
return custom_static(filename)
def custom_static(path):
for dir in app.config.get("STATIC_PATHS", []):
target = os.path.join(app.root_path, dir, path)
if os.path.isfile(target):
return send_from_directory(os.path.dirname(target), os.path.basename(target))
abort(404)
# Configure Analytics
# ~~-> PlausibleAnalytics:ExternalService~~
from portality.lib import plausible
plausible.create_logfile(app.config.get('PLAUSIBLE_LOG_DIR', None))
# Redirects from previous DOAJ app.
# RJ: I have decided to put these here so that they can be managed
# alongside the DOAJ codebase. I know they could also go into the
# nginx config, but there is a chance that they will get lost or forgotten
# some day, whereas this approach doesn't have that risk.
# ~~-> Legacy:WebRoute~~
@app.route("/doaj")
def legacy():
func = request.values.get("func")
if func == "csv":
return redirect(url_for('doaj.csv_data')), 301
elif func == "rss":
return redirect(url_for('atom.feed')), 301
elif func == "browse" or func == 'byPublicationFee ':
return redirect(url_for('doaj.search')), 301
elif func == "openurl":
vals = request.values.to_dict(flat=True)
del vals["func"]
return redirect(url_for('openurl.openurl', **vals), 301)
abort(404)
@app.route("/doaj2csv")
def another_legacy_csv_route():
return redirect("/csv"), 301
###################################################
# ~~-> DOAJArticleXML:Schema~~
@app.route("/schemas/doajArticles.xsd")
def legacy_doaj_XML_schema():
schema_fn = 'doajArticles.xsd'
return send_file(
os.path.join(app.config.get("STATIC_DIR"), "doaj", schema_fn),
mimetype="application/xml", as_attachment=True, attachment_filename=schema_fn
)
# ~~-> CrossrefArticleXML:WebRoute~~
@app.route("/isCrossrefLoaded")
def is_crossref_loaded():
if app.config.get("LOAD_CROSSREF_THREAD") is not None and app.config.get("LOAD_CROSSREF_THREAD").is_alive():
return "false"
else:
return "true"
# FIXME: this used to calculate the site stats on request, but for the time being
# this is an unnecessary overhead, so taking it out. Will need to put something
# equivalent back in when we do the admin area
# ~~-> SiteStats:Feature~~
@app.context_processor
def set_current_context():
""" Set some template context globals. """
'''
Inserts variables into every template this blueprint renders. This
one deals with the announcement in the header, which can't be built
into the template directly, as various styles are applied only if a
header is present on the page. It also makes the list of DOAJ
sponsors available and may include similar minor pieces of
information.
'''
return {
'settings': settings,
'statistics': models.JournalArticle.site_statistics(),
"current_user": current_user,
"app": app,
"current_year": dates.now_str(FMT_YEAR),
"base_url": app.config.get('BASE_URL'),
}
# Jinja2 Template Filters
# ~~-> Jinja2:Environment~~
@app.template_filter("bytesToFilesize")
def bytes_to_filesize(size):
units = ["bytes", "Kb", "Mb", "Gb"]
scale = 0
while size > 1000 and scale < len(units):
size = float(size) / 1000.0 # note that it is no longer 1024
scale += 1
return "{size:.1f}{unit}".format(size=size, unit=units[scale])
@app.template_filter('utc_timestamp')
def utc_timestamp(stamp, string_format=FMT_DATETIME_STD):
"""
Format a local time datetime object to UTC
:param stamp: a datetime object
:param string_format: defaults to "%Y-%m-%dT%H:%M:%SZ", which complies with ISO 8601
:return: the string formatted datetime
"""
local = tzlocal.get_localzone()
ld = local.localize(stamp)
tt = ld.utctimetuple()
utcdt = datetime(tt.tm_year, tt.tm_mon, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec, tzinfo=pytz.utc)
return utcdt.strftime(string_format)
human_date = app.template_filter("human_date")(dates.human_date)
@app.template_filter('doi_url')
def doi_url(doi):
"""
Create a link from a DOI.
:param doi: the string DOI
:return: the HTML link
"""
try:
return "https://doi.org/" + normalise_doi(doi)
except ValueError:
return ""
@app.template_filter('form_diff_table_comparison_value')
def form_diff_table_comparison_value(val):
"""
Function for converting the given value to a suitable UI value for presentation in the diff table
on the admin forms for update requests.
:param val: the raw value to be converted to a display value
:return:
"""
if val is None:
return ""
if isinstance(val, list) and len(val) == 0:
return ""
if isinstance(val, list):
dvals = []
for v in val:
dvals.append(form_diff_table_comparison_value(v))
return ", ".join(dvals)
else:
if val is True or (isinstance(val, str) and val.lower() == "true"):
return "Yes"
elif val is False or (isinstance(val, str) and val.lower() == "false"):
return "No"
return val
@app.template_filter('form_diff_table_subject_expand')
def form_diff_table_subject_expand(val):
"""
Function for expanding one or more subject classifications out to their full terms
:param val:
:return:
"""
if val is None:
return ""
if isinstance(val, list) and len(val) == 0:
return ""
if not isinstance(val, list):
val = [val]
from portality import lcc
results = []
for v in val:
if v is None or v == "":
continue
expanded = lcc.lcc_index_by_code.get(v)
if expanded is not None:
results.append(expanded + " [code: " + v + "]")
else:
results.append(v)
return ", ".join(results)
@app.template_filter("is_in_the_past")
def is_in_the_past(dttm):
return dates.is_before(dttm, dates.today())
#######################################################
@app.context_processor
def search_query_source_wrapper():
def search_query_source(**params):
return edges.make_url_query(**params)
return dict(search_query_source=search_query_source)
@app.context_processor
def maned_of_wrapper():
def maned_of():
# ~~-> EditorGroup:Model ~~
egs = []
assignments = {}
if current_user.has_role("admin"):
egs = models.EditorGroup.groups_by_maned(current_user.id)
if len(egs) > 0:
assignments = models.Application.assignment_to_editor_groups(egs)
return egs, assignments
return dict(maned_of=maned_of)
@app.context_processor
def editor_of_wrapper():
def editor_of():
# ~~-> EditorGroup:Model ~~
egs = []
assignments = {}
if current_user.has_role("editor"):
egs = models.EditorGroup.groups_by_editor(current_user.id)
if len(egs) > 0:
assignments = models.Application.assignment_to_editor_groups(egs)
return egs, assignments
return dict(editor_of=editor_of)
@app.context_processor
def associate_of_wrapper():
def associate_of():
# ~~-> EditorGroup:Model ~~
egs = []
assignments = {}
if current_user.has_role("associate_editor"):
egs = models.EditorGroup.groups_by_associate(current_user.id)
if len(egs) > 0:
assignments = models.Application.assignment_to_editor_groups(egs)
return egs, assignments
return dict(associate_of=associate_of)
# ~~-> Account:Model~~
# ~~-> AuthNZ:Feature~~
@app.before_request
def standard_authentication():
"""Check remote_user on a per-request basis."""
remote_user = request.headers.get('REMOTE_USER', '')
if remote_user:
user = models.Account.pull(remote_user)
if user:
login_user(user, remember=False)
elif 'api_key' in request.values:
q = models.Account.query(q='api_key:"' + request.values['api_key'] + '"')
if 'hits' in q:
res = q['hits']['hits']
if len(res) == 1:
user = models.Account.pull(res[0]['_source']['id'])
if user:
login_user(user, remember=False)
# Register configured API versions
# ~~-> APIv1:Blueprint~~
# ~~-> APIv2:Blueprint~~
# ~~-> APIv3:Blueprint~~
features = app.config.get('FEATURES', [])
if 'api1' in features or 'api2' in features or 'api3' in features:
@app.route('/api/')
def api_directory():
vers = []
# NOTE: we never could run API v1 and v2 at the same time.
# This code is here for future reference to add additional API versions
if 'api1' in features:
vers.append(
{
'version': '1.0.0',
'base_url': url_for('api_v1.api_spec', _external=True,
_scheme=app.config.get('PREFERRED_URL_SCHEME', 'https')),
'note': 'First version of the DOAJ API',
'docs_url': url_for('api_v1.docs', _external=True,
_scheme=app.config.get('PREFERRED_URL_SCHEME', 'https'))
}
)
if 'api2' in features:
vers.append(
{
'version': '2.0.0',
'base_url': url_for('api_v2.api_spec', _external=True,
_scheme=app.config.get('PREFERRED_URL_SCHEME', 'https')),
'note': 'Second version of the DOAJ API',
'docs_url': url_for('api_v2.docs', _external=True,
_scheme=app.config.get('PREFERRED_URL_SCHEME', 'https'))
}
)
if 'api3' in features:
vers.append(
{
'version': '3.0.0',
'base_url': url_for('api_v3.api_spec', _external=True,
_scheme=app.config.get('PREFERRED_URL_SCHEME', 'https')),
'note': 'Third version of the DOAJ API',
'docs_url': url_for('api_v3.docs', _external=True,
_scheme=app.config.get('PREFERRED_URL_SCHEME', 'https'))
}
)
return jsonify({'api_versions': vers})
# Make the reCAPTCHA key available to the js
# ~~-> ReCAPTCHA:ExternalService~~
@app.route('/get_recaptcha_site_key')
def get_site_key():
return app.config.get('RECAPTCHA_SITE_KEY', '')
@app.errorhandler(400)
def page_not_found(e):
return render_template('400.html'), 400
@app.errorhandler(401)
def page_not_found(e):
return render_template('401.html'), 401
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def page_not_found(e):
return render_template('500.html'), 500
def run_server(host=None, port=None, fake_https=False):
"""
:param host:
:param port:
:param fake_https:
if fake_https is True, develop can use https:// to access the server
that can help for debugging Plausible
:return:
"""
pycharm_debug = app.config.get('DEBUG_PYCHARM', False)
if len(sys.argv) > 1:
if sys.argv[1] == '-d':
pycharm_debug = True
if pycharm_debug:
app.config['DEBUG'] = False
import pydevd
pydevd.settrace(app.config.get('DEBUG_PYCHARM_SERVER', 'localhost'),
port=app.config.get('DEBUG_PYCHARM_PORT', 6000),
stdoutToServer=True, stderrToServer=True)
# run background jobs immediately if DEBUG_BGJOBS_IMMEDIATELY is True
if app.config.get('DEBUG_BGJOBS_IMMEDIATELY', False):
from portality.tasks import redis_huey
redis_huey.run_bgjobs_immediately()
run_kwargs = {}
if fake_https:
run_kwargs['ssl_context'] = 'adhoc'
host = host or app.config['HOST']
port = port or app.config['PORT']
app.run(host=host, debug=app.config['DEBUG'], port=port,
**run_kwargs)
if __name__ == "__main__":
run_server()