Basic support for projects

This commit is contained in:
Thomas Forgione 2024-08-21 16:08:32 +02:00
parent 1c7f7e648a
commit b7d53c381c
4 changed files with 48 additions and 39 deletions

6
app.py
View File

@ -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('/')

56
db.py
View File

@ -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()

View File

@ -20,5 +20,6 @@ CREATE TABLE acquisition (
CREATE TABLE object (
id INTEGER PRIMARY KEY AUTOINCREMENT,
project TEXT NOT NULL,
name TEXT NOT NULL
);

View File

@ -4,23 +4,37 @@
<section class="section">
<div class="container">
<h1 class="title">Bienvenue sur NenuScanner</h1>
{% if objects %}
{% if projects %}
<div class="content">
<p>Voici les objets existants dans la base de données :
<p>Voici les projets existants dans la base de données :
<ul>
{% for object in objects %}
<li><a href="/object/{{ object.id }}">{{ object.name }}</a></li>
{% for project in projects %}
<li>
<h2 class="title mb-0 mt-3">{{ project.name }} <em>({{ project.objects | length }} objets)</em></h2>
<ul>
{% for object in project.objects %}
<li><a href="/object/{{ object.id }}">{{ object.name }}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
{% else %}
<p>Il n'y a aucun objet pour le moment...</p>
<p>Il n'y a aucun projet pour le moment...</p>
{% endif %}
<button id="add-object" class="button is-link">Ajouter un nouvel objet</button>
<div id="add-object-modal" class="modal">
<div class="modal-background"></div>
<form action="/create-object/" method="POST">
<div class="modal-content">
<div class="field">
<label class="label">Nom du projet</label>
<div class="control">
<input class="input" type="text" name="project" placeholder="Nom du projet" required>
</div>
</div>
<div class="field">
<label class="label">Nom de l'objet</label>
<div class="control">