-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_module.py
221 lines (176 loc) · 5.82 KB
/
web_module.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
"""
Most of the functions needed by the web service are here.
In run_app.py we just keep the main web logic.
"""
import os
from collections import OrderedDict
import yaml
import flask
from flask import Blueprint
from conf import (
directory,
static_folder,
user_static_folder,
config_file_path,
ConfigurationError,
)
# Key: internal value; value: string to show
upload_structure_block_known_formats = OrderedDict(
{
"cif": "CIF File (.cif)",
"info": "cif2cell output (.info)",
}
)
def get_secret_key():
try:
with open(os.path.join(directory, "SECRET_KEY")) as f:
secret_key = f.readlines()[0].strip()
if len(secret_key) < 16:
raise ValueError
return secret_key
except Exception:
raise ConfigurationError(
"Please create a SECRET_KEY file in {} with a random string "
"of at least 16 characters".format(directory)
)
def parse_config(config):
default_templates_folder = "default_templates"
user_templates_folder = "user_templates"
retdict = {}
templates = config.get("templates", {})
known_templates = [
"how_to_cite",
"about",
"select_content",
"upload_structure_additional_content",
]
for template_name in known_templates:
# Note that this still allows to set it to None explicitly to skip this section
try:
retdict[template_name] = templates[template_name]
if retdict[template_name] is not None:
retdict[template_name] = os.path.join(
user_templates_folder, retdict[template_name]
)
except KeyError:
retdict[template_name] = os.path.join(
default_templates_folder, "{}.html".format(template_name)
)
for template_name in templates:
if (
template_name not in known_templates
and templates[template_name] is not None
):
retdict[template_name] = os.path.join(
user_templates_folder, templates[template_name]
)
additional_accordion_entries = config.get("additional_accordion_entries", [])
retdict["additional_accordion_entries"] = []
for accordian_entry in additional_accordion_entries:
retdict["additional_accordion_entries"].append(
{
"header": accordian_entry["header"],
"template_page": os.path.join(
user_templates_folder, accordian_entry["template_page"]
),
}
)
return retdict
def set_config_defaults(config):
"""Add defaults so the site works"""
new_config = config.copy()
new_config.setdefault("window_title", "Materials Cloud Tool")
new_config.setdefault(
"page_title",
"<PLEASE SPECIFY A PAGE_TITLE AND A WINDOW_TITLE IN THE CONFIG FILE>",
)
new_config.setdefault("custom_css_files", {})
new_config.setdefault("custom_js_files", {})
new_config.setdefault("templates", {})
return new_config
def get_config():
try:
with open(config_file_path) as config_file:
config = yaml.safe_load(config_file)
except IOError as exc:
if exc.errno == 2: # No such file or directory
config = {}
else:
raise
# set defaults
config = set_config_defaults(config)
return {
"config": config,
"include_pages": parse_config(config),
"upload_structure_block_known_formats": upload_structure_block_known_formats,
}
static_bp = Blueprint("static", __name__, url_prefix="/static")
user_static_bp = Blueprint("user_static", __name__, url_prefix="/user_static")
@static_bp.route("/js/<path:path>")
def send_js(path):
"""
Serve static JS files
"""
return flask.send_from_directory(os.path.join(static_folder, "js"), path)
@user_static_bp.route("/js/<path:path>")
def send_custom_js(path):
"""
Serve static JS files
"""
return flask.send_from_directory(os.path.join(user_static_folder, "js"), path)
@static_bp.route("/img/<path:path>")
def send_img(path):
"""
Serve static image files
"""
return flask.send_from_directory(os.path.join(static_folder, "img"), path)
@user_static_bp.route("/img/<path:path>")
def send_custom_img(path):
"""
Serve static image files
"""
return flask.send_from_directory(os.path.join(user_static_folder, "img"), path)
@static_bp.route("/css/<path:path>")
def send_css(path):
"""
Serve static CSS files
"""
return flask.send_from_directory(os.path.join(static_folder, "css"), path)
@user_static_bp.route("/css/<path:path>")
def send_custom_css(path):
"""
Serve static CSS files
"""
return flask.send_from_directory(os.path.join(user_static_folder, "css"), path)
@user_static_bp.route("/data/<path:path>")
def send_custom_data(path):
"""
Serve static font files
"""
return flask.send_from_directory(os.path.join(user_static_folder, "data"), path)
@static_bp.route("/css/images/<path:path>")
def send_cssimages(path):
"""
Serve static CSS images files
"""
return flask.send_from_directory(os.path.join(static_folder, "css", "images"), path)
@user_static_bp.route("/css/images/<path:path>")
def send_custom_cssimages(path):
"""
Serve static CSS images files
"""
return flask.send_from_directory(
os.path.join(user_static_folder, "css", "images"), path
)
@static_bp.route("/fonts/<path:path>")
def send_fonts(path):
"""
Serve static font files
"""
return flask.send_from_directory(os.path.join(static_folder, "fonts"), path)
@user_static_bp.route("/fonts/<path:path>")
def send_custom_fonts(path):
"""
Serve static font files
"""
return flask.send_from_directory(os.path.join(user_static_folder, "fonts"), path)