-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_clients.py
129 lines (107 loc) · 4.13 KB
/
app_clients.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
import mysql.connector as sql
from app_clients_db import connect_to_db # to import connection function from app_clients_db.py
app=Flask(__name__)
app.secret_key="admin123"
auth = HTTPBasicAuth()
# Login setup
users = {"admin": generate_password_hash("admin102030")}
@auth.verify_password
def verify_password(username, password):
if username in users and check_password_hash(users.get(username), password):
return username
@app.route("/")
@app.route("/index")
@auth.login_required
def index ():
con = connect_to_db() # make new connection
if con is None:
return "Failed to connect to the database" # message for connection failure
try:
cur=con.cursor()
cur.execute("SELECT * FROM clientes")
data=cur.fetchall()
print("Fetched data:", data) # Debugging output
return render_template("index.html", datas=data)
except Exception as e:
return f"An error occurred: {e}" # generate any cursor-related errors
finally:
if con.is_connected():
con.close() #close connection
@app.route("/add_user", methods=["POST", "GET"])
def add_user():
con=connect_to_db() # make new connection
if con is None:
return "Failed to connect to the database" # message for connection failure
if request.method=="POST":
nome=request.form["nome"]
email=request.form["email"]
idade=request.form.get("idade")
cidade=request.form["cidade"]
cpf=request.form["cpf"]
try:
cur=con.cursor()
cur.execute("INSERT INTO clientes (NOME, EMAIL, IDADE, CIDADE, CPF) VALUES (%s, %s, %s, %s, %s)", (nome, email, idade, cidade, cpf))
con.commit()
flash("Dados cadastrados", "success")
return redirect (url_for("index"))
except Exception as e:
return f"An error occurred: {e}"
finally:
if con.is_connected():
con.close()
return render_template("add_user.html")
@app.route("/edit_user/<string:id>", methods=["POST", "GET"])
def edit_user(id):
con=connect_to_db() # make new connection
if con is None:
return "Failed to connect to the database" # message for connection failure
if request.method=="POST":
nome=request.form["nome"]
email=request.form["email"]
idade=request.form.get("idade")
cidade=request.form["cidade"]
cpf=request.form["cpf"]
try:
cur=con.cursor()
cur.execute("UPDATE clientes SET NOME=%s, EMAIL=%s, IDADE=%s, CIDADE=%s, CPF=%s WHERE ID=%s", (nome, email, idade, cidade, cpf, id))
con.commit()
flash("Dados atualizados", "success")
return redirect(url_for("index"))
except Exception as e:
return f"An error occurred {e}"
finally:
if con.is_connected():
con.close()
try:
if con:
cur=con.cursor(dictionary=True)
cur.execute("SELECT * FROM clientes WHERE ID =%s", (id,))
data=cur.fetchone()
print("Fetched data for editing:", data)
return render_template("edit_user.html", datas=data)
except Exception as e:
return f"An error occurred: {e}"
finally:
if con.is_connected():
con.close()
@app.route("/delete_user/<string:id>", methods=["GET"])
def delete_user(id):
con=connect_to_db() # make new connection
if con is None:
return "Failed to connect to the database" # message for connection failure
try:
cur=con.cursor()
cur.execute("DELETE FROM clientes WHERE ID=%s", (id,))
con.commit()
flash("Dados deletados", "warning")
return redirect(url_for("index"))
except Exception as e:
return f"An error occurred: {e}"
finally:
if con.is_connected():
con.close()
if __name__=='__main__':
app.run(host='0.0.0.0', port=80, debug=True)