2017-05-10 18:41:02 +02:00
|
|
|
from flask import Flask, redirect, url_for, render_template, request, flash
|
2017-05-11 18:31:59 +02:00
|
|
|
from models import db, Contact
|
2017-05-10 18:41:02 +02:00
|
|
|
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
|
2017-05-16 19:51:21 +02:00
|
|
|
|
|
|
|
@app.context_processor
|
|
|
|
def add_lang_to_templates():
|
|
|
|
return {'lang': 'en'}
|
|
|
|
|
2017-05-10 18:41:02 +02:00
|
|
|
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()
|
2017-05-11 18:31:59 +02:00
|
|
|
if form.validate_on_submit():
|
2017-05-10 18:41:02 +02:00
|
|
|
# Get form
|
2017-05-11 18:31:59 +02:00
|
|
|
name = form.name.data
|
|
|
|
surname = form.surname.data
|
|
|
|
email = form.email.data
|
|
|
|
phone = form.phone.data
|
2017-05-10 18:41:02 +02:00
|
|
|
# Save in database
|
|
|
|
try:
|
2017-05-11 18:31:59 +02:00
|
|
|
my_contact = Contact(name, surname, email, phone)
|
2017-05-10 18:41:02 +02:00
|
|
|
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
|
2017-05-16 19:51:21 +02:00
|
|
|
'''
|
|
|
|
rtn = None
|
2017-05-11 18:31:59 +02:00
|
|
|
my_contact = Contact.query.filter_by(id=id).first()
|
2017-05-16 19:51:21 +02:00
|
|
|
if request.method.lower() == 'get':
|
|
|
|
form = ContactForm(obj=my_contact)
|
|
|
|
else:
|
|
|
|
form = ContactForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
form.populate_obj(my_contact)
|
|
|
|
# Get form
|
|
|
|
name = form.name.data
|
|
|
|
surname = form.surname.data
|
|
|
|
email = form.email.data
|
|
|
|
phone = form.phone.data
|
|
|
|
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')
|
|
|
|
rtn = redirect(url_for('index'))
|
|
|
|
except:
|
|
|
|
db.session.rollback()
|
|
|
|
flash('Error updating contact.', 'danger')
|
|
|
|
return rtn if rtn is not None else render_template(
|
2017-05-10 18:41:02 +02:00
|
|
|
'web/edit_contact.html',
|
|
|
|
form=form,
|
2017-05-16 19:51:21 +02:00
|
|
|
contact_id=my_contact.id)
|
2017-05-10 18:41:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/contacts")
|
|
|
|
def contacts():
|
|
|
|
'''
|
|
|
|
Show alls contacts
|
|
|
|
'''
|
2017-05-11 18:31:59 +02:00
|
|
|
contacts = Contact.query.order_by(Contact.name).all()
|
2017-05-10 18:41:02 +02:00
|
|
|
return render_template('web/contacts.html', contacts=contacts)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/search")
|
|
|
|
def search():
|
|
|
|
'''
|
|
|
|
Search
|
|
|
|
'''
|
|
|
|
name_search = request.args.get('name')
|
2017-05-11 18:31:59 +02:00
|
|
|
all_contacts = Contact.query.filter(
|
|
|
|
Contact.name.contains(name_search)
|
|
|
|
).order_by(Contact.name).all()
|
2017-05-10 18:41:02 +02:00
|
|
|
return render_template('web/contacts.html', contacts=all_contacts)
|
|
|
|
|
|
|
|
|
2017-05-11 18:31:59 +02:00
|
|
|
@app.route("/contacts/delete", methods=('POST',))
|
|
|
|
def contacts_delete():
|
2017-05-10 18:41:02 +02:00
|
|
|
'''
|
|
|
|
Delete contact
|
|
|
|
'''
|
|
|
|
try:
|
2017-05-11 18:31:59 +02:00
|
|
|
mi_contacto = Contact.query.filter_by(id=request.form['id']).first()
|
2017-05-10 18:41:02 +02:00
|
|
|
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
|
2017-05-16 19:51:21 +02:00
|
|
|
app.run(host='0.0.0.0', port=8888, debug=True)
|