auto save

This commit is contained in:
2026-11-07 16:54:40 +01:00
parent 0a9da4f9c5
commit df06a4129f
3 changed files with 233 additions and 19 deletions
+36 -3
View File
@@ -577,7 +577,7 @@ def create_app():
def notes_create():
title = request.form.get("title", "").strip()
content = request.form.get("content", "").strip()
if not title or not content:
if not title:
return redirect(url_for("notes_page"))
note = Note(
user_id=current_user.id,
@@ -588,13 +588,40 @@ def create_app():
user_db.session.commit()
return _note_redirect(note.id)
@app.route("/notes/autosave", methods=["POST"])
@login_required
def notes_autosave():
data = request.get_json(silent=True)
if not data:
return jsonify({"error": "json required"}), 400
note_id = data.get("id")
title = (data.get("title") or "").strip()[:200]
content = (data.get("content") or "").strip()[:2000]
if not title:
return jsonify({"error": "title required"}), 400
if note_id:
note = _note_or_404(int(note_id))
note.title = title
note.content = content
note.touch()
user_db.session.commit()
return jsonify({"id": note.id, "status": "saved"})
note = Note(
user_id=current_user.id,
title=title,
content=content,
)
user_db.session.add(note)
user_db.session.commit()
return jsonify({"id": note.id, "status": "created"})
@app.route("/notes/<int:note_id>", methods=["POST"])
@login_required
def notes_update(note_id):
note = _note_or_404(note_id)
title = request.form.get("title", "").strip()
content = request.form.get("content", "").strip()
if title and content:
if title:
note.title = title[:200]
note.content = content[:2000]
note.touch()
@@ -628,11 +655,17 @@ def create_app():
def notes_update_item(note_id, item_id):
note = _note_or_404(note_id)
item = NoteItem.query.filter_by(id=item_id, note_id=note.id).first_or_404()
text = request.form.get("text", "").strip()
if request.is_json:
data = request.get_json(silent=True) or {}
text = (data.get("text") or "").strip()
else:
text = request.form.get("text", "").strip()
if text:
item.text = text[:255]
note.touch()
user_db.session.commit()
if request.is_json:
return jsonify({"id": item.id, "status": "saved"})
return _note_redirect(note.id)
@app.route(