diff --git a/app.py b/app.py index 1c8445b..9ef1de2 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ 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 # Flask @@ -27,15 +27,15 @@ def new_contact(): Create new contact ''' form = ContactForm() - if request.method == 'POST' and form.validate_on_submit(): + if form.validate_on_submit(): # Get form - name = request.form['name'] - surname = request.form['surname'] - email = request.form['email'] - phone = request.form['phone'] + name = form.name.data + surname = form.surname.data + email = form.email.data + phone = form.phone.data # Save in database try: - my_contact = Contacts(name, surname, email, phone) + my_contact = Contact(name, surname, email, phone) db.session.add(my_contact) db.session.commit() # User info @@ -56,13 +56,14 @@ def edit_contact(id): :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(): + my_contact = Contact.query.filter_by(id=id).first() + if form.validate_on_submit(): # Get form - name = request.form['name'] - surname = request.form['surname'] - email = request.form['email'] - phone = request.form['phone'] + name = form.name.data + surname = form.surname.data + email = form.email.data + phone = form.phone.data + try: # Update contact my_contact.name = name @@ -87,7 +88,7 @@ def 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) @@ -97,21 +98,21 @@ def search(): Search ''' name_search = request.args.get('name') - all_contacts = Contacts.query.filter( - Contacts.name.contains(name_search) - ).order_by(Contacts.name).all() + all_contacts = Contact.query.filter( + Contact.name.contains(name_search) + ).order_by(Contact.name).all() return render_template('web/contacts.html', contacts=all_contacts) -@app.route("/contacts/delete/") -def contacts_delete(id): +@app.route("/contacts/delete", methods=('POST',)) +def contacts_delete(): ''' Delete contact :param id: Id from contact ''' 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.commit() flash('Delete successfully.', 'danger') diff --git a/migrations.py b/migrations.py index 43d0cfe..f9beb6e 100644 --- a/migrations.py +++ b/migrations.py @@ -1,4 +1,4 @@ -from models import db, Contacts +from models import db, Contact from faker import Factory fake = Factory.create() @@ -15,6 +15,6 @@ for num in range(100): email = fake.email() phone = fake.phone_number() # Save in database - mi_contacto = Contacts(name, surname, email, phone) + mi_contacto = Contact(name, surname, email, phone) db.session.add(mi_contacto) db.session.commit() diff --git a/models.py b/models.py index f19cdc1..0e98aa3 100644 --- a/models.py +++ b/models.py @@ -7,7 +7,11 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///book.sqlite' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) -class Contacts(db.Model): +class Contact(db.Model): + + + __tablename__ = 'contacts' + id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False) surname = db.Column(db.String(100), nullable=True) diff --git a/templates/web/contacts.html b/templates/web/contacts.html index d5e44ce..fc0df3b 100644 --- a/templates/web/contacts.html +++ b/templates/web/contacts.html @@ -20,8 +20,17 @@ {{ contact.email }} {{ contact.phone }} - Edit - Delete +
+
+ Edit +
+
+
+ + +
+
+
{% endfor %}