-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
122 lines (93 loc) · 3.41 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
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
from flask import Flask, render_template, request, url_for, session
from flask_migrate import Migrate
from werkzeug.utils import redirect
from database import db
from forms import PersonaForm
from models import Persona
app = Flask(__name__)
app.secret_key="develoteca"
USER_DB = 'postgres'
PASS_DB = 'admin'
URL_DB = 'localhost'
NAME_DB = 'clientes_db'
FULL_URL_DB = f'postgresql://{USER_DB}:{PASS_DB}@{URL_DB}/{NAME_DB}'
app.config['SQLALCHEMY_DATABASE_URI'] = FULL_URL_DB
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'llave_secreta'
db.init_app(app)
migrate = Migrate()
migrate.init_app(app, db)
@app.route('/')
def inicio():
return render_template('sitio/index.html')
@app.route('/sobre')
def sobre():
return render_template('sitio/sobre.html')
@app.route('/servicios')
def servicios():
return render_template('sitio/servicios.html')
@app.route('/admin')
def admin_index():
if not 'login' in session:
return redirect('/admin/login')
return render_template('admin/indexad.html')
@app.route('/admin/login')
def login():
return render_template('admin/login.html')
@app.route('/admin/login', methods=['POST'])
def log():
_usuario=request.form['txtUsuario']
_password=request.form['txtPassword']
if _usuario == 'admin' and _password == '123':
session['login']=True
session['usuario']='Administrador'
return redirect('/admin')
return render_template('admin/login.html')
@app.route('/admin/clientes')
def clientes():
if not 'login' in session:
return redirect('/admin/login')
personas = Persona.query.order_by('id')
total_personas = Persona.query.count()
app.logger.debug(f'Listado personas: {personas}')
app.logger.debug(f'Total personas: {total_personas}')
return render_template('admin/clientes.html', personas=personas, total_personas=total_personas)
@app.route('/admin/ver/<int:id>')
def ver_detalle(id):
persona = Persona.query.get_or_404(id)
app.logger.debug(f'Ver Persona{persona}')
return render_template('admin/detalle.html', persona=persona)
@app.route('/admin/agregar', methods=['GET','POST'])
def agregar():
persona = Persona()
personaForm = PersonaForm(obj=persona)
if request.method == 'POST':
if personaForm.validate_on_submit():
personaForm.populate_obj(persona)
app.logger.debug(f'Persona a insertar: {persona}')
db.session.add(persona)
db.session.commit()
return redirect(url_for('clientes'))
return render_template('admin/agregar.html', forma=personaForm)
@app.route('/admin/editar/<int:id>', methods=['GET', 'POST'])
def editar(id):
persona = Persona.query.get_or_404(id)
personaForma = PersonaForm(obj=persona)
if request.method == 'POST':
if personaForma.validate_on_submit():
personaForma.populate_obj(persona)
app.logger.debug(f'Persona a actualizar: {persona}')
db.session.commit()
return redirect(url_for('clientes'))
return render_template('admin/editar.html', forma=personaForma)
@app.route('/admin/eliminar/<int:id>')
def eliminar(id):
persona = Persona.query.get_or_404(id)
app.logger.debug(f'Persona a eliminar: {persona}')
db.session.delete(persona)
db.session.commit()
return redirect(url_for('clientes'))
@app.route('/admin/cerrar')
def cerrar():
session.clear()
return redirect('/admin/login')