From 70383ebb34ef9bbf2141ed4f69eb52eaf4494c3d Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Thu, 22 Aug 2024 16:20:14 +0200 Subject: [PATCH] Allow delete object --- app.py | 8 ++++++++ db.py | 28 +++++++++++++++++++--------- schema.sql | 2 +- templates/object.html | 1 + 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 9db12b5..3a76bea 100755 --- a/app.py +++ b/app.py @@ -68,6 +68,14 @@ def object(id: int): return render_template('object.html', object=object) +@app.route('/delete-object/') +def delete_object(id: int): + conn = db.get() + with conn: + db.Object.delete_from_id(id, conn) + return redirect('/') + + @app.route('/scan/') def scan(id: int): conn = db.get() diff --git a/db.py b/db.py index 5ecaa14..e29a497 100755 --- a/db.py +++ b/db.py @@ -182,14 +182,6 @@ class Acquisition: return Acquisition.from_row(response.fetchone()) -class FullObject: - 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 @@ -263,7 +255,16 @@ class Object: ) return Acquisition.from_row(response.fetchone()) - def full(self, db: sqlite3.Connection) -> FullObject: + @staticmethod + def delete_from_id(object_id: int, db: sqlite3.Connection) -> 'Object': + cur = db.cursor() + response = cur.execute( + 'DELETE FROM object WHERE id = ? RETURNING ' + Object.select_args() + ';', + [object_id] + ) + return Object.from_row(response.fetchone()) + + def full(self, db: sqlite3.Connection) -> 'FullObject': cur = db.cursor() response = cur.execute( 'SELECT ' + Acquisition.select_args() + ' FROM acquisition WHERE object_id = ? ORDER BY date DESC;', @@ -273,6 +274,15 @@ class Object: return FullObject(self.id, self.name, self.project, acquisitions) +class FullObject(Object): + def __init__(self, object_id: int, name: str, project: str, acquisitions: list[Acquisition]): + super().__init__(object_id, name, project) + self.id = object_id + self.name = name + self.project = project + self.acquisitions = acquisitions + + def main(): # Move current data to backup dir if os.path.isdir(config.DATA_DIR): diff --git a/schema.sql b/schema.sql index 84b52e6..6b84640 100644 --- a/schema.sql +++ b/schema.sql @@ -15,7 +15,7 @@ CREATE TABLE acquisition ( date INTEGER NOT NULL, validated INT NOT NULL, CONSTRAINT fk_calibration FOREIGN KEY(calibration_id) REFERENCES calibration(id), - CONSTRAINT fk_object FOREIGN KEY(object_id) REFERENCES object(id) + CONSTRAINT fk_object FOREIGN KEY(object_id) REFERENCES object(id) ON DELETE CASCADE ); CREATE TABLE object ( diff --git a/templates/object.html b/templates/object.html index 4c33601..540255d 100644 --- a/templates/object.html +++ b/templates/object.html @@ -48,6 +48,7 @@ {% endif %} + Supprimer cet objet