-
Notifications
You must be signed in to change notification settings - Fork 0
/
contats.py
151 lines (111 loc) · 4.16 KB
/
contats.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
from sqlite3 import dbapi2 as sqlite3
from contextlib import closing
from flask import Flask, url_for, abort, render_template, g, redirect, flash, request
# ------------------------------------------------------------------------
# Configuracoes
DATABASE = 'tmp/contats.db'
SECRET_KEY = '1234'
DEBUG = True
app = Flask(__name__)
app.config.from_object( __name__ )
# ------------------------------------------------------------------------
# Inicio do DB e funcoes de suporte
def db_connect():
return sqlite3.connect( app.config['DATABASE'] )
def db_init():
with closing( db_connect() ) as db:
with app.open_resource( 'banco.sql' ) as sql:
db.cursor().executescript( sql.read() )
db.commit()
def get_all():
query = g.db.execute( 'SELECT * FROM contatos ORDER BY id DESC' )
registros = []
for linha in query.fetchall():
registros.append(
{
"id" : linha[0],
"nome" : linha[1],
"sobrenome" : linha[2],
"email" : linha[3],
"fone" : linha[4]
}
)
return registros
def get( id ):
if not id:
return False
query = g.db.execute( 'SELECT * FROM contatos WHERE id = ? ORDER BY id DESC LIMIT 1', [ id ] )
registro = query.fetchone()
contato = {
"id" : registro[0],
"nome" : registro[1],
"sobrenome" : registro[2],
"email" : registro[3],
"fone" : registro[4],
"endereco" : registro[5],
}
return contato
# ------------------------------------------------------------------------
@app.before_request
def before_request():
g.db = db_connect()
@app.teardown_request
def teardown_request( exception ):
if hasattr( g, 'db' ):
g.db.close()
# ------------------------------------------------------------------------
@app.route('/remover/<int:id>')
def remover(id):
if not id:
flash( u'Id não informado' )
else:
g.db.execute(
'DELETE FROM contatos WHERE id = ?',
[ id ]
)
g.db.commit()
flash( u'Contato removido com sucesso' )
return redirect( url_for( 'index' ) )
@app.route('/salvar', methods = ['POST'])
def salvar():
id_editar = request.form['id']
nome = request.form['nome']
sobrenome = request.form['sobrenome']
email = request.form['email']
fone = request.form['fone']
endereco = request.form['endereco']
if not nome or not email:
flash( u'Nome e E-mail são obrigatórios' )
else:
if not id_editar:
g.db.execute(
'INSERT INTO contatos (nome, sobrenome, email, fone, endereco) VALUES (?, ?, ?, ?, ?)',
[ nome, sobrenome, email, fone, endereco ]
)
acao = 'cadastrado'
else:
g.db.execute(
'UPDATE contatos SET nome = ?, sobrenome = ?, email = ?, fone = ?, endereco = ? WHERE id = ?',
[ nome, sobrenome, email, fone, endereco, id_editar ]
)
acao = 'modificado'
g.db.commit()
flash( u'Contato %s com sucesso' % acao )
return redirect( url_for( 'index' ) )
@app.route('/editar/<int:id>')
def editar( id ):
registros = get_all()
if not id:
flash( u'Id nao informado' )
return redirect( url_for( 'index' ) )
else:
registro = get( id )
return render_template('index.html', contatos = registros, contato = registro)
@app.route('/')
def index():
registros = get_all()
return render_template('index.html', contatos = registros)
# ------------------------------------------------------------------------
if __name__ == '__main__':
app.run()