Voici les objects existants dans la base de données : +
-
+ {% for object in objects %}
+
- {{ object.name }} + {% endfor %} +
diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app.py b/app.py index 7df7358..661e601 100755 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from flask import Flask, render_template, send_from_directory +from flask import Flask, redirect, request, render_template, send_from_directory from . import db app = Flask(__name__) @@ -13,6 +13,14 @@ def hello_world(): return render_template('index.html', objects=objects) +@app.route("/create-object/", methods=["POST"]) +def create_object(): + conn = db.get() + with conn: + db.Object.create(request.form.get('name'), conn) + return redirect('/') + + @app.route("/calibration") def calibration(): return render_template('calibration.html') diff --git a/db.py b/db.py index 1cebd6b..a1091bb 100755 --- a/db.py +++ b/db.py @@ -1,9 +1,8 @@ #!/usr/bin/env python -from flask import current_app, g +from flask import g import os import sqlite3 -import typing from typing import Optional @@ -53,6 +52,15 @@ class Object: self.id = object_id self.name = name + @staticmethod + def create(name: str, db: sqlite3.Connection) -> 'Object': + cur = db.cursor() + response = cur.execute( + 'INSERT INTO object(name) VALUES (?) RETURNING ' + Object.select_args() + ';', + [name] + ) + return response.fetchone() + def save(self, db: sqlite3.Connection): cur = db.cursor() cur.execute( diff --git a/templates/index.html b/templates/index.html index c14eb81..9bb9f2a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,14 +5,58 @@
Voici les objects existants dans la base de données : +
Il n'y a aucun object pour le moment...
{% endif %} + +