def register(): if request.method == "POST": username = request.form["username"] password = request.form["password"] db = get_db() error = None if not username: error = "Username is required." elif not password: error = "Password is required." elif (db.execute("SELECT id FROM user WHERE username = ?", (username, )).fetchone() is not None): error = f"User {username} is already registered." if error is None: db.execute( "INSERT INTO user (username, password) VALUES (?, ?)", (username, generate_password_hash(password)), ) db.commit() return redirect(url_for("auth.login")) flash(error) return render_template("auth/register.html")
def home(): db = get_db() ledger = db.execute( "SELECT id, user_id, date, detail, income, expenses, source, tag, note, order_num" " FROM ledger WHERE user_id = ? ORDER BY order_num DESC", [g.user["id"]]).fetchall() return render_template("ledger/index.html", ledger=ledger)
def load_logged_in_user(): user_id = session.get("user_id") if user_id is None: g.user = None else: g.user = (get_db().execute("SELECT * FROM user WHERE id = ?", (user_id, )).fetchone())
def update_order(id): order = request.form["order"] error = None if not order: error = "Order number is required." if error is not None: return error else: db = get_db() db.execute("UPDATE ledger SET order_num = ? WHERE id = ?", (order, id)) db.commit() return "ok"
def update(id): date = request.form["date"] detail = request.form["detail"] income = request.form["income"] expenses = request.form["expenses"] source = request.form["source"] tag = request.form["tag"] note = request.form["note"] db = get_db() db.execute( "UPDATE ledger SET date = ?, detail = ?, income = ?, expenses = ?, source = ?, tag = ?, note = ?" " WHERE id = ?", (date, detail, income, expenses, source, tag, note, id)) db.commit() return "ok"
def chart(): db = get_db() ledger = db.execute( "SELECT id, user_id, date, income, expenses, tag, order_num" " FROM ledger WHERE user_id = ? ORDER BY order_num DESC", [g.user["id"]]).fetchall() data = {} for row in ledger: date = row["date"].strftime("%Y.%m.%d") if date not in data: data[date] = {"income": 0, "expenses": 0} data[date]["income"] += row["income"] data[date]["expenses"] += row["expenses"] return render_template("ledger/chart.html", data=json.dumps(data))
def insert(): date = request.form["date"] detail = request.form["detail"] income = request.form["income"] expenses = request.form["expenses"] source = request.form["source"] tag = request.form["tag"] note = request.form["note"] db = get_db() row_num = db.execute("SELECT count(*) FROM ledger WHERE user_id = ?", [g.user["id"]]).fetchone() db.execute( "INSERT INTO ledger (user_id, date, detail, income, expenses, source, tag, note, order_num)" " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", (g.user["id"], date, detail, int(income), int(expenses), source, tag, note, int(row_num[0]) + 1), ) db.commit() return "ok"
def login(): isLogout = request.args.get("isLogout", False) if request.method == "POST": username = request.form["username"] password = request.form["password"] db = get_db() error = None user = db.execute("SELECT * FROM user WHERE username = ?", (username, )).fetchone() if user is None: error = "Incorrect username." elif not check_password_hash(user["password"], password): error = "Incorrect password." if error is None: session.clear() session["user_id"] = user["id"] return redirect(url_for("ledger.home")) flash(error) return render_template("auth/login.html", isLogout=isLogout)
def delete(id): db = get_db() db.execute("DELETE FROM ledger WHERE id = ?", (id, )) db.commit() return "ok"