notatki
This commit is contained in:
+96
-1
@@ -14,7 +14,7 @@ from flask_login import current_user, login_required, login_user, logout_user
|
||||
from auth import init_login_manager
|
||||
from config import load_secret_key, sensors_api_url, users_database_uri
|
||||
from sensors_client import SensorsAPIError, SensorsClient
|
||||
from user_models import User, user_db
|
||||
from user_models import Note, NoteItem, User, user_db
|
||||
|
||||
|
||||
def create_app():
|
||||
@@ -103,6 +103,101 @@ def create_app():
|
||||
def api_docs():
|
||||
return render_template("docs.html", user=current_user)
|
||||
|
||||
# --- Notes (stored in main_app.db) ---
|
||||
|
||||
def _note_or_404(note_id):
|
||||
return Note.query.filter_by(
|
||||
id=note_id, user_id=current_user.id
|
||||
).first_or_404()
|
||||
|
||||
def _note_redirect(note_id):
|
||||
return redirect(
|
||||
url_for("notes_page", _anchor=f"note-{note_id}")
|
||||
)
|
||||
|
||||
@app.route("/notes")
|
||||
@login_required
|
||||
def notes_page():
|
||||
notes = (
|
||||
Note.query.filter_by(user_id=current_user.id)
|
||||
.order_by(Note.updated_at.desc(), Note.id.desc())
|
||||
.all()
|
||||
)
|
||||
return render_template("notes.html", user=current_user, notes=notes)
|
||||
|
||||
@app.route("/notes", methods=["POST"])
|
||||
@login_required
|
||||
def notes_create():
|
||||
title = request.form.get("title", "").strip()
|
||||
content = request.form.get("content", "").strip()
|
||||
if not title or not content:
|
||||
return redirect(url_for("notes_page"))
|
||||
note = Note(
|
||||
user_id=current_user.id,
|
||||
title=title[:200],
|
||||
content=content[:2000],
|
||||
)
|
||||
user_db.session.add(note)
|
||||
user_db.session.commit()
|
||||
return _note_redirect(note.id)
|
||||
|
||||
@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:
|
||||
note.title = title[:200]
|
||||
note.content = content[:2000]
|
||||
note.touch()
|
||||
user_db.session.commit()
|
||||
return _note_redirect(note.id)
|
||||
|
||||
@app.route("/notes/<int:note_id>/delete", methods=["POST"])
|
||||
@login_required
|
||||
def notes_delete(note_id):
|
||||
note = _note_or_404(note_id)
|
||||
user_db.session.delete(note)
|
||||
user_db.session.commit()
|
||||
return redirect(url_for("notes_page"))
|
||||
|
||||
@app.route("/notes/<int:note_id>/items", methods=["POST"])
|
||||
@login_required
|
||||
def notes_add_item(note_id):
|
||||
note = _note_or_404(note_id)
|
||||
text = request.form.get("text", "").strip()
|
||||
if text:
|
||||
item = NoteItem(note=note, text=text[:255])
|
||||
note.touch()
|
||||
user_db.session.add(item)
|
||||
user_db.session.commit()
|
||||
return _note_redirect(note.id)
|
||||
|
||||
@app.route(
|
||||
"/notes/<int:note_id>/items/<int:item_id>/toggle", methods=["POST"]
|
||||
)
|
||||
@login_required
|
||||
def notes_toggle_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()
|
||||
item.is_done = request.form.get("is_done") in {"1", "true", "on", "yes"}
|
||||
note.touch()
|
||||
user_db.session.commit()
|
||||
return _note_redirect(note.id)
|
||||
|
||||
@app.route(
|
||||
"/notes/<int:note_id>/items/<int:item_id>/delete", methods=["POST"]
|
||||
)
|
||||
@login_required
|
||||
def notes_delete_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()
|
||||
user_db.session.delete(item)
|
||||
note.touch()
|
||||
user_db.session.commit()
|
||||
return _note_redirect(note.id)
|
||||
|
||||
# --- API: proxied to the sensors service ---
|
||||
# The browser only talks to this app; the sensors service owns the
|
||||
# sensor database and the actual API implementation.
|
||||
|
||||
Reference in New Issue
Block a user