Working on DB
This commit is contained in:
parent
8820523f01
commit
34c4e9b370
|
|
@ -1,3 +1,4 @@
|
|||
db.sqlite
|
||||
data
|
||||
node_modules
|
||||
static/calibration-visualiser.*
|
||||
|
|
|
|||
5
app.py
5
app.py
|
|
@ -1,13 +1,16 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from flask import Flask, render_template, send_from_directory
|
||||
from . import db
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def hello_world():
|
||||
return render_template('index.html')
|
||||
conn = db.get()
|
||||
objects = db.Object.all(conn)
|
||||
return render_template('index.html', objects=objects)
|
||||
|
||||
|
||||
@app.route("/calibration")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,83 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from flask import current_app, g
|
||||
import os
|
||||
import sqlite3
|
||||
import typing
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def get() -> sqlite3.Connection:
|
||||
if 'db' not in g:
|
||||
g.db = sqlite3.connect(
|
||||
os.environ.get('DATABASE_PATH', 'db.sqlite'),
|
||||
detect_types=sqlite3.PARSE_DECLTYPES,
|
||||
)
|
||||
g.db.row_factory = sqlite3.Row
|
||||
|
||||
return g.db
|
||||
|
||||
|
||||
def init(db):
|
||||
with open('schema.sql', 'r') as f:
|
||||
db.executescript(f.read())
|
||||
|
||||
|
||||
def close():
|
||||
db = g.pop('db', None)
|
||||
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
|
||||
def init_app(app):
|
||||
app.teardown_appcontext(close)
|
||||
app.cli.add_command(init)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
db = sqlite3.connect(
|
||||
os.environ.get('DATABASE_PATH', 'db.sqlite'),
|
||||
detect_types=sqlite3.PARSE_DECLTYPES,
|
||||
)
|
||||
db.row_factory = sqlite3.Row
|
||||
init(db)
|
||||
|
||||
|
||||
class Object:
|
||||
@staticmethod
|
||||
def select_args() -> str:
|
||||
return 'id, name'
|
||||
|
||||
def __init__(self, object_id: int, name: str):
|
||||
self.id = object_id
|
||||
self.name = name
|
||||
|
||||
def save(self, db: sqlite3.Connection):
|
||||
cur = db.cursor()
|
||||
cur.execute(
|
||||
'UPDATE object SET name = ? WHERE id = ?',
|
||||
[self.name, self.id]
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def from_row(row: sqlite3.Row) -> 'Object':
|
||||
return Object(*row)
|
||||
|
||||
@staticmethod
|
||||
def get_from_id(object_id: int, db: sqlite3.Connection) -> Optional['Object']:
|
||||
cur = db.cursor()
|
||||
response = cur.execute(
|
||||
'SELECT ' + Object.select_args() + ' FROM object WHERE id = ?;',
|
||||
[object_id]
|
||||
)
|
||||
return Object.from_row(response.fetchone())
|
||||
|
||||
@staticmethod
|
||||
def all(db: sqlite3.Connection) -> list['Object']:
|
||||
cur = db.cursor()
|
||||
response = cur.execute(
|
||||
'SELECT ' + Object.select_args() + ' FROM object;',
|
||||
[]
|
||||
)
|
||||
return list(map(Object.from_row, response.fetchall()))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
DROP TABLE IF EXISTS object;
|
||||
|
||||
CREATE TABLE object (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
|
|
@ -4,9 +4,15 @@
|
|||
<section class="section">
|
||||
<div class="container">
|
||||
<h1 class="title">Bienvenue sur NenuScanner</h1>
|
||||
<p>
|
||||
<a href="/calibration">Cliquez ici pour visualiser l'étalonnage!</a>
|
||||
</p>
|
||||
{% if objects %}
|
||||
<ul>
|
||||
{% for object in objects %}
|
||||
<li>{{ object.name }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% else %}
|
||||
<p>Il n'y a aucun object pour le moment...</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</section>
|
||||
{% endblock content %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue