Working
This commit is contained in:
parent
b1b21703c7
commit
aa55934bf0
34
app.py
34
app.py
|
|
@ -1,12 +1,24 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from flask import Flask, redirect, request, render_template, send_from_directory
|
||||
from flask import Flask, redirect, request, render_template, send_from_directory, session
|
||||
import json
|
||||
import os
|
||||
from os.path import join
|
||||
import sqlite3
|
||||
from typing import Optional
|
||||
import uuid
|
||||
from . import db, config, scanner, calibration
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = os.urandom(20).hex()
|
||||
|
||||
|
||||
def get_calibration(conn: sqlite3.Connection) -> Optional[db.Calibration]:
|
||||
calibration_id = session.get('calibration_id', None)
|
||||
if calibration_id is None:
|
||||
return None
|
||||
|
||||
return db.Calibration.get_from_id(calibration_id, conn)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
|
|
@ -28,7 +40,7 @@ def create_object():
|
|||
def object(id: int):
|
||||
conn = db.get()
|
||||
object = db.Object.get_from_id(id, conn)
|
||||
return render_template('object.html', object=object)
|
||||
return render_template('object.html', object=object, calibration=get_calibration(conn))
|
||||
|
||||
|
||||
@app.route('/scan/<id>')
|
||||
|
|
@ -38,19 +50,13 @@ def scan(id: int):
|
|||
return render_template('object.html', object=object)
|
||||
|
||||
|
||||
@app.route("/calibrate/<id>")
|
||||
def calibrate(id: int):
|
||||
@app.route("/calibrate/")
|
||||
def calibrate():
|
||||
conn = db.get()
|
||||
object = db.Object.get_from_id(id, conn)
|
||||
if object.calibration_id is None:
|
||||
with conn:
|
||||
calibration = db.Calibration.create(conn)
|
||||
object.calibration_id = calibration.id
|
||||
object.save(conn)
|
||||
else:
|
||||
calibration = object.calibration(conn)
|
||||
|
||||
return render_template('calibrate.html', object=object, calibration=calibration, leds=config.LEDS_UUIDS)
|
||||
with conn:
|
||||
calibration = db.Calibration.create(conn)
|
||||
session['calibration_id'] = calibration.id
|
||||
return render_template('calibrate.html', calibration=calibration)
|
||||
|
||||
|
||||
@app.route("/api/preview/<id>")
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
</div>
|
||||
</div>
|
||||
<progress id="progress-bar" class="progress is-link" style="display: none;" value="0" max="1000"></progress>
|
||||
<div id="calibrate" {% if calibration.state == 0 %}style="display: none;"{% endif %}>
|
||||
<div id="calibrate" style="display: none;">
|
||||
<p>Si les données d'étalonnage conviennent, appuyez sur le bouton ci-dessous pour procéder à l'étalonnage du scanner</p>
|
||||
<button id="calibrate-button" class="button is-link">Étalonner le scanner</button>
|
||||
<p id="calibration-info"></p>
|
||||
|
|
@ -58,11 +58,11 @@
|
|||
previewButton.addEventListener('click', async () => {
|
||||
buttons.forEach(x => x.setAttribute('disabled', 'disabled'));
|
||||
previewButton.classList.add('is-loading');
|
||||
let response = await fetch('/api/preview/{{ object.id }}');
|
||||
let response = await fetch('/api/preview/{{ calibration.id }}');
|
||||
let uuid = await response.text();
|
||||
|
||||
if (response.status >= 200 && response.status < 400) {
|
||||
previewImage.src = "/data/{{ object.id }}/previews/" + uuid + ".jpg";
|
||||
previewImage.src = "/data/{{ calibration.id }}/previews/" + uuid + ".jpg";
|
||||
previewImage.style.display = "block";
|
||||
errorContainer.style.display = "none";
|
||||
} else {
|
||||
|
|
@ -74,20 +74,6 @@
|
|||
buttons.forEach(x => x.removeAttribute('disabled'));
|
||||
});
|
||||
|
||||
// If we already have calibration images, we show them right now
|
||||
if ({% if calibration.state > 0 %}true{% else %}false{% endif %}) {
|
||||
let cell, img;
|
||||
{% for led in leds %}
|
||||
cell = document.createElement('div');
|
||||
cell.classList.add('cell');
|
||||
img = document.createElement('img');
|
||||
img.classList.add('is-loading');
|
||||
img.src = '/data/calibration/{{ calibration.id }}/{{ led }}.jpg?v=0';
|
||||
cell.appendChild(img);
|
||||
grid.appendChild(cell);
|
||||
{% endfor %}
|
||||
}
|
||||
|
||||
scanButton.addEventListener('click', async () => {
|
||||
scanIndex++;
|
||||
progress = 0;
|
||||
|
|
@ -126,7 +112,7 @@
|
|||
calibrateButton.addEventListener('click', async () => {
|
||||
calibrateButton.classList.add('is-loading');
|
||||
|
||||
await fetch('/api/calibrate/{{ object.id }}');
|
||||
await fetch('/api/calibrate/{{ calibration.id }}');
|
||||
window.location.href = '/calibration/{{ calibration.id }}';
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -32,8 +32,42 @@
|
|||
</div>
|
||||
{% endif %}
|
||||
#}
|
||||
<a href="/scan/{{ object.id }}">Faire un scan</a>
|
||||
{% if calibration %}
|
||||
<a href="/scan/{{ object.id }}" class="button is-link">Faire un scan</a>
|
||||
{% else %}
|
||||
<button class="button is-link" id="scan">Faire un scan</button>
|
||||
<div id="calibration-modal" class="modal">
|
||||
<div class="modal-background"></div>
|
||||
<div class="modal-content">
|
||||
<div class="has-text-centered mb-3">Le scanner n'est pas étalonné.</div>
|
||||
|
||||
<div class="field is-grouped is-grouped-centered">
|
||||
<div class="control">
|
||||
<a href="/calibrate/" class="button is-link">Étalonner le scanner</a>
|
||||
</div>
|
||||
<div class="control">
|
||||
<a href="/calibrate/" class="button is-link">Réutiliser le dernier étalonnage</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="modal-close is-large" aria-label="close"></button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{% endblock content %}
|
||||
|
||||
{% block extrajs %}{% if not calibration %}
|
||||
<script>
|
||||
let modal = document.getElementById('calibration-modal');
|
||||
document.getElementById('scan').addEventListener('click', () => {
|
||||
modal.classList.add('is-active');
|
||||
});
|
||||
(document.querySelectorAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button') || []).forEach((close) => {
|
||||
close.addEventListener('click', () => {
|
||||
modal.classList.remove('is-active');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endif %}{% endblock %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue