forked from occrp-attic/spindle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
46 lines (38 loc) · 1.58 KB
/
util.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
import os
from collections import OrderedDict # noqa
from flask import current_app
from flask import url_for as flask_url_for
from spindle import authz
def angular_templates():
partials_dir = os.path.join(current_app.static_folder, '..')
partials_dir = os.path.join(partials_dir, 'templates', 'angular')
for (root, dirs, files) in os.walk(partials_dir):
for file_name in files:
file_path = os.path.join(root, file_name)
with open(file_path, 'rb') as fh:
file_name = file_path[len(partials_dir) + 1:]
yield (file_name, fh.read().decode('utf-8'))
def url_for(*a, **kw):
""" Always generate external URLs. """
try:
kw['_external'] = True
if current_app.config.get('PREFERRED_URL_SCHEME'):
kw['_scheme'] = current_app.config.get('PREFERRED_URL_SCHEME')
return flask_url_for(*a, **kw)
except RuntimeError:
return None
def result_entity(entity):
if '_source' in entity and '_id' in entity:
entity['_source']['id'] = entity['_id']
entity = entity['_source']
entity['$uri'] = url_for('entities.view', id=entity.get('id'))
colls = entity.get('$collections', [])
entity['$collections'] = authz.collections(authz.READ).intersection(colls)
sources = entity.get('$sources', [])
entity['$sources'] = authz.sources(authz.READ).intersection(sources)
entity.pop('$text', None)
entity.pop('$latin', None)
for k, v in entity.items():
if isinstance(v, (set, list, tuple)) and len(v) == 0:
entity.pop(k)
return entity