You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.7 KiB
70 lines
1.7 KiB
from flask import Flask, render_template, request, redirect |
|
from db import get_db_connection |
|
|
|
app = Flask(__name__) |
|
|
|
@app.route("/") |
|
def index(): |
|
return """ |
|
<h1>OTB Billing</h1> |
|
<p>Version 0.0.4</p> |
|
<p><a href="/clients">Clients</a></p> |
|
""" |
|
|
|
@app.route("/dbtest") |
|
def dbtest(): |
|
try: |
|
conn = get_db_connection() |
|
cursor = conn.cursor() |
|
cursor.execute("SELECT NOW()") |
|
result = cursor.fetchone() |
|
conn.close() |
|
|
|
return f"<h1>Database OK</h1><p>{result[0]}</p>" |
|
except Exception as e: |
|
return f"<h1>Database FAILED</h1><pre>{e}</pre>" |
|
|
|
@app.route("/clients") |
|
def clients(): |
|
conn = get_db_connection() |
|
cursor = conn.cursor(dictionary=True) |
|
|
|
cursor.execute("SELECT * FROM clients ORDER BY created_at DESC") |
|
clients = cursor.fetchall() |
|
|
|
conn.close() |
|
|
|
return render_template("clients/list.html", clients=clients) |
|
|
|
@app.route("/clients/new", methods=["GET","POST"]) |
|
def new_client(): |
|
if request.method == "POST": |
|
|
|
client_code = request.form["client_code"] |
|
company_name = request.form["company_name"] |
|
contact_name = request.form["contact_name"] |
|
email = request.form["email"] |
|
phone = request.form["phone"] |
|
|
|
conn = get_db_connection() |
|
cursor = conn.cursor() |
|
|
|
cursor.execute( |
|
""" |
|
INSERT INTO clients |
|
(client_code, company_name, contact_name, email, phone) |
|
VALUES (%s,%s,%s,%s,%s) |
|
""", |
|
(client_code, company_name, contact_name, email, phone) |
|
) |
|
|
|
conn.commit() |
|
conn.close() |
|
|
|
return redirect("/clients") |
|
|
|
return render_template("clients/new.html") |
|
|
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=5050)
|
|
|