Work
This commit is contained in:
parent
4cf574ced8
commit
b1b21703c7
14
app.py
14
app.py
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
from flask import Flask, redirect, request, render_template, send_from_directory
|
from flask import Flask, redirect, request, render_template, send_from_directory
|
||||||
import json
|
import json
|
||||||
import os
|
|
||||||
from os.path import join
|
from os.path import join
|
||||||
import uuid
|
import uuid
|
||||||
from . import db, config, scanner, calibration
|
from . import db, config, scanner, calibration
|
||||||
|
|
@ -21,8 +20,7 @@ def hello_world():
|
||||||
def create_object():
|
def create_object():
|
||||||
conn = db.get()
|
conn = db.get()
|
||||||
with conn:
|
with conn:
|
||||||
object = db.Object.create(request.form.get('name'), conn)
|
db.Object.create(request.form.get('name'), conn)
|
||||||
os.makedirs(join(config.DATA_DIR, str(object.id), 'previews'))
|
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -30,8 +28,14 @@ def create_object():
|
||||||
def object(id: int):
|
def object(id: int):
|
||||||
conn = db.get()
|
conn = db.get()
|
||||||
object = db.Object.get_from_id(id, conn)
|
object = db.Object.get_from_id(id, conn)
|
||||||
calibration = object.calibration(conn)
|
return render_template('object.html', object=object)
|
||||||
return render_template('object.html', object=object, calibration=calibration)
|
|
||||||
|
|
||||||
|
@app.route('/scan/<id>')
|
||||||
|
def scan(id: int):
|
||||||
|
conn = db.get()
|
||||||
|
object = db.Object.get_from_id(id, conn)
|
||||||
|
return render_template('object.html', object=object)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/calibrate/<id>")
|
@app.route("/calibrate/<id>")
|
||||||
|
|
|
||||||
122
db.py
122
db.py
|
|
@ -37,15 +37,6 @@ def init_app(app):
|
||||||
app.cli.add_command(init)
|
app.cli.add_command(init)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
db = sqlite3.connect(
|
|
||||||
os.environ.get('DATABASE_PATH', 'db.sqlite'),
|
|
||||||
detect_types=sqlite3.PARSE_DECLTYPES,
|
|
||||||
)
|
|
||||||
db.row_factory = sqlite3.Row
|
|
||||||
init(db)
|
|
||||||
|
|
||||||
|
|
||||||
class CalibrationState(IntEnum):
|
class CalibrationState(IntEnum):
|
||||||
Empty = 0
|
Empty = 0
|
||||||
HasData = 1
|
HasData = 1
|
||||||
|
|
@ -91,36 +82,72 @@ class Calibration:
|
||||||
return Calibration.from_row(response.fetchone())
|
return Calibration.from_row(response.fetchone())
|
||||||
|
|
||||||
|
|
||||||
class Object:
|
class Acquisition:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def select_args() -> str:
|
def select_args() -> str:
|
||||||
return 'id, name, calibration_id'
|
return 'id, calibration_id, object_id'
|
||||||
|
|
||||||
def __init__(self, object_id: int, name: str, calibration: int):
|
def __init__(self, acquisition_id: int, calibration_id: int, object_id: int):
|
||||||
self.id = object_id
|
self.id = object_id
|
||||||
self.name = name
|
self.calibration_id = calibration_id
|
||||||
self.calibration_id = calibration
|
self.object_id = object_id
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def create(name: str, db: sqlite3.Connection) -> 'Object':
|
|
||||||
cur = db.cursor()
|
|
||||||
response = cur.execute(
|
|
||||||
'INSERT INTO object(name, calibration_id) VALUES (?, ?) RETURNING ' + Object.select_args() + ';',
|
|
||||||
[name, None]
|
|
||||||
)
|
|
||||||
return Object.from_row(response.fetchone())
|
|
||||||
|
|
||||||
def save(self, db: sqlite3.Connection):
|
def save(self, db: sqlite3.Connection):
|
||||||
cur = db.cursor()
|
cur = db.cursor()
|
||||||
cur.execute(
|
cur.execute(
|
||||||
'UPDATE object SET name = ?, calibration_id = ? WHERE id = ?',
|
'UPDATE acquisition SET calibration_id = ?, object_id = ? WHERE id = ?',
|
||||||
[self.name, self.calibration_id, self.id]
|
[self.calibration_id, self.objct_id, self.id]
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_row(row: sqlite3.Row) -> 'Acquisition':
|
||||||
|
return Acquisition(*row)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_from_id(acquisition_id: int, db: sqlite3.Connection) -> Optional['Acquisition']:
|
||||||
|
cur = db.cursor()
|
||||||
|
response = cur.execute(
|
||||||
|
'SELECT ' + Acquisition.select_args() + ' FROM acquisition WHERE id = ?;',
|
||||||
|
[acquisition_id]
|
||||||
|
)
|
||||||
|
return Acquisition.from_row(response.fetchone())
|
||||||
|
|
||||||
|
def calibration(self, db: sqlite3.Connection) -> Optional[Calibration]:
|
||||||
|
if self.calibration_id is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return Calibration.get_from_id(self.calibration_id, db)
|
||||||
|
|
||||||
|
|
||||||
|
class Object:
|
||||||
|
@staticmethod
|
||||||
|
def select_args() -> str:
|
||||||
|
return 'id, name'
|
||||||
|
|
||||||
|
def __init__(self, object_id: int, name: str):
|
||||||
|
self.id = object_id
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
def save(self, db: sqlite3.Connection):
|
||||||
|
cur = db.cursor()
|
||||||
|
cur.execute(
|
||||||
|
'UPDATE object SET name = ? WHERE id = ?',
|
||||||
|
[self.name, self.id]
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_row(row: sqlite3.Row) -> 'Object':
|
def from_row(row: sqlite3.Row) -> 'Object':
|
||||||
return Object(*row)
|
return Object(*row)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def create(name: str, db: sqlite3.Connection) -> 'Object':
|
||||||
|
cur = db.cursor()
|
||||||
|
response = cur.execute(
|
||||||
|
'INSERT INTO object(name) VALUES (?) RETURNING ' + Object.select_args() + ';',
|
||||||
|
[name]
|
||||||
|
)
|
||||||
|
return Object.from_row(response.fetchone())
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_from_id(object_id: int, db: sqlite3.Connection) -> Optional['Object']:
|
def get_from_id(object_id: int, db: sqlite3.Connection) -> Optional['Object']:
|
||||||
cur = db.cursor()
|
cur = db.cursor()
|
||||||
|
|
@ -139,8 +166,45 @@ class Object:
|
||||||
)
|
)
|
||||||
return list(map(Object.from_row, response.fetchall()))
|
return list(map(Object.from_row, response.fetchall()))
|
||||||
|
|
||||||
def calibration(self, db: sqlite3.Connection) -> Optional[Calibration]:
|
def add_acquisition(self, calibration_id: int, db: sqlite3.Connection) -> Acquisition:
|
||||||
if self.calibration_id is None:
|
cur = db.cursor()
|
||||||
return None
|
response = cur.execute(
|
||||||
|
'INSERT INTO acquisition(calibration_id, object_id) VALUES (?, ?) RETURNING ' + Acquisition.select_args() + ';',
|
||||||
|
[calibration_id, self.id]
|
||||||
|
)
|
||||||
|
return Acquisition.from_row(response.fetchone())
|
||||||
|
|
||||||
return Calibration.get_from_id(self.calibration_id, db)
|
|
||||||
|
def main():
|
||||||
|
db = sqlite3.connect(
|
||||||
|
os.environ.get('DATABASE_PATH', 'db.sqlite'),
|
||||||
|
detect_types=sqlite3.PARSE_DECLTYPES,
|
||||||
|
)
|
||||||
|
db.row_factory = sqlite3.Row
|
||||||
|
init(db)
|
||||||
|
|
||||||
|
# Create a new object
|
||||||
|
with db:
|
||||||
|
object = Object.create('Mon premier objet', db)
|
||||||
|
calibration = Calibration.create(db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
calibration = Calibration.create(db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
|
||||||
|
object = Object.create('Mon deuxième objet', db)
|
||||||
|
calibration = Calibration.create(db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
calibration = Calibration.create(db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
object.add_acquisition(calibration.id, db)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
|
||||||
15
schema.sql
15
schema.sql
|
|
@ -1,14 +1,21 @@
|
||||||
DROP TABLE IF EXISTS object;
|
|
||||||
DROP TABLE IF EXISTS calibration;
|
DROP TABLE IF EXISTS calibration;
|
||||||
|
DROP TABLE IF EXISTS acquisition;
|
||||||
|
DROP TABLE IF EXISTS object;
|
||||||
|
|
||||||
CREATE TABLE calibration (
|
CREATE TABLE calibration (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
state INTEGER NOT NULL
|
state INTEGER NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE acquisition (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
calibration_id INTEGER NOT NULL,
|
||||||
|
object_id INTEGER NOT NULL,
|
||||||
|
CONSTRAINT fk_calibration FOREIGN KEY(calibration_id) REFERENCES calibration(id),
|
||||||
|
CONSTRAINT fk_object FOREIGN KEY(object_id) REFERENCES object(id)
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE object (
|
CREATE TABLE object (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL
|
||||||
calibration_id INTEGER,
|
|
||||||
CONSTRAINT fk_calibration FOREIGN KEY(calibration_id) REFERENCES calibration(id)
|
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
<section class="section">
|
<section class="section">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1 class="title">{{ object.name }}</h1>
|
<h1 class="title">{{ object.name }}</h1>
|
||||||
|
{#
|
||||||
<div class="field is-grouped is-grouped-multiline">
|
<div class="field is-grouped is-grouped-multiline">
|
||||||
<div class="control">
|
<div class="control">
|
||||||
{% if calibration is none or calibration.state == 0 %}
|
{% if calibration is none or calibration.state == 0 %}
|
||||||
|
|
@ -30,6 +31,8 @@
|
||||||
<a href="#" title="Non implémenté" disabled="disabled" class="button is-link">Utiliser le dernier étalonnage connu</a>
|
<a href="#" title="Non implémenté" disabled="disabled" class="button is-link">Utiliser le dernier étalonnage connu</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
#}
|
||||||
|
<a href="/scan/{{ object.id }}">Faire un scan</a>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue