all repos — iot-project @ 0627d8a0865451db9600af9ebcb04e083c391149

backend_iot/views.py (view raw)

 1from backend_iot import app
 2from backend_iot.db import get_latest_record, handle_new_data, record_to_dict
 3from flask import request, render_template, abort
 4
 5def latest_or_abort():
 6    latest = get_latest_record()
 7    if latest is None:
 8        return abort(400)
 9    return record_to_dict(latest)
10
11@app.route("/")
12def index_route():
13    if request.method == 'GET':
14        return render_template("index.html")
15
16@app.route("/data", methods = ['GET', 'POST'])
17def data_route():
18    if request.method == "GET":
19        return latest_or_abort()
20    return handle_new_data(request.form)