-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
50 lines (36 loc) · 1.1 KB
/
server.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
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
class Timesheet(db.Model):
id = db.Column(db.Integer, primary_key=True)
slots = db.Column(db.Text())
def __init__(self, slots):
self.slots = slots
@app.route('/')
def hello_world():
return render_template('index.html')
@app.route('/slots', methods=['GET', 'POST'])
def slots():
if request.method == 'POST':
_save_slots()
return 'Ok'
else:
return _return_slots(), 200, {'Content-Type': 'application/json'}
def _save_slots():
timesheets = Timesheet.query.all();
if not timesheets:
timesheet = Timesheet(slots=request.data)
db.session.add(timesheet)
else:
timesheet = timesheets[0]
timesheet.slots = request.data
db.session.commit()
def _return_slots():
timesheets = Timesheet.query.all();
if timesheets:
return timesheets[0].slots
return '[]'
if __name__ == '__main__':
app.run(debug=True)