Fixbug structure

This commit is contained in:
Andros Fenollosa 2017-05-11 18:31:59 +02:00
parent 3b431c6d0a
commit ce763d6493
4 changed files with 39 additions and 25 deletions

41
app.py
View File

@ -1,5 +1,5 @@
from flask import Flask, redirect, url_for, render_template, request, flash from flask import Flask, redirect, url_for, render_template, request, flash
from models import db, Contacts from models import db, Contact
from forms import ContactForm from forms import ContactForm
# Flask # Flask
@ -27,15 +27,15 @@ def new_contact():
Create new contact Create new contact
''' '''
form = ContactForm() form = ContactForm()
if request.method == 'POST' and form.validate_on_submit(): if form.validate_on_submit():
# Get form # Get form
name = request.form['name'] name = form.name.data
surname = request.form['surname'] surname = form.surname.data
email = request.form['email'] email = form.email.data
phone = request.form['phone'] phone = form.phone.data
# Save in database # Save in database
try: try:
my_contact = Contacts(name, surname, email, phone) my_contact = Contact(name, surname, email, phone)
db.session.add(my_contact) db.session.add(my_contact)
db.session.commit() db.session.commit()
# User info # User info
@ -56,13 +56,14 @@ def edit_contact(id):
:param id: Id from contact :param id: Id from contact
''' '''
form = ContactForm() form = ContactForm()
my_contact = Contacts.query.filter_by(id=id).first() my_contact = Contact.query.filter_by(id=id).first()
if request.method == 'POST' and form.validate_on_submit(): if form.validate_on_submit():
# Get form # Get form
name = request.form['name'] name = form.name.data
surname = request.form['surname'] surname = form.surname.data
email = request.form['email'] email = form.email.data
phone = request.form['phone'] phone = form.phone.data
try: try:
# Update contact # Update contact
my_contact.name = name my_contact.name = name
@ -87,7 +88,7 @@ def contacts():
''' '''
Show alls contacts Show alls contacts
''' '''
contacts = Contacts.query.order_by(Contacts.name).all() contacts = Contact.query.order_by(Contact.name).all()
return render_template('web/contacts.html', contacts=contacts) return render_template('web/contacts.html', contacts=contacts)
@ -97,21 +98,21 @@ def search():
Search Search
''' '''
name_search = request.args.get('name') name_search = request.args.get('name')
all_contacts = Contacts.query.filter( all_contacts = Contact.query.filter(
Contacts.name.contains(name_search) Contact.name.contains(name_search)
).order_by(Contacts.name).all() ).order_by(Contact.name).all()
return render_template('web/contacts.html', contacts=all_contacts) return render_template('web/contacts.html', contacts=all_contacts)
@app.route("/contacts/delete/<id>") @app.route("/contacts/delete", methods=('POST',))
def contacts_delete(id): def contacts_delete():
''' '''
Delete contact Delete contact
:param id: Id from contact :param id: Id from contact
''' '''
try: try:
mi_contacto = Contacts.query.filter_by(id=id).first() mi_contacto = Contact.query.filter_by(id=request.form['id']).first()
db.session.delete(mi_contacto) db.session.delete(mi_contacto)
db.session.commit() db.session.commit()
flash('Delete successfully.', 'danger') flash('Delete successfully.', 'danger')

View File

@ -1,4 +1,4 @@
from models import db, Contacts from models import db, Contact
from faker import Factory from faker import Factory
fake = Factory.create() fake = Factory.create()
@ -15,6 +15,6 @@ for num in range(100):
email = fake.email() email = fake.email()
phone = fake.phone_number() phone = fake.phone_number()
# Save in database # Save in database
mi_contacto = Contacts(name, surname, email, phone) mi_contacto = Contact(name, surname, email, phone)
db.session.add(mi_contacto) db.session.add(mi_contacto)
db.session.commit() db.session.commit()

View File

@ -7,7 +7,11 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///book.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app) db = SQLAlchemy(app)
class Contacts(db.Model): class Contact(db.Model):
__tablename__ = 'contacts'
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False) name = db.Column(db.String(80), nullable=False)
surname = db.Column(db.String(100), nullable=True) surname = db.Column(db.String(100), nullable=True)

View File

@ -20,8 +20,17 @@
<td>{{ contact.email }}</td> <td>{{ contact.email }}</td>
<td>{{ contact.phone }}</td> <td>{{ contact.phone }}</td>
<td class="text-right"> <td class="text-right">
<a class="btn btn-primary" href="{{ url_for('edit_contact', id=contact.id) }}">Edit</a> <div class="row">
<a class="btn btn-danger" data-toggle="confirmation" {# data-title="¿Estas seguro?" #} href="{{ url_for('contacts_delete', id=contact.id) }}">Delete</a> <div class="col-xs-6">
<a class="btn btn-primary" href="{{ url_for('edit_contact', id=contact.id) }}">Edit</a>
</div>
<div class="col-xs-6">
<form action="{{ url_for('contacts_delete') }}" method="post">
<input type="hidden" name="id" value="{{ contact.id }}">
<input type="submit" class="btn btn-danger" data-toggle="confirmation" {# data-title="¿Estas seguro?" #} value="Delete">
</form>
</div>
</div>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}