db db db init

db
km0 2 years ago
parent 898436252a
commit ed08dee35c

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

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

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

Loading…
Cancel
Save