Settings page mostly complete.
This commit is contained in:
parent
e84228161a
commit
ae54277e58
9 changed files with 406 additions and 174 deletions
|
|
@ -9,6 +9,8 @@ from crudkit.core import normalize_payload
|
|||
|
||||
bp_entry = Blueprint("entry", __name__)
|
||||
|
||||
ENTRY_WHITELIST = ["inventory", "user", "worklog", "room"]
|
||||
|
||||
def _fields_for_model(model: str):
|
||||
fields: list[str] = []
|
||||
fields_spec = []
|
||||
|
|
@ -158,7 +160,29 @@ def _fields_for_model(model: str):
|
|||
{"name": "ownership", "order": 10, "attrs": {"class": "row mb-2"}},
|
||||
{"name": "timestamps", "order": 20, "attrs": {"class": "row d-flex align-items-center"}},
|
||||
{"name": "updates", "order": 30, "attrs": {"class": "row"}},
|
||||
{"name": "buttons"},
|
||||
]
|
||||
elif model == "room":
|
||||
fields = [
|
||||
"label",
|
||||
"name"
|
||||
]
|
||||
fields_spec = [
|
||||
{"name": "label", "label": "", "type": "display", "attrs": {"class": "display-6 mb-3"},
|
||||
"row": "label", "wrap": {"class": "col"}},
|
||||
{"name": "buttons", "label": "", "row": "label", "type": "template", "template": "entry_buttons.html",
|
||||
"wrap": {"class": "col-auto text-end me-2"}, "attrs": {"data-model": model}},
|
||||
{"name": "name", "label": "Name", "row": "name", "attrs": {"class": "form-control"},
|
||||
"label_attrs": {"class": "form-label"}, "wrap": {"class": "col mb-3"}},
|
||||
{"name": "area", "label": "Area", "row": "details", "attrs": {"class": "form-control"},
|
||||
"label_attrs": {"class": "form-label"}, "wrap": {"class": "col"}, "label_spec": "{name}"},
|
||||
{"name": "room_function", "label": "Description", "label_spec": "{description}",
|
||||
"attrs": {"class": "form-control"}, "label_attrs": {"class": "form-label"}, "row": "details",
|
||||
"wrap": {"class": "col"}},
|
||||
]
|
||||
layout = [
|
||||
{"name": "label", "order": 0, "attrs": {"class": "row align-items-center"}},
|
||||
{"name": "name", "order": 10, "attrs": {"class": "row"}},
|
||||
{"name": "details", "order": 20, "attrs": {"class": "row"}},
|
||||
]
|
||||
|
||||
return (fields, fields_spec, layout)
|
||||
|
|
@ -192,7 +216,7 @@ def init_entry_routes(app):
|
|||
@bp_entry.get("/entry/<model>/<int:id>")
|
||||
def entry(model: str, id: int):
|
||||
cls = crudkit.crud.get_model(model)
|
||||
if cls is None or model not in ["inventory", "worklog", "user"]:
|
||||
if cls is None or model not in ENTRY_WHITELIST:
|
||||
abort(404)
|
||||
|
||||
fields, fields_spec, layout = _fields_for_model(model)
|
||||
|
|
@ -236,7 +260,7 @@ def init_entry_routes(app):
|
|||
@bp_entry.get("/entry/<model>/new")
|
||||
def entry_new(model: str):
|
||||
cls = crudkit.crud.get_model(model)
|
||||
if cls is None or model not in ["inventory", "worklog", "user"]:
|
||||
if cls is None or model not in ENTRY_WHITELIST:
|
||||
abort(404)
|
||||
|
||||
fields, fields_spec, layout = _fields_for_model(model)
|
||||
|
|
@ -276,7 +300,7 @@ def init_entry_routes(app):
|
|||
@bp_entry.post("/entry/<model>")
|
||||
def create_entry(model: str):
|
||||
try:
|
||||
if model not in ["inventory", "user", "worklog"]:
|
||||
if model not in ENTRY_WHITELIST:
|
||||
raise TypeError("Invalid model.")
|
||||
cls = crudkit.crud.get_model(model)
|
||||
svc = crudkit.crud.get_service(cls)
|
||||
|
|
@ -328,7 +352,7 @@ def init_entry_routes(app):
|
|||
@bp_entry.post("/entry/<model>/<int:id>")
|
||||
def update_entry(model, id):
|
||||
try:
|
||||
if model not in ["inventory", "user", "worklog"]:
|
||||
if model not in ENTRY_WHITELIST:
|
||||
raise TypeError("Invalid model.")
|
||||
cls = crudkit.crud.get_model(model)
|
||||
payload = normalize_payload(request.get_json(), cls)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from flask import Blueprint, render_template
|
|||
|
||||
import crudkit
|
||||
|
||||
from crudkit.ui.fragments import render_form
|
||||
from crudkit.ui.fragments import render_table
|
||||
|
||||
bp_settings = Blueprint("settings", __name__)
|
||||
|
||||
|
|
@ -13,10 +13,35 @@ def init_settings_routes(app):
|
|||
brand_service = crudkit.crud.get_service(brand_model)
|
||||
device_type_model = crudkit.crud.get_model('devicetype')
|
||||
device_type_service = crudkit.crud.get_service(device_type_model)
|
||||
area_model = crudkit.crud.get_model('area')
|
||||
area_service = crudkit.crud.get_service(area_model)
|
||||
function_model = crudkit.crud.get_model('roomfunction')
|
||||
function_service = crudkit.crud.get_service(function_model)
|
||||
room_model = crudkit.crud.get_model('room')
|
||||
room_service = crudkit.crud.get_service(room_model)
|
||||
|
||||
brands = brand_service.list({"sort": "name", "limit": 0})
|
||||
device_types = device_type_service.list({"sort": "description", "limit": 0})
|
||||
areas = area_service.list({"sort": "name", "limit": 0})
|
||||
functions = function_service.list({"sort": "description", "limit": 0})
|
||||
rooms = room_service.list({
|
||||
"sort": "name",
|
||||
"limit": 0,
|
||||
"fields": [
|
||||
"name",
|
||||
"area.name",
|
||||
"room_function.description"
|
||||
]
|
||||
})
|
||||
|
||||
return render_template("settings.html", brands=brands, device_types=device_types)
|
||||
rooms = render_table(rooms,
|
||||
[
|
||||
{"field": "name"},
|
||||
{"field": "area.name", "label": "Area"},
|
||||
{"field": "room_function.description", "label": "Description"},
|
||||
],
|
||||
opts={"object_class": 'room'})
|
||||
|
||||
return render_template("settings.html", brands=brands, device_types=device_types, areas=areas, functions=functions, rooms=rooms)
|
||||
|
||||
app.register_blueprint(bp_settings)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue