-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathapp.py
127 lines (107 loc) · 3.34 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
123
124
125
126
127
from flask import Flask, redirect, url_for, render_template, request, flash
from models import db, Contacts
from forms import ContactForm
# Flask
app = Flask(__name__)
app.secret_key = 'my secret'
# Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///book.sqlite'
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root@localhost/book'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
@app.route("/")
def index():
'''
Home page
'''
return redirect(url_for('contacts'))
@app.route("/new_contact", methods=('GET', 'POST'))
def new_contact():
'''
Create new contact
'''
form = ContactForm()
if request.method == 'POST' and form.validate_on_submit():
# Get form
name = request.form['name']
surname = request.form['surname']
email = request.form['email']
phone = request.form['phone']
# Save in database
try:
my_contact = Contacts(name, surname, email, phone)
db.session.add(my_contact)
db.session.commit()
# User info
flash('Contact created correctly', 'success')
return redirect(url_for('contacts'))
except:
db.session.rollback()
flash('Error generating contact.', 'danger')
return render_template('web/new_contact.html', form=form)
@app.route("/edit_contact/<id>", methods=('GET', 'POST'))
def edit_contact(id):
'''
Edit contact
:param id: Id from contact
'''
form = ContactForm()
my_contact = Contacts.query.filter_by(id=id).first()
if request.method == 'POST' and form.validate_on_submit():
# Get form
name = request.form['name']
surname = request.form['surname']
email = request.form['email']
phone = request.form['phone']
try:
# Update contact
my_contact.name = name
my_contact.surname = surname
my_contact.email = email
my_contact.phone = phone
db.session.add(my_contact)
db.session.commit()
# User info
flash('Saved successfully', 'success')
except:
db.session.rollback()
flash('Error update contact.', 'danger')
return render_template(
'web/edit_contact.html',
form=form,
my_contact=my_contact)
@app.route("/contacts")
def contacts():
'''
Show alls contacts
'''
contacts = Contacts.query.order_by(Contacts.name).all()
return render_template('web/contacts.html', contacts=contacts)
@app.route("/search")
def search():
'''
Search
'''
name_search = request.args.get('name')
all_contacts = Contacts.query.filter(
Contacts.name.contains(name_search)
).order_by(Contacts.name).all()
return render_template('web/contacts.html', contacts=all_contacts)
@app.route("/contacts/delete/<id>")
def contacts_delete(id):
'''
Delete contact
:param id: Id from contact
'''
try:
mi_contacto = Contacts.query.filter_by(id=id).first()
db.session.delete(mi_contacto)
db.session.commit()
flash('Delete successfully.', 'danger')
except:
db.session.rollback()
flash('Error delete contact.', 'danger')
return redirect(url_for('contacts'))
if __name__ == "__main__":
app.debug = True
app.run()