adaugat lab4 si lab5
Signed-off-by: hellisabove <robertnedela15@gmail.com>
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
from flask import session, Blueprint, request, redirect, render_template
|
||||
from ..model.account import check_login
|
||||
|
||||
|
||||
auth_pages = Blueprint("auth", __name__)
|
||||
|
||||
@auth_pages.route("/login", methods=["GET", "POST"])
|
||||
def login_page():
|
||||
error = ""
|
||||
if "email" in request.form:
|
||||
email = request.form["email"]
|
||||
password = request.form["password"]
|
||||
if check_login(email, password):
|
||||
session["email"] = email
|
||||
session["auth"] = True
|
||||
return redirect("/", code=302)
|
||||
else:
|
||||
error = "Invalid username or password"
|
||||
return render_template("login.html", error=error)
|
||||
|
||||
@auth_pages.route("/logout")
|
||||
def logout():
|
||||
session["auth"] = False
|
||||
return redirect("/")
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
from flask import session, Blueprint, request, redirect, render_template
|
||||
from ..model.sensors import fetch_iot_data
|
||||
|
||||
|
||||
index_pages = Blueprint("index", __name__)
|
||||
|
||||
@index_pages.route("/")
|
||||
def index_page():
|
||||
if not session.get("auth", False):
|
||||
return redirect("/login", code=302)
|
||||
sensors = fetch_iot_data()
|
||||
return render_template("index.html", sensors=sensors)
|
||||
|
||||
@index_pages.route("/contact")
|
||||
def contact_page():
|
||||
return render_template("contact.html")
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from ..model.sensors import iot_data, set_iot_data, save_data
|
||||
import os, json
|
||||
|
||||
# TODO: create a Blueprint + routes for 'api/iot' endpoint
|
||||
iot_api = Blueprint("iot_api", __name__)
|
||||
|
||||
DATA_FILE = "/home/nedi/iap1-labs/lab4/iot_data.json"
|
||||
|
||||
@iot_api.route("/api/iot", methods=["GET"])
|
||||
def get_sensor_data():
|
||||
return jsonify(iot_data)
|
||||
|
||||
@iot_api.route("/api/iot", methods=["POST"])
|
||||
def update_sensor_data():
|
||||
payload = request.get_json(silent=True)
|
||||
|
||||
set_iot_data(payload)
|
||||
|
||||
save_data(DATA_FILE)
|
||||
return jsonify(iot_data)
|
||||
|
||||
@iot_api.route("/api/iot", methods=["DELETE"])
|
||||
def reset_sensor_data():
|
||||
for key in iot_data:
|
||||
iot_data[key] = 0
|
||||
save_data(DATA_FILE)
|
||||
return jsonify(iot_data)
|
||||
Reference in New Issue
Block a user