Get printer editing working end to end

This commit is contained in:
Reid 'arrdem' McKenzie 2023-07-08 13:45:31 -06:00
parent 0fa65cfbe2
commit c1375cb179
3 changed files with 47 additions and 16 deletions

View file

@ -108,6 +108,7 @@ def handle_add_printer():
def get_edit_printers():
pid = int(request.args.get("id", "-1"))
if row := ctx.db.fetch_printer(pid=pid):
print(row)
return render_template("edit_printer.html.j2", printer=row)
else:
flash("No such printer", category="error")
@ -119,7 +120,11 @@ def get_edit_printers():
def handle_edit_printers():
args = request.form.copy()
args["id"] = int(args["id"])
ctx.db.edit_printer(**request.form)
args["enabled"] = int(args["enabled"])
args["filament_id"] = int(args["filament_id"])
args["chassis_id"] = int(args["chassis_id"])
print(args)
ctx.db.edit_printer(**args)
return redirect("/admin")

View file

@ -65,9 +65,9 @@ CREATE TABLE IF NOT EXISTS filament (
, UNIQUE(name)
);
INSERT OR IGNORE INTO filament (name) VALUES ('pla');
INSERT OR IGNORE INTO filament (name) VALUES ('abs');
INSERT OR IGNORE INTO filament (name) VALUES ('petg');
INSERT OR IGNORE INTO filament (name) VALUES ('PLA');
INSERT OR IGNORE INTO filament (name) VALUES ('ABS');
INSERT OR IGNORE INTO filament (name) VALUES ('PETG');
ALTER TABLE printers ADD filament_id INTEGER REFERENCES filament(id) DEFAULT 1;
@ -98,6 +98,9 @@ SELECT
, p.stream_url
, p.api_key
, p.last_poll_date
, p.filament_id
, p.chassis_id
, p.enabled
, s.name as status
FROM printers p
INNER JOIN printer_statuses s ON p.status_id = s.id
@ -151,15 +154,30 @@ WHERE
id = :pid
;
-- name: edit-printer
-- name: edit-printer!
UPDATE printers
SET
name = :name
, url = :url
, stream_url = :stream_url
, api_key = :api_key
, filament_load = :filament_load
, enabled = (CASE WHEN LOWER(:enabled) = 'true' THEN 1 ELSE 0)
, filament_id = :filament_id
, chassis_id = :chassis_id
, enabled = :enabled
WHERE
id = :id
;
-- name: list-chassis
SELECT
id
, name
FROM printer_chassis
;
-- name: list-filament
SELECT
id
, name
FROM filament
;

View file

@ -28,19 +28,27 @@
</div>
</div>
<div class"row">
<div class="six columns">
<label for="filament_load">Filament load</label>
<select name="filament_load">
<option value="pla" {% if printer.filament_load == "pla"%}selected{%endif%}>PLA</option>
<option value="petg" {% if printer.filament_load == "petg" %}selected{%endif%}>PETG</option>
<option value="abs" {% if printer.filament_load == "abs" %}selected{%endif%}>ABS</option>
<div class="four columns">
<label for="chassis">Chassis</label>
<select name="chassis_id">
{% for c in ctx.db.list_chassis() %}
<option value="{{c.id}}" {% if printer.chassis_id == c.id %}selected{%endif%}>{{c.name}}</option>
{% endfor %}
</select>
</div>
<div class="six columns">
<div class="four columns">
<label for="filament">Filament load</label>
<select name="filament_id">
{% for f in ctx.db.list_filament() %}
<option value="{{f.id}}" {% if printer.filament_id == f.id %}selected{%endif%}>{{f.name}}</option>
{% endfor %}
</select>
</div>
<div class="four columns">
<label for="enabled">Printing enabled</label>
<select name="enabled">
<option value="true" {% if printer.enabled %}selected{%endif%}>Enabled</option>
<option value="false" {% if not printer.enabled %}selected{%endif%}>Disabled</option>
<option value="1" {% if printer.enabled %}selected{%endif%}>Enabled</option>
<option value="0" {% if not printer.enabled %}selected{%endif%}>Disabled</option>
</select>
</div>
</div>