def view_payment(payment_id): db = get_db() payment = db.execute("SELECT * FROM payment WHERE user_id = ? AND id = ?", (g.user["id"], payment_id,)).fetchone() if not payment: flash("Payment not found") return redirect(url_for("payments.user_payments")) return render_template("payments/payment_details.html", payment=payment)
def load_logged_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 _add_user(app, username, password): with app.app_context(): db = get_db() cursor = db.cursor() cursor.execute( "INSERT INTO user (username, password) VALUES (?, ?)", (username, generate_password_hash(password))) db.commit() return cursor.lastrowid
def _add_payment(app, user_id, amount, description): with app.app_context(): db = get_db() cursor = db.cursor() cursor.execute( "INSERT INTO payment (user_id, amount, description) VALUES (?, ?, ?)", (user_id, amount, description)) db.commit() return cursor.lastrowid
def login(): 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 or not check_password_hash(user["password"], password): error = "Incorrect username or password" if error is None: session.clear() session["user_id"] = user["id"] return redirect(url_for("payments.user_payments")) flash(error) return render_template("auth/login.html")
def add_payment(): if request.method == "POST": db = get_db() amount = request.form["amount"] description = request.form["description"] error = None if not amount: error = "Amount required" if not description: error = "Description required" if error is None: db.execute( "INSERT INTO payment (user_id, amount, description) VALUES (?, ?, ?)", (g.user["id"], amount, description)) db.commit() return redirect(url_for("payments.user_payments")) return render_template("payments/add_payment.html")
def register(): if request.method == "POST": username = request.form["username"] password = request.form["password"] db = get_db() error = None if not username: error = "Username required" if not password: error = "Password required" elif (db.execute("SELECT id FROM user WHERE username = ?", (username, )).fetchone() is not None): error = "User {0} is already registered.".format(username) 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 view_payment(payment_id): db = get_db() payment = db.execute("SELECT * FROM payment WHERE id = ?", (payment_id, )).fetchone() return render_template("payments/payment_details.html", payment=payment)
def user_payments(): db = get_db() payments = db.execute( "SELECT * FROM payment WHERE user_id = ? ORDER BY id", (g.user["id"], )).fetchall() return render_template("payments/payments_list.html", payments=payments)