.
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
{% load crispy_forms_tags %}
|
{% load crispy_forms_tags %}
|
||||||
<form method="post" hx-post="{{ form_action }}" hx-target="#constraint-row-{{ initial.key|default:"new" }}" hx-swap="outerHTML">
|
<form method="post" hx-post="{{ form_action }}" hx-target="#constraint-row-{{ initial.key|default:"new" }}" hx-swap="outerHTML">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
{% if error %}
|
||||||
|
<div class="notification is-danger">{{ error }}</div>
|
||||||
|
{% endif %}
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Key</label>
|
<label class="label">Key</label>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
|
|||||||
@@ -506,7 +506,18 @@ def rota_option_inline(request, rota_id):
|
|||||||
val = request.POST.get("value", "")
|
val = request.POST.get("value", "")
|
||||||
vtype = request.POST.get("type", "string")
|
vtype = request.POST.get("type", "string")
|
||||||
if not key:
|
if not key:
|
||||||
return HttpResponseBadRequest("Key is required")
|
# Re-render the inline form with an error message
|
||||||
|
initial = {"key": key, "value": val, "type": vtype}
|
||||||
|
inline_html = render_to_string(
|
||||||
|
"rota/partials/rota_option_inline.html",
|
||||||
|
{"form_action": reverse("rota:rota_option_inline", args=(rota.id,)), "initial": initial, "error": "Key is required", "rota": rota},
|
||||||
|
request=request,
|
||||||
|
)
|
||||||
|
return HttpResponse(inline_html)
|
||||||
|
|
||||||
|
# Try to parse according to type; on failure re-render form with error
|
||||||
|
parsed = None
|
||||||
|
parse_error = None
|
||||||
try:
|
try:
|
||||||
if vtype == "int":
|
if vtype == "int":
|
||||||
parsed = int(val)
|
parsed = int(val)
|
||||||
@@ -519,7 +530,16 @@ def rota_option_inline(request, rota_id):
|
|||||||
else:
|
else:
|
||||||
parsed = val
|
parsed = val
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return HttpResponseBadRequest(f"Invalid value: {exc}")
|
parse_error = str(exc)
|
||||||
|
|
||||||
|
if parse_error:
|
||||||
|
initial = {"key": key, "value": val, "type": vtype}
|
||||||
|
inline_html = render_to_string(
|
||||||
|
"rota/partials/rota_option_inline.html",
|
||||||
|
{"form_action": reverse("rota:rota_option_inline", args=(rota.id,)), "initial": initial, "error": f"Invalid value: {parse_error}", "rota": rota},
|
||||||
|
request=request,
|
||||||
|
)
|
||||||
|
return HttpResponse(inline_html)
|
||||||
|
|
||||||
opts = rota.options or {}
|
opts = rota.options or {}
|
||||||
opts[key] = parsed
|
opts[key] = parsed
|
||||||
|
|||||||
Reference in New Issue
Block a user