diff --git a/.gitignore b/.gitignore index f71342e..0cdc4cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +db.sqlite data node_modules static/calibration-visualiser.* diff --git a/app.py b/app.py index fc8ace5..7df7358 100755 --- a/app.py +++ b/app.py @@ -1,13 +1,16 @@ #!/usr/bin/env python from flask import Flask, render_template, send_from_directory +from . import db app = Flask(__name__) @app.route("/") def hello_world(): - return render_template('index.html') + conn = db.get() + objects = db.Object.all(conn) + return render_template('index.html', objects=objects) @app.route("/calibration") diff --git a/db.py b/db.py new file mode 100755 index 0000000..1cebd6b --- /dev/null +++ b/db.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python + +from flask import current_app, g +import os +import sqlite3 +import typing +from typing import Optional + + +def get() -> sqlite3.Connection: + if 'db' not in g: + g.db = sqlite3.connect( + os.environ.get('DATABASE_PATH', 'db.sqlite'), + detect_types=sqlite3.PARSE_DECLTYPES, + ) + g.db.row_factory = sqlite3.Row + + return g.db + + +def init(db): + with open('schema.sql', 'r') as f: + db.executescript(f.read()) + + +def close(): + db = g.pop('db', None) + + if db is not None: + db.close() + + +def init_app(app): + app.teardown_appcontext(close) + 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 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 + def from_row(row: sqlite3.Row) -> 'Object': + return Object(*row) + + @staticmethod + def get_from_id(object_id: int, db: sqlite3.Connection) -> Optional['Object']: + cur = db.cursor() + response = cur.execute( + 'SELECT ' + Object.select_args() + ' FROM object WHERE id = ?;', + [object_id] + ) + return Object.from_row(response.fetchone()) + + @staticmethod + def all(db: sqlite3.Connection) -> list['Object']: + cur = db.cursor() + response = cur.execute( + 'SELECT ' + Object.select_args() + ' FROM object;', + [] + ) + return list(map(Object.from_row, response.fetchall())) diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..f9cc648 --- /dev/null +++ b/schema.sql @@ -0,0 +1,6 @@ +DROP TABLE IF EXISTS object; + +CREATE TABLE object ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL +); diff --git a/templates/index.html b/templates/index.html index 8abec17..c14eb81 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,9 +4,15 @@

Bienvenue sur NenuScanner

-

- Cliquez ici pour visualiser l'étalonnage! -

+ {% if objects %} + + {% else %} +

Il n'y a aucun object pour le moment...

+ {% endif %}
{% endblock content %}