Basic support for projects
This commit is contained in:
parent
1c7f7e648a
commit
b7d53c381c
6
app.py
6
app.py
|
|
@ -49,15 +49,15 @@ def manage_auto_use_last_calibration():
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
conn = db.get()
|
conn = db.get()
|
||||||
objects = db.Object.all(conn)
|
projects = db.Object.all_by_project(conn)
|
||||||
return render_template('index.html', objects=objects)
|
return render_template('index.html', projects=projects)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/create-object/", methods=["POST"])
|
@app.route("/create-object/", methods=["POST"])
|
||||||
def create_object():
|
def create_object():
|
||||||
conn = db.get()
|
conn = db.get()
|
||||||
with conn:
|
with conn:
|
||||||
db.Object.create(request.form.get('name'), conn)
|
db.Object.create(request.form.get('name'), request.form.get('project'), conn)
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
56
db.py
56
db.py
|
|
@ -3,6 +3,7 @@
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from flask import g
|
from flask import g
|
||||||
|
import itertools
|
||||||
import os
|
import os
|
||||||
from os.path import join
|
from os.path import join
|
||||||
import shutil
|
import shutil
|
||||||
|
|
@ -173,26 +174,34 @@ class Acquisition:
|
||||||
|
|
||||||
|
|
||||||
class FullObject:
|
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.id = object_id
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.project = project
|
||||||
self.acquisitions = acquisitions
|
self.acquisitions = acquisitions
|
||||||
|
|
||||||
|
|
||||||
|
class Project:
|
||||||
|
def __init__(self, name: str, objects: list['Object']):
|
||||||
|
self.name = name
|
||||||
|
self.objects = objects
|
||||||
|
|
||||||
|
|
||||||
class Object:
|
class Object:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def select_args() -> str:
|
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.id = object_id
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.project = project
|
||||||
|
|
||||||
def save(self, db: sqlite3.Connection):
|
def save(self, db: sqlite3.Connection):
|
||||||
cur = db.cursor()
|
cur = db.cursor()
|
||||||
cur.execute(
|
cur.execute(
|
||||||
'UPDATE object SET name = ? WHERE id = ?',
|
'UPDATE object SET name = ?, project = ? WHERE id = ?',
|
||||||
[self.name, self.id]
|
[self.name, self.project, self.id]
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -202,11 +211,11 @@ class Object:
|
||||||
return Object(*row)
|
return Object(*row)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(name: str, db: sqlite3.Connection) -> 'Object':
|
def create(name: str, project: str, db: sqlite3.Connection) -> 'Object':
|
||||||
cur = db.cursor()
|
cur = db.cursor()
|
||||||
response = cur.execute(
|
response = cur.execute(
|
||||||
'INSERT INTO object(name) VALUES (?) RETURNING ' + Object.select_args() + ';',
|
'INSERT INTO object(name, project) VALUES (?, ?) RETURNING ' + Object.select_args() + ';',
|
||||||
[name]
|
[name, project]
|
||||||
)
|
)
|
||||||
object = Object.from_row(response.fetchone())
|
object = Object.from_row(response.fetchone())
|
||||||
os.makedirs(join(config.OBJECT_DIR, str(object.id)))
|
os.makedirs(join(config.OBJECT_DIR, str(object.id)))
|
||||||
|
|
@ -230,6 +239,13 @@ class Object:
|
||||||
)
|
)
|
||||||
return list(map(Object.from_row, response.fetchall()))
|
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:
|
def add_acquisition(self, calibration_id: int, db: sqlite3.Connection) -> Acquisition:
|
||||||
cur = db.cursor()
|
cur = db.cursor()
|
||||||
response = cur.execute(
|
response = cur.execute(
|
||||||
|
|
@ -245,7 +261,7 @@ class Object:
|
||||||
[self.id]
|
[self.id]
|
||||||
)
|
)
|
||||||
acquisitions = list(map(lambda x: Acquisition.from_row(x), response.fetchall()))
|
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():
|
def main():
|
||||||
|
|
@ -268,28 +284,6 @@ def main():
|
||||||
db.row_factory = sqlite3.Row
|
db.row_factory = sqlite3.Row
|
||||||
init(db)
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
|
|
@ -20,5 +20,6 @@ CREATE TABLE acquisition (
|
||||||
|
|
||||||
CREATE TABLE object (
|
CREATE TABLE object (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
project TEXT NOT NULL,
|
||||||
name TEXT NOT NULL
|
name TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -4,23 +4,37 @@
|
||||||
<section class="section">
|
<section class="section">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1 class="title">Bienvenue sur NenuScanner</h1>
|
<h1 class="title">Bienvenue sur NenuScanner</h1>
|
||||||
{% if objects %}
|
{% if projects %}
|
||||||
<div class="content">
|
<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>
|
<ul>
|
||||||
{% for object in objects %}
|
{% for project in projects %}
|
||||||
<li><a href="/object/{{ object.id }}">{{ object.name }}</a></li>
|
<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 %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>Il n'y a aucun objet pour le moment...</p>
|
<p>Il n'y a aucun projet pour le moment...</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<button id="add-object" class="button is-link">Ajouter un nouvel objet</button>
|
<button id="add-object" class="button is-link">Ajouter un nouvel objet</button>
|
||||||
<div id="add-object-modal" class="modal">
|
<div id="add-object-modal" class="modal">
|
||||||
<div class="modal-background"></div>
|
<div class="modal-background"></div>
|
||||||
<form action="/create-object/" method="POST">
|
<form action="/create-object/" method="POST">
|
||||||
<div class="modal-content">
|
<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">
|
<div class="field">
|
||||||
<label class="label">Nom de l'objet</label>
|
<label class="label">Nom de l'objet</label>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue