#!/usr/bin/env python from flask import Flask, redirect, request, render_template, send_from_directory, jsonify import os from os.path import join import uuid from . import db, config, scanner app = Flask(__name__) @app.route("/") def hello_world(): conn = db.get() objects = db.Object.all(conn) return render_template('index.html', objects=objects) @app.route("/create-object/", methods=["POST"]) def create_object(): conn = db.get() with conn: object = db.Object.create(request.form.get('name'), conn) os.makedirs(join(config.DATA_DIR, str(object.id), 'previews')) return redirect('/') @app.route('/object/') def object(id: int): conn = db.get() object = db.Object.get_from_id(id, conn) return render_template('object.html', object=object) @app.route("/calibrate/") def calibrate(id: int): conn = db.get() object = db.Object.get_from_id(id, conn) return render_template('calibrate.html', object=object) @app.route("/api/preview/") def preview(id: int): conn = db.get() db.Object.get_from_id(id, conn) capture_uuid = uuid.uuid4() if scanner.capture(join(config.DATA_DIR, str(id), 'previews', str(capture_uuid) + '.jpg')): return str(capture_uuid) else: return "Impossible de capturer l'image.", 500 @app.route("/api/scan-for-calibration/") def scan_calibration(id: int): conn = db.get() db.Object.get_from_id(id, conn) scanner.scan(join(config.DATA_DIR, id, 'calibration')) return jsonify(config.LEDS_UUIDS) @app.route("/calibration/") def calibration(id: int): conn = db.get() object = db.Object.get_from_id(id, conn) return render_template('calibration.html', object=object) @app.route('/static/') def send_static(path): return send_from_directory('static', path) @app.route('/data/') def send_data(path): return send_from_directory('data', path)