from flask import Flask, render_template, request, redirect, url_for from jsondb import JSONDB from utils import get_now_datetime app = Flask(__name__) db = JSONDB("db.json") db.ensure("lists", "object") @app.route("/") def index(): lists = db.get("lists") return render_template("index.html", lists=lists.keys()) @app.route("/lists/add", methods=["GET", "POST"]) def add_list(): if request.method == "GET": return render_template("add_list.html") else: name = request.form.get("name") if not name: return "Nosaukums nav definēts!" lists = db.get("lists") if name in lists.keys(): return "Šāds piezīmju saraksts jau eksistē!" lists.add(name, []) return redirect(url_for("index")) @app.route("/lists//delete") def delete_list(name): lists = db.get("lists") if name not in lists.keys(): return "Šads piezīmju saraksts neeksistē!" lists.remove(name) return redirect(url_for("index")) @app.route("/lists/") def edit_list(name): lists = db.get("lists") if name not in lists.keys(): return "Šads piezīmju saraksts neeksistē!" note_list = lists.get(name) # Get all tags tags = [] for note in note_list._array: tags += note["tags"] tags = list(set(tags)) # Get search and tag filter search = request.args.get('s') tag = request.args.get('t') if search: notes = note_list.get_by_occurence_in_string(["name", "content"], search) elif tag: notes = note_list.get_by_occurence_in_array("tags", tag) else: notes = note_list.all() return render_template("edit_list.html", note_list=notes, note_list_name=name, s=search, t=tag, tags=tags) @app.route("/lists//add", methods=["GET", "POST"]) @app.route("/lists//notes//edit", methods=["GET", "POST"]) def add_note(list_name, note_name=None): edit = not note_name == None lists = db.get("lists") if list_name not in lists.keys(): return "Šads piezīmju saraksts neeksistē!" note_list = lists.get(list_name) note = {} if edit: note_id, note = note_list.get_by_key("name", note_name)[0] if request.method == "GET": return render_template("add_note.html", list_name=list_name, note=note, edit=edit) else: name = request.form.get("name") tags_str = request.form.get("tags") content = request.form.get("content") if not name or not content: return "Piezīmei nav satura vai nosaukuma!" if not edit: same_name_notes = note_list.get_by_key("name", name) if len(same_name_notes) > 0: return "Piezīme ar šādu nosaukumu jau eksistē!" tags = [] if not tags_str else tags_str.split("|") new_note = { "name": name, "datetime": get_now_datetime(), "tags": tags, "content": content } if edit: note_list.change_by_index(note_id, new_note) else: note_list.add(new_note) return redirect(url_for("edit_list", name=list_name)) @app.route("/list//notes//delete") def delete_note(list_name, note_name): lists = db.get("lists") if list_name not in lists.keys(): return "Šads piezīmju saraksts neeksistē!" note_list = lists.get(list_name) notes = note_list.get_by_key("name", note_name) if len(notes) == 0: return "Piezīme ar šādu nosaukumu neeksistē!" note_index = notes[0][0] note_list.remove_by_index(note_index) return redirect(url_for("edit_list", name=list_name))