-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
148 lines (119 loc) · 4.3 KB
/
app.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
import os
from flask import Flask, request, jsonify, redirect
from flask_sqlalchemy import SQLAlchemy
from flask.ext.cors import CORS
from datetime import datetime
app = Flask(__name__)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:////tmp/test.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
from model import *
@app.route('/')
def index():
return 'Banish The Beep API'
@app.route('/create_all')
def create_tables():
db.create_all()
return jsonify({ 'success' : True })
@app.route('/drop_all')
def drop_tables():
db.drop_all()
return jsonify({ 'success' : True })
@app.route('/job/create', methods=['POST'])
def create():
# TODO VALIDATION
team_id = request.form['team_id']
patient_id = request.form['patient_id']
urgency = request.form['urgency']
creator_comment = request.form['creator_comment']
doctor_comment = request.form['doctor_comment'] if 'doctor_comment' in request.form else ''
bed = request.form['bed']
ward = request.form['ward']
location = request.form['location'] if 'location' in request.form else ''
creator_name = request.form['creator_name']
job = Job(team_id, patient_id, urgency, creator_comment, doctor_comment, bed, ward, location, creator_name)
db.session.add(job)
db.session.commit()
return redirect("http://hat-970.getforge.io/list", code=200)
@app.route('/job/read')
def read_all():
#rows = db.session.query(Team, Job, Doctor).filter(Job.done is None).all()
rows = Job.query.filter_by(done=None)
json_jobs = []
for job in rows:
json_jobs.append({
'id': job.id,
'team_id': job.team_id,
'patient_id': job.patient_id,
'urgency': job.urgency,
'creator_comment': job.creator_comment,
'doctor_comment': job.doctor_comment,
'bed': job.bed,
'ward': job.ward,
'location': job.location,
'creator_id': job.creator_id,
'creator_name': job.creator_name,
'acknowledged': job.acknowledged,
'done': job.done,
'created_at': job.created_at
})
return jsonify({ 'jobs' : json_jobs })
@app.route('/job/read/<team_id>')
def read(team_id):
rows = Job.query.filter_by(team_id=team_id, done=None)
json_jobs = []
for job in rows:
json_jobs.append({
'id': job.id,
'team_id': job.team_id,
'patient_id': job.patient_id,
'urgency': job.urgency,
'creator_comment': job.creator_comment,
'doctor_comment': job.doctor_comment,
'bed': job.bed,
'ward': job.ward,
'location': job.location,
'creator_id': job.creator_id,
'creator_name': job.creator_name,
'acknowledged': job.acknowledged,
'done': job.done,
'created_at': job.created_at
})
return jsonify({ 'jobs' : json_jobs })
@app.route('/job/update/<id>', methods=['POST'])
def update(id):
job = Job.query.get(id)
if 'team_id' in request.form:
job.team_id = request.form['team_id']
if 'patient_id' in request.form:
job.patient_id = request.form['patient_id']
if 'urgency' in request.form:
job.urgency = request.form['urgency']
if 'creator_comment' in request.form:
job.creator_comment = request.form['creator_comment']
if 'doctor_comment' in request.form:
job.doctor_comment = request.form['doctor_comment']
if 'bed' in request.form:
job.bed = request.form['bed']
if 'ward' in request.form:
job.ward = request.form['ward']
if 'location' in request.form:
job.location = request.form['location']
if 'creator_name' in request.form:
job.creator_name = request.form['creator_name']
if 'acknowledged' in request.form:
job.acknowledged = datetime.now()
if 'done' in request.form:
job.done = datetime.now()
db.session.add(job)
db.session.commit()
return jsonify({ 'success' : True })
if not app.debug:
import logging
from logging import FileHandler
file_handler = FileHandler('app_debug.log')
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
if __name__ == "__main__":
app.run()