-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
77 lines (64 loc) · 2.35 KB
/
App.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
from flask import Flask, render_template, request, redirect, url_for, flash
from flask_mysqldb import MySQL
app = Flask(__name__)
#Mysql Conexion
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'flaskcontacts'
mysql = MySQL(app)
#Sesion
app.secret_key = 'mysecretkey'
@app.route('/')
def Index():
cur = mysql.connection.cursor()
cur.execute('SELECT * FROM contacts')
data = cur.fetchall()
return render_template('index.html', contacts = data)
@app.route('/add_contact', methods=['POST'])
def add_contact():
if request.method == 'POST':
fullname = request.form['fullname']
passw = request.form['passw']
email = request.form['email']
cur = mysql.connection.cursor()
cur.execute('INSERT INTO contacts (fullname, passw, email) VALUES (%s, %s, %s)',
(fullname, passw, email))
mysql.connection.commit()
flash('Contacto Agregado Correctamente')
return redirect(url_for('Index'))
@app.route('/edit/<id>')
def get_contact(id):
cur =mysql.connection.cursor()
cur.execute('SELECT * FROM contacts WHERE id = %s ', [id])
data = cur.fetchall()
return render_template('edit-contact.html', contact = data[0])
@app.route('/update/<id>', methods = ['POST'])
def update_contact(id):
if request.method == 'POST':
fullname = request.form['fullname']
passw = request.form['passw']
email = request.form['email']
cur = mysql.connection.cursor()
cur.execute("""
UPDATE contacts
SET fullname = %s,
email = %s,
passw = %s
WHERE id = %s
""", (fullname, email, passw, id))
mysql.connection.commit()
flash('Contacto Actualizado Correctamente')
return redirect(url_for('Index'))
@app.route('/delete/<string:id>')
def delete_contact(id):
cur = mysql.connection.cursor()
cur.execute('DELETE FROM contacts WHERE id = {0}'.format(id))
mysql.connection.commit()
flash('Contacto Removido Correctamente')
return redirect(url_for('Index'))
@app.route('/redirect_login')
def redirect_login():
return render_template('redireccionar_admin_login.html')
if __name__ == '__main__':
app.run(port = 3000, debug = True)