nenuscanner/app.py

76 lines
1.9 KiB
Python
Executable File

#!/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/<id>')
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/<id>")
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/<id>")
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/<id>")
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/<id>")
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/<path:path>')
def send_static(path):
return send_from_directory('static', path)
@app.route('/data/<path:path>')
def send_data(path):
return send_from_directory('data', path)