|
| 1 | +from flask import Flask, render_template, request, redirect, url_for, flash, jsonify |
| 2 | +import sqlite3 |
| 3 | +from version import version |
| 4 | +import os |
| 5 | + |
| 6 | +app = Flask(__name__) |
| 7 | +app.secret_key = "supersecretkey" |
| 8 | + |
| 9 | +DATABASE = 'database.db' |
| 10 | + |
| 11 | +def get_db(): |
| 12 | + conn = sqlite3.connect(DATABASE) |
| 13 | + conn.row_factory = sqlite3.Row |
| 14 | + return conn |
| 15 | + |
| 16 | +def init_db(): |
| 17 | + conn = get_db() |
| 18 | + with app.open_resource('schema.sql', mode='r') as f: |
| 19 | + conn.cursor().executescript(f.read()) |
| 20 | + conn.commit() |
| 21 | + |
| 22 | +@app.route('/') |
| 23 | +def index(): |
| 24 | + conn = get_db() |
| 25 | + cur = conn.cursor() |
| 26 | + cur.execute("SELECT * FROM customers_data") |
| 27 | + customers = cur.fetchall() |
| 28 | + return render_template('index.html', customers=customers) |
| 29 | + |
| 30 | +@app.route('/customer/new', methods=['GET', 'POST']) |
| 31 | +def new_customer(): |
| 32 | + if request.method == 'POST': |
| 33 | + conn = get_db() |
| 34 | + cur = conn.cursor() |
| 35 | + customer = ( |
| 36 | + request.form['name'], |
| 37 | + request.form['phone'], |
| 38 | + request.form['address'], |
| 39 | + request.form['contact'] |
| 40 | + ) |
| 41 | + cur.execute("INSERT INTO customers_data (name, phone, address, contact) VALUES (?, ?, ?, ?)", customer) |
| 42 | + conn.commit() |
| 43 | + flash('Customer added successfully!', 'success') |
| 44 | + return redirect(url_for('index')) |
| 45 | + return render_template('new_customer.html') |
| 46 | + |
| 47 | +@app.route('/customer/edit/<id>', methods=['GET', 'POST']) |
| 48 | +def edit_customer(id): |
| 49 | + conn = get_db() |
| 50 | + cur = conn.cursor() |
| 51 | + if request.method == 'POST': |
| 52 | + updated_customer = ( |
| 53 | + request.form['name'], |
| 54 | + request.form['phone'], |
| 55 | + request.form['address'], |
| 56 | + request.form['contact'], |
| 57 | + id |
| 58 | + ) |
| 59 | + cur.execute("UPDATE customers_data SET name = ?, phone = ?, address = ?, contact = ? WHERE id = ?", updated_customer) |
| 60 | + conn.commit() |
| 61 | + flash('Customer updated successfully!', 'success') |
| 62 | + return redirect(url_for('index')) |
| 63 | + cur.execute("SELECT * FROM customers_data WHERE id = ?", (id,)) |
| 64 | + customer = cur.fetchone() |
| 65 | + return render_template('edit_customer.html', customer=customer) |
| 66 | + |
| 67 | +@app.route('/customer/delete/<id>', methods=['POST']) |
| 68 | +def delete_customer(id): |
| 69 | + conn = get_db() |
| 70 | + cur = conn.cursor() |
| 71 | + cur.execute("DELETE FROM customers_data WHERE id = ?", (id,)) |
| 72 | + conn.commit() |
| 73 | + flash('Customer deleted successfully!', 'success') |
| 74 | + return redirect(url_for('index')) |
| 75 | + |
| 76 | +@app.route('/actuator/info', methods=['GET', 'POST']) |
| 77 | +def info(): |
| 78 | + return jsonify({"build": {"version": version}}) |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + init_db() # Initialize the database before starting the app |
| 82 | + app.run(host="0.0.0.0", port=5000, debug=True) |
0 commit comments