diff --git a/app.py b/app.py index 4176398..6123ed2 100755 --- a/app.py +++ b/app.py @@ -49,15 +49,15 @@ def manage_auto_use_last_calibration(): @app.route("/") def index(): conn = db.get() - objects = db.Object.all(conn) - return render_template('index.html', objects=objects) + projects = db.Object.all_by_project(conn) + return render_template('index.html', projects=projects) @app.route("/create-object/", methods=["POST"]) def create_object(): conn = db.get() with conn: - db.Object.create(request.form.get('name'), conn) + db.Object.create(request.form.get('name'), request.form.get('project'), conn) return redirect('/') diff --git a/db.py b/db.py index 6df26ec..9d65f00 100755 --- a/db.py +++ b/db.py @@ -3,6 +3,7 @@ from enum import IntEnum from datetime import datetime from flask import g +import itertools import os from os.path import join import shutil @@ -173,26 +174,34 @@ class Acquisition: class FullObject: - def __init__(self, object_id: int, name: str, acquisitions: list[Acquisition]): + def __init__(self, object_id: int, name: str, project: str, acquisitions: list[Acquisition]): self.id = object_id self.name = name + self.project = project self.acquisitions = acquisitions +class Project: + def __init__(self, name: str, objects: list['Object']): + self.name = name + self.objects = objects + + class Object: @staticmethod def select_args() -> str: - return 'id, name' + return 'id, name, project' - def __init__(self, object_id: int, name: str): + def __init__(self, object_id: int, name: str, project: str): self.id = object_id self.name = name + self.project = project def save(self, db: sqlite3.Connection): cur = db.cursor() cur.execute( - 'UPDATE object SET name = ? WHERE id = ?', - [self.name, self.id] + 'UPDATE object SET name = ?, project = ? WHERE id = ?', + [self.name, self.project, self.id] ) @staticmethod @@ -202,11 +211,11 @@ class Object: return Object(*row) @staticmethod - def create(name: str, db: sqlite3.Connection) -> 'Object': + def create(name: str, project: str, db: sqlite3.Connection) -> 'Object': cur = db.cursor() response = cur.execute( - 'INSERT INTO object(name) VALUES (?) RETURNING ' + Object.select_args() + ';', - [name] + 'INSERT INTO object(name, project) VALUES (?, ?) RETURNING ' + Object.select_args() + ';', + [name, project] ) object = Object.from_row(response.fetchone()) os.makedirs(join(config.OBJECT_DIR, str(object.id))) @@ -230,6 +239,13 @@ class Object: ) return list(map(Object.from_row, response.fetchall())) + @staticmethod + def all_by_project(db: sqlite3.Connection) -> list[Project]: + objects = Object.all(db) + objects_by_projects = itertools.groupby(objects, lambda x: x.project) + # print(dict(objects_by_projects)) + return list(map(lambda x: Project(x[0], list(x[1])), objects_by_projects)) + def add_acquisition(self, calibration_id: int, db: sqlite3.Connection) -> Acquisition: cur = db.cursor() response = cur.execute( @@ -245,7 +261,7 @@ class Object: [self.id] ) acquisitions = list(map(lambda x: Acquisition.from_row(x), response.fetchall())) - return FullObject(self.id, self.name, acquisitions) + return FullObject(self.id, self.name, self.project, acquisitions) def main(): @@ -268,28 +284,6 @@ def main(): db.row_factory = sqlite3.Row init(db) - # Create a new object - # with db: - # 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.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() diff --git a/schema.sql b/schema.sql index ed7595b..84b52e6 100644 --- a/schema.sql +++ b/schema.sql @@ -20,5 +20,6 @@ CREATE TABLE acquisition ( CREATE TABLE object ( id INTEGER PRIMARY KEY AUTOINCREMENT, + project TEXT NOT NULL, name TEXT NOT NULL ); diff --git a/templates/index.html b/templates/index.html index 5f9aeb9..bd1909e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,23 +4,37 @@

Bienvenue sur NenuScanner

- {% if objects %} + {% if projects %}
-

Voici les objets existants dans la base de données : +

Voici les projets existants dans la base de données :

    - {% for object in objects %} -
  • {{ object.name }}
  • + {% for project in projects %} +
  • +

    {{ project.name }} ({{ project.objects | length }} objets)

    + + +
  • {% endfor %}
{% else %} -

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

+

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

{% endif %}