From ed4a448016676937b944f1dd85e4e1fbfb9781f1 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 15 Jul 2024 16:03:39 +0200 Subject: [PATCH] Add calibration dates --- app.py | 3 +-- dateutils.py | 34 ++++++++++++++++++++++++++++++++++ db.py | 29 +++++++++++++++++++++-------- schema.sql | 3 ++- templates/calibration.html | 2 ++ 5 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 dateutils.py diff --git a/app.py b/app.py index 3bd4e45..84d4ed2 100755 --- a/app.py +++ b/app.py @@ -170,9 +170,8 @@ def validate_calibration(): if calib is None: return 'oops', 404 - calib.state = db.CalibrationState.IsValidated with conn: - calib.save(conn) + calib.validate(conn) return redirect('/') diff --git a/dateutils.py b/dateutils.py new file mode 100644 index 0000000..e84bef6 --- /dev/null +++ b/dateutils.py @@ -0,0 +1,34 @@ +from datetime import datetime + + +def format_month(month: int) -> str: + if month == 1: + return 'janvier' + elif month == 2: + return 'février' + elif month == 3: + return 'mars' + elif month == 4: + return 'avril' + elif month == 5: + return 'mai' + elif month == 6: + return 'juin' + elif month == 7: + return 'juillet' + elif month == 8: + return 'août' + elif month == 9: + return 'septembre' + elif month == 10: + return 'octobre' + elif month == 11: + return 'novembre' + elif month == 12: + return 'décembre' + else: + raise RuntimeError(f'No such month: {month}') + + +def format(date: datetime) -> str: + return f'{date.day} {format_month(date.month)} {date.year} à {date.hour:02}h{date.minute:02}' diff --git a/db.py b/db.py index 7c1ae9c..63d69cd 100755 --- a/db.py +++ b/db.py @@ -10,9 +10,10 @@ import sqlite3 from typing import Optional if __name__ != '__main__': - from . import config + from . import config, dateutils else: import config + import dateutils def get() -> sqlite3.Connection: @@ -55,17 +56,21 @@ class CalibrationState(IntEnum): class Calibration: @staticmethod def select_args() -> str: - return 'id, state' + return 'id, state, validated_date' - def __init__(self, calibration_id: int, state: int): + def __init__(self, calibration_id: int, state: int, validated_date: Optional[int]): self.id = calibration_id self.state = CalibrationState(state) + if validated_date is None: + self.validated_date = None + else: + self.validated_date = datetime.fromtimestamp(validated_date) @staticmethod def create(db: sqlite3.Connection) -> 'Calibration': cur = db.cursor() response = cur.execute( - 'INSERT INTO calibration(state) VALUES (?) RETURNING ' + Calibration.select_args() + ';', + 'INSERT INTO calibration(state, validated_date) VALUES (?, NULL) RETURNING ' + Calibration.select_args() + ';', [int(CalibrationState.Empty)] ) calibration = Calibration.from_row(response.fetchone()) @@ -83,8 +88,8 @@ class Calibration: def save(self, db: sqlite3.Connection): cur = db.cursor() cur.execute( - 'UPDATE calibration SET state = ? WHERE id = ?', - [int(self.state), self.id] + 'UPDATE calibration SET state = ?, validated_date = ? WHERE id = ?', + [int(self.state), int(self.validated_date.timestamp()) if self.validated_date is not None else None, self.id] ) @staticmethod @@ -100,13 +105,21 @@ class Calibration: def get_last(db: sqlite3.Connection) -> Optional['Calibration']: cur = db.cursor() response = cur.execute( - 'SELECT ' + Calibration.select_args() + ' FROM calibration WHERE state = 3 ORDER BY id DESC LIMIT 1;', + 'SELECT ' + Calibration.select_args() + ' FROM calibration WHERE state = 3 ORDER BY validated_date DESC LIMIT 1;', [] ) return Calibration.from_row(response.fetchone()) + def validate(self, db: sqlite3.Connection): + self.state = CalibrationState.IsValidated + self.validated_date = datetime.now() + self.save(db) -Calibration.Dummy = Calibration(-1, CalibrationState.Empty) + def get_pretty_date(self) -> str: + return dateutils.format(self.validated_date) + + +Calibration.Dummy = Calibration(-1, CalibrationState.Empty, None) class Acquisition: diff --git a/schema.sql b/schema.sql index 92cf3db..bbad611 100644 --- a/schema.sql +++ b/schema.sql @@ -4,7 +4,8 @@ DROP TABLE IF EXISTS object; CREATE TABLE calibration ( id INTEGER PRIMARY KEY AUTOINCREMENT, - state INTEGER NOT NULL + state INTEGER NOT NULL, + validated_date INTEGER ); CREATE TABLE acquisition ( diff --git a/templates/calibration.html b/templates/calibration.html index 0ec13da..3f8286a 100644 --- a/templates/calibration.html +++ b/templates/calibration.html @@ -4,6 +4,8 @@

Visualisation de l'étalonnage {{ calibration.id }}

+ {% if calibration.validated_date %} +

Validé le {{ calibration.get_pretty_date() }}

{% endif %}

Positions 3D des LEDs