-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·418 lines (343 loc) · 12.4 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from email.mime.text import MIMEText
from datetime import datetime
from smtplib import SMTP
import traceback
import os
from flask import Flask, render_template, redirect, request, session, url_for, Response
from werkzeug.wsgi import DispatcherMiddleware
from PIL import Image
import yaml
app = Flask(__name__)
if app.config['ENV'] == 'production':
app.config.from_object('scripts.config.ProductionConfig')
elif app.config['ENV'] == 'staging':
app.wsgi_app = DispatcherMiddleware(app, {'/lcp_dev': app.wsgi_app})
else:
app.config.from_object('scripts.config.DevConfig')
def _data():
data = {}
return data
@app.errorhandler(404)
def page_not_found(error):
"""
Convert 404 page to a custom template.
"""
return render_template('404.html')
@app.errorhandler(500)
def internal_server_error(error):
"""
This is the page for handling internal server error
there will be a log in apache and email generated
"""
trace = traceback.format_exc()
location = request.path
app.logger.error("500 - Internal Server Error - {0}\n{1}\n".format(
location, trace))
content = "There was a internal server error on the Flask app running the \
LCP website. \nThe time of this error is: {0}\nError traceback:\n{1}\
".format(datetime.now(), trace)
if 'Username' in session:
content += "\nThe user tha triggered this error is: {0}".format(
session['Username'])
send_email("Internal Server Error - Flask LCP - {0}".format(location),
content, '[email protected]')
return render_template('500.html')
@app.route('/robots.txt')
def send_text_file():
"""
Serve robots file
"""
if app.config['ENV'] == 'production':
return app.send_static_file('robots.txt')
return Response("User-agent: *\nDisallow: /", mimetype='text/plain')
def send_email(subject, content, sender, rec=None):
"""
Send plain text email
"""
server = SMTP('127.0.0.1')
msg = MIMEText(content, 'plain')
recipients = app.config['PRIMARY_ADMIN']
if rec is not None:
recipients = rec
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ', '.join(recipients)
server.sendmail(sender, recipients, msg.as_string())
server.quit()
def resize_image():
"""
Function that takes a image to crop and upload ßit.
"""
x_axis = float(request.form.get('x', None))
y_axis = float(request.form.get('y', None))
width = float(request.form.get('width', None))
height = float(request.form.get('height', None))
file = request.files['picture']
uid = request.form.get('UID', None)
username = request.form.get('email', None).split("@")[0]
filename = username + '.' + file.filename.split('.')[-1]
if os.path.exists(app.config['UPLOAD_FOLDER'] + filename):
os.remove(app.config['UPLOAD_FOLDER'] + filename)
image = Image.open(file)
image = image.crop((x_axis, y_axis, width+x_axis, height+y_axis))
image = image.resize((200, 200), Image.ANTIALIAS)
image.save(app.config['UPLOAD_FOLDER'] + filename)
return filename
def get_news_data():
"""
Retrieve and process the news data
"""
data = _data()
path = "sitedata"
fn = "news.yml"
with open(os.path.join(path, fn), 'r') as f:
data["news"] = yaml.safe_load(f)
years = []
for i,post in enumerate(data['news']['news_items']):
# Gives unique names to each news item to create links later
data['news']['news_items'][i]['post_number'] = 'news_' + str(i)
# List of all the years for the news items
years.append(post['date'].split('-')[0])
data['news']['tag_info'] = {
'year': sorted(set(years), reverse=True)
}
return data
###############################################################################
#
# Pages for the LCP registration, checkout and basic information for the lab.
#
###############################################################################
@app.route("/info/")
@app.route("/info/index")
@app.route("/info/index.html")
def lab_index():
"""
Index page for the lab info
"""
return render_template('info/index.html')
@app.route("/info/reg_form")
@app.route("/info/reg_form.html")
def reg_form():
"""
Registration form for new lab members
"""
return render_template('info/registration_form.html')
@app.route("/info/intro_to_mark_lab")
@app.route("/info/intro_to_mark_lab.html")
def lcp_intro():
"""
Basic lab info
"""
return render_template('info/intro_to_mark_lab.html')
@app.route("/info/check_out_form")
@app.route("/info/check_out_form.html")
def check_out_form():
"""
Form for when a person leaves
"""
return render_template('info/check_out_form.html')
def checkout_form(var):
"""
Checkout form that will be called in the submittion of the 'info/submit'
"""
subject = 'LCP checkout form - {0}'.format(var['email'])
content = "\nLastname: {0}\nFirstname: {1}\nEnd date: {2}\n\
Archive location:\nMachine name: {3}\nFilepath: {4}\n\
Special instructions: {5}\nNew preferred e-mail address: {6}\n\
Valid from (date): {7}\nNew office address: {8}\nValid from (date): {9}\n\
New home address: {10}\nNew telephone number(s): {11}\n\
Anything else: {12}\n".format(
var['lastname'], var['firstname'], var['enddate'], var['machine'],
var['filepath'], var['instructions'], var['email'], var['email-when'],
var['office-address'], var['office-when'], var['home-address'],
var['phone'], var['extra'])
# Log the variables in case an error occurs.
app.logger.info("A person did the checkout form. Variables are the \
following: {}".format(var))
send_email(subject=subject, content=content, sender=var['email'],
rec=app.config['EMAIL_RECIPIENTS'])
return content
def registration_form(var):
'''
Check in form that will be called in the submittion of the 'info/submit'
'''
picture = 'missing.jpg'
if request.files.get("picture"):
app.logger.info("Image found in the registration form.")
if var["y"] and var["x"] and var["height"] and var["width"]:
picture = resize_image()
else:
app.logger.error("ERROR, missing axis in the image")
session['ERROR'] = "Error uploading the image."
picture = 'missing.jpg'
subject = 'LCP registration form - {0}'.format(var['email'])
content = "\nFull Name: {0} {1}\n\nStart date: {2}\nMIT username: {3}\
\nLCP username: {4}\nMIT ID number: {5}\nPreferred e-mail address: {6}\
\nOffice address: {7}\nHome address: {8}\nTelephone number(s): {9}\
\nEmergency contact: {10}\nCurrent project(s) in LCP: {11} \
\nFocus of research: {12}\nBio: {13}\nPicture: {14}\
\nEHS training date: {15}\nHuman studies training date: {16}\
\nAnything else: {17}\n".format(
var['firstname'], var['lastname'], var['startdate'], var['username'],
var['lcp_username'], var['id'], var['email'], var['office-address'],
var['home-address'], var['phone'], var['emergency-contact'],
var['research'], var['Other'], var['Bio'], picture,
var['ehs_training'], var['extra'], var['human_studies_training'])
app.logger.info("Checking form submitted:\n{}\n".format(var))
send_email(subject=subject, content=content, sender=var['email'],
rec=app.config['EMAIL_RECIPIENTS'])
return content
@app.route("/info/submit", methods=['POST'])
def submit():
"""
This form accept both registration and checkout form.
"""
app.logger.info("Person doing the registration\n")
app.logger.info(request.form)
var = {'firstname': request.form.get('firstname', None),
'lastname': request.form.get('lastname', None),
'startdate': request.form.get('startdate', None),
'username': request.form.get('username', None),
'lcp_username': request.form.get('lcp_username', None),
'id': request.form.get('id', None),
'email': request.form.get('email', None),
'office-address': request.form.get('office-address', None),
'home-address': request.form.get('home-address', None),
'phone': request.form.get('phone', None),
'emergency-contact': request.form.get('emergency-contact', None),
'research': request.form.get('research', None),
'Other': request.form.get('Other', None),
'Bio': request.form.get('Bio', None),
'ehs_training': request.form.get('ehs_training', None),
'human_studies_training': request.form.get('human_studies_training',
None),
'extra': request.form.get('extra', None),
'enddate': request.form.get('enddate', None),
'machine': request.form.get('machine', None),
'filepath': request.form.get('filepath', None),
'instructions': request.form.get('instructions', None),
'email-when': request.form.get('email-when', None),
'office-when': request.form.get('office-when', None),
'y': request.form.get("y", None), 'x': request.form.get("x", None),
'height': request.form.get("height", None),
'width': request.form.get("width", None)}
content = ''
if request.form.get('Registration_form', 'None') != 'None':
content = registration_form(var)
elif request.form.get('Checkout_form', 'None') != 'None':
content = checkout_form(var)
else:
return render_template('info/index.html')
return render_template('info/submit.html',
Content=content.replace("\n", "<br>"))
###############################################################################
#
# Main pages for the LCP website
#
###############################################################################
@app.route("/")
@app.route("/home")
@app.route("/index")
@app.route("/index.html")
@app.route("/index.shtml")
def index():
"""
LCP index page
"""
data = get_news_data()
return render_template('index.html', **data)
@app.route("/about")
@app.route("/about.html")
@app.route("/about.shtml")
def about():
"""
About page
"""
return render_template('about.html')
@app.route("/publications")
@app.route("/publications.html")
@app.route("/publications.shtml")
def publications():
"""
Publications page
"""
return render_template('publications.html')
@app.route("/rgm_publications")
@app.route("/rgm_publications.html")
def rgm_publications():
"""
Special publications page for RGM
"""
return render_template('rgm_publications.html')
@app.route("/brp_references")
@app.route("/brp_references.html")
def brp_references():
"""
Special page for KP
"""
return render_template('brp_references.html')
@app.route("/mimic")
@app.route("/mimic.html")
@app.route("/mimic.shtml")
def mimic():
"""
Info page for Critical Care Informatics
"""
return render_template('mimic.html')
@app.route("/physionet")
@app.route("/physionet.html")
@app.route("/physionet.shtml")
def physionet():
"""
Info page for PhysioNet
"""
return render_template('physionet.html')
@app.route("/brp")
@app.route("/brp.html")
@app.route("/brp.shtml")
def brp():
"""
OLD brp page
"""
return render_template('brp.html')
@app.route("/people")
@app.route("/people.html")
@app.route("/people.shtml")
def people():
"""
Display a list of people.
"""
data = _data()
path = 'sitedata'
fn = 'people.yml'
with open(os.path.join(path, fn), 'r') as f:
data['people'] = yaml.safe_load(f)
return render_template('people.html', **data)
@app.route("/news")
@app.route("/news.html")
@app.route("/news.shtml")
def news():
"""
Display a list of news items.
"""
data = get_news_data()
return render_template('news.html', **data)
@app.route("/<news_id>")
def news_item(news_id):
"""
Display an individual news item.
"""
news = get_news_data()
post = [x for x in news['news']['news_items']
if x['post_number'] == news_id]
if post:
data = {}
data['post'] = post[0]
return render_template("news_post.html", **data)
else:
return render_template('404.html')
if __name__ == "__main__":
app.jinja_env.auto_reload = True
app.run(host='0.0.0.0', port=8083, threaded=True, debug=True)