From ed08dee35c49b463487a2445f3c72591caf7df3f Mon Sep 17 00:00:00 2001 From: Francesco Luzzana Date: Fri, 28 Oct 2022 11:31:03 +0200 Subject: [PATCH] db db db init --- db.py | 37 +++++++++++++++++++++++++++++++++++++ init_db.py | 10 +++------- workbook.py | 49 ++++--------------------------------------------- 3 files changed, 44 insertions(+), 52 deletions(-) create mode 100644 db.py diff --git a/db.py b/db.py new file mode 100644 index 0000000..0a9f732 --- /dev/null +++ b/db.py @@ -0,0 +1,37 @@ +import sqlite3 + +def get_db_connection(): + conn = sqlite3.connect('database.db') + conn.row_factory = sqlite3.Row + return conn + +def dict_from_row(row): + return dict(zip(row.keys(), row)) + +def create_instrument(name, slug, description, params, sockets): + connection = get_db_connection() + cur = connection.cursor() + cur.execute( + "INSERT INTO instruments (name, slug, description, panel) VALUES (?, ?, ?, ?)", + (name, slug, description, f'{slug}.svg') + ) + + for param in params: + cur.execute( + "INSERT OR IGNORE INTO params (param_name) VALUES (?)",(param,) + ) + cur.execute( + "INSERT INTO instrument_param (instrument, param_name) VALUES (?, ?)", (slug, param ) + ) + + for socket in sockets: + cur.execute( + "INSERT OR IGNORE INTO sockets (socket_name) VALUES (?)", (socket,) + ) + + cur.execute( + "INSERT INTO instrument_socket (instrument, socket_name) VALUES (?, ?)", (slug, socket ) + ) + + connection.commit() + connection.close() \ No newline at end of file diff --git a/init_db.py b/init_db.py index c26c73f..a41325c 100644 --- a/init_db.py +++ b/init_db.py @@ -1,16 +1,12 @@ -import sqlite3 +from db import create_instrument, get_db_connection -connection = sqlite3.connect('database.db') +connection = get_db_connection() with open('schema.sql') as f: connection.executescript(f.read()) -cur = connection.cursor() -cur.execute( - "INSERT INTO instruments (name, slug, description, panel) VALUES (?, ?, ?, ?)", - ('Test Instrument', 'test_instrument', 'The classic original the one we like to test', 'test_instrument.svg') - ) +create_instrument('Test Instrument', 'test_instrument', 'The classic original the one we like to test', ['eye','back'], ['feet','faat', 'foot', 'nose'] ) connection.commit() connection.close() \ No newline at end of file diff --git a/workbook.py b/workbook.py index a636a61..b107af9 100644 --- a/workbook.py +++ b/workbook.py @@ -1,7 +1,7 @@ from flask import Flask, render_template, request, redirect, url_for +from db import get_db_connection, dict_from_row, create_instrument from werkzeug.utils import secure_filename import json -import sqlite3 from dotenv import load_dotenv from pathlib import Path import os @@ -35,49 +35,6 @@ app = Flask(__name__) app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=os.environ.get('BASE_URL', '')) -def get_db_connection(): - conn = sqlite3.connect('database.db') - conn.row_factory = sqlite3.Row - return conn - -def dict_from_row(row): - return dict(zip(row.keys(), row)) - -def create_instrument(name, description, params, sockets, panel): - slug = secure_filename(name) - panel.save(f'static/panels/{slug}.svg') - - connection = get_db_connection() - cur = connection.cursor() - cur.execute( - "INSERT INTO instruments (name, slug, description, panel) VALUES (?, ?, ?, ?)", - (name, slug, description, f'{slug}.svg') - ) - - instrument_id = cur.lastrowid - - for param in params: - cur.execute( - "INSERT OR IGNORE INTO params (param_name) VALUES (?)",(param,) - ) - cur.execute( - "INSERT INTO instrument_param (instrument, param_name) VALUES (?, ?)", (slug, param ) - ) - - for socket in sockets: - cur.execute( - "INSERT OR IGNORE INTO sockets (socket_name) VALUES (?)", (socket,) - ) - - cur.execute( - "INSERT INTO instrument_socket (instrument, socket_name) VALUES (?, ?)", (slug, socket ) - ) - - connection.commit() - connection.close() - - - @app.route("/") def workbook(): return render_template('workbook.html') @@ -119,7 +76,9 @@ def add_instrument(): params = json.loads(request.form.get('params')) sockets = json.loads(request.form.get('sockets')) panel = request.files['panel'] - create_instrument(name, description, params, sockets, panel) + slug = secure_filename(name) + panel.save(f'static/panels/{slug}.svg') + create_instrument(name, slug, description, params, sockets) return redirect(url_for('instruments'))