-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.py
76 lines (64 loc) · 2.2 KB
/
frontend.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
import sqlite3, re
from flask import Flask, flash, request, jsonify, render_template
from flask_httpauth import HTTPBasicAuth
from enum import Enum
REGISTERED = {
"admin": "A98xC2qALFKD" # Regenerate pair for live/production deployment
}
""" Label for batch mode """
class BatchMode(Enum):
Text = "text"
URL = "url"
app = Flask(__name__)
auth = HTTPBasicAuth()
app.secret_key = 'YwycT897iWAr' # For session management, regenerate for live server
""" Main page for web app """
@app.route('/medfact/', methods=['GET'])
@auth.login_required
def home():
return render_template('gui.html')
""" Page to process form submission """
@app.route('/medfact/submit/', methods=['POST'])
@auth.login_required
def submit():
mode = request.form.get('mode').strip()
content = request.form.get('content').strip()
email = request.form.get('email').strip()
err = False
if re.search('^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$', email) == None:
flash("Invalid email provided", 'danger')
err = True
if mode == BatchMode.Text.value:
text_size = len(content.split())
if text_size > 100:
flash("Text is too long", 'danger')
err = True
elif text_size < 3:
flash("Text is too short", 'danger')
err = True
elif mode == BatchMode.URL.value:
num_urls = len(content.split('\n'))
valid_urls = content.count('http')
if num_urls > 10:
flash("Too many URLs", 'danger')
err = True
elif num_urls < 1 or num_urls != valid_urls:
flash("Not enough valid URLs have been entered", 'danger')
err = True
else:
flash("Invalid batch mode selected", 'danger')
err = True
if err == False:
db = sqlite3.connect('batch.sqlite')
cursor = db.cursor()
cursor.execute("INSERT INTO jobs (mode, content, email) VALUES (?,?,?)", (mode, content, email))
db.commit()
db.close()
flash('Submitted batch request successfully, results will be sent via email when processed', 'info')
return render_template('gui.html')
@auth.get_password
def get_pwd(username):
if username in REGISTERED: return REGISTERED.get(username)
else: return None
if __name__ == "__main__":
app.run(host='0.0.0.0',debug=False,threaded=True) # Serve RESTful API