Hooray! Updates to updates!
This commit is contained in:
parent
85b0e576c7
commit
bcf14cf251
3 changed files with 106 additions and 33 deletions
|
|
@ -9,6 +9,28 @@ from crudkit.core import normalize_payload
|
|||
|
||||
bp_entry = Blueprint("entry", __name__)
|
||||
|
||||
def _apply_worklog_updates(worklog, updates, delete_ids):
|
||||
note_cls = type(worklog).updates.property.mapper.class_
|
||||
note_svc = crudkit.crud.get_service(note_cls)
|
||||
sess = note_svc.session
|
||||
|
||||
existing = {u.id: u for u in worklog.updates if not getattr(u, "is_deleted", False)}
|
||||
|
||||
for item in updates:
|
||||
uid = item.get("id")
|
||||
content = (item.get("content") or "").strip()
|
||||
if not content and not uid:
|
||||
continue
|
||||
if uid:
|
||||
# per-note version log preserved, but commit deferred.
|
||||
note_svc.update(uid, {"content": content}, actor="bulk_child_update", commit=False)
|
||||
else:
|
||||
note_svc.create({"work_log_id": worklog.id, "content": content}, actor="bulk_child_create", commit=False)
|
||||
|
||||
for uid in delete_ids:
|
||||
if uid in existing:
|
||||
note_svc.delete(uid, actor="bulk_child_delete", commit=False)
|
||||
|
||||
def init_entry_routes(app):
|
||||
|
||||
@bp_entry.get("/entry/<model>/<int:id>")
|
||||
|
|
@ -168,6 +190,9 @@ def init_entry_routes(app):
|
|||
cls = crudkit.crud.get_model(model)
|
||||
payload = normalize_payload(request.get_json(), cls)
|
||||
|
||||
updates = payload.pop("updates", None) or []
|
||||
delete_ids = payload.pop("delete_update_ids", None) or []
|
||||
|
||||
params = {}
|
||||
if model == "inventory":
|
||||
pass
|
||||
|
|
@ -197,13 +222,19 @@ def init_entry_routes(app):
|
|||
else:
|
||||
raise TypeError("Invalid model.")
|
||||
|
||||
print(payload)
|
||||
|
||||
service = crudkit.crud.get_service(cls)
|
||||
service.update(id, data=payload, actor="update_entry")
|
||||
sess = service.session
|
||||
|
||||
obj = service.update(id, data=payload, actor="update_entry", commit=False)
|
||||
|
||||
if model == "worklog" and (updates or delete_ids):
|
||||
_apply_worklog_updates(obj, updates, delete_ids)
|
||||
|
||||
sess.commit()
|
||||
|
||||
return {"status": "success", "payload": payload}
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return {"status": "failure", "error": str(e)}
|
||||
|
||||
app.register_blueprint(bp_entry)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
const fd = new FormData(form);
|
||||
const out = {};
|
||||
|
||||
// base values
|
||||
fd.forEach((value, key) => {
|
||||
if (key in out) {
|
||||
if (!Array.isArray(out[key])) out[key] = [out[key]];
|
||||
|
|
@ -16,21 +17,21 @@
|
|||
}
|
||||
});
|
||||
|
||||
// normalize radios and checkboxes
|
||||
form.querySelectorAll('input[type="checkbox"], input[type="radio"]').forEach(el => {
|
||||
if (!el.name) return;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
if (out[el.name] !== undefined) return;
|
||||
if (out[el.name] !== undefined) return; // already set for this group
|
||||
const checked = form.querySelector(`input[type="radio"][name="${CSS.escape(el.name)}"]:checked`);
|
||||
if (checked) out[el.name] = checked.value ?? true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (el.type === 'checkbox') {
|
||||
const group = form.querySelectorAll(`input[type="checkbox"][name="${CSS.escape(el.name)}"]`);
|
||||
if (group.length > 1) {
|
||||
const checkedVals = Array.from(group)
|
||||
.filter(i => i.checked)
|
||||
.map(i => i.value ?? true);
|
||||
const checkedVals = Array.from(group).filter(i => i.checked).map(i => i.value ?? true);
|
||||
out[el.name] = checkedVals;
|
||||
} else {
|
||||
out[el.name] = el.checked;
|
||||
|
|
@ -41,24 +42,61 @@
|
|||
return out;
|
||||
}
|
||||
|
||||
function collectExistingUpdateIds() {
|
||||
return Array.from(document.querySelectorAll('script[type="application/json"][id^="md-"]'))
|
||||
.map(el => Number(el.id.slice(3)))
|
||||
.filter(Number.isFinite);
|
||||
}
|
||||
|
||||
function collectEditedUpdates() {
|
||||
const updates = [];
|
||||
for (const id of collectExistingUpdateIds()) {
|
||||
updates.push({ id, content: getMarkdown(id) }); // ensure getMarkdown exists
|
||||
}
|
||||
for (const md of (window.newDrafts || [])) {
|
||||
if ((md ?? '').trim()) updates.push({ content: md });
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
|
||||
function collectDeletedIds() {
|
||||
return (window.deletedIds || []).filter(Number.isFinite);
|
||||
}
|
||||
|
||||
document.getElementById("{{ field['attrs']['data-model'] }}_form").addEventListener("submit", async e => {
|
||||
e.preventDefault();
|
||||
|
||||
const json = formToJson(e.target);
|
||||
json['id'] = {{ field['template_ctx']['values']['id'] }};
|
||||
json.id = {{ field['template_ctx']['values']['id'] }};
|
||||
|
||||
response = await fetch("{{ url_for('entry.update_entry', id=field['template_ctx']['values']['id'], model=field['attrs']['data-model']) }}", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(json)
|
||||
});
|
||||
// map friendly names to real FK columns if needed
|
||||
if (json.contact && !json.contact_id) json.contact_id = Number(json.contact) || null;
|
||||
if (json.work_item && !json.work_item_id) json.work_item_id = Number(json.work_item) || null;
|
||||
|
||||
reply = await response.json();
|
||||
if (reply['status'] === 'success') {
|
||||
toastMessage("This entry has been successfully saved!", "success");
|
||||
} else {
|
||||
toastMessage(`Unable to save entry: ${reply['error']}`, "danger");
|
||||
// child mutations
|
||||
json.updates = collectEditedUpdates();
|
||||
json.delete_update_ids = collectDeletedIds();
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
"{{ url_for('entry.update_entry', id=field['template_ctx']['values']['id'], model=field['attrs']['data-model']) }}",
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(json),
|
||||
}
|
||||
);
|
||||
|
||||
const reply = await response.json();
|
||||
if (reply.status === 'success') {
|
||||
toastMessage('This entry has been successfully saved!', 'success');
|
||||
window.newDrafts = [];
|
||||
window.deletedIds = [];
|
||||
} else {
|
||||
toastMessage(`Unable to save entry: ${reply.error}`, 'danger');
|
||||
}
|
||||
} catch (err) {
|
||||
toastMessage(`Network error: ${String(err)}`, 'danger');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue