-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
90 lines (76 loc) · 3.1 KB
/
__init__.py
File metadata and controls
90 lines (76 loc) · 3.1 KB
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
from flask import Flask, render_template, url_for, json, redirect, request, flash
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Test
from datetime import datetime, timedelta
app = Flask(__name__)
engine = create_engine('postgresql://vagrant:password@localhost/playground')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
@app.route('/')
def homepage():
# session.rollback()
tests = session.query(Test).all()
return render_template('homepage.html', tests=tests)
@app.route('/test/new/', methods=['GET', 'POST'])
def newTest():
if request.method == 'POST':
form = parseForm(request.form)
newtest = Test(
integer=form['integer'],
floatpt=form['floatpt'],
string=request.form['string'],
uni=request.form['uni'],
boolean=form['boolean'],
date=form['date'],
time=form['time'],
datetime=form['datetime'],
interval=timedelta(days=form['days'], seconds=form['seconds'], microseconds=form['microseconds']),
int_array=form['int_array'],
str_array=form['str_array'],
json_object=form['json_object']
)
session.add(newtest)
session.commit()
flash('New Test Added!')
return redirect(url_for('homepage'))
else:
return render_template('newtest.html')
@app.route('/formplayground/')
def formPlayground():
return render_template('formplayground.html')
# helper function
def parseForm(form):
form_dict = {}
form_dict['integer'] = form['integer'] if form['integer'] else 0
form_dict['floatpt'] = form['floatpt'] if form['floatpt'] else 0.0
form_dict['boolean'] = True if form.get('boolean') else False
form_dict['date'] = datetime.strptime(form['date'], '%Y-%m-%d').date() if form['date'] else None
form_dict['time'] = datetime.strptime(form['time'], '%H:%M').time() if form['time'] else None
form_dict['datetime'] = datetime.strptime(form['datetime'], '%Y-%m-%dT%H:%M') if form['datetime'] else None
form_dict['days'] = int(form['days']) if form['days'] else 0
form_dict['seconds'] = int(form['seconds']) if form['seconds'] else 0
form_dict['microseconds'] = int(form['microseconds']) if form['microseconds'] else 0
int_array1 = int(form['int_array1']) if form['int_array1'] else 0
int_array2 = int(form['int_array2']) if form['int_array2'] else 0
int_array3 = int(form['int_array3']) if form['int_array3'] else 0
form_dict['int_array'] = [int_array1, int_array2, int_array3]
form_dict['str_array'] = [form['str_array1'], form['str_array2'], form['str_array3']]
json_object = {
"instructions": [
{
"text": 1,
"image": 2,
"video": 3,
"gif": 4,
"note": 5
}
]
}
form_dict['json_object'] = json.dumps(json_object)
return form_dict
if __name__ == '__main__':
app.secret_key = 'secret_key'
app.debug = True
app.run(host='0.0.0.0', port=8080)