from flask import Flask, render_template, request, redirect, url_for, Blueprint
from blueprintapp.app import db
from blueprintapp.blueprints.people.models import Person


people = Blueprint('people', __name__, template_folder='templates')


@people.route('/', methods=['GET', 'POST'])
def index():
    people = Person.query.all()
    return render_template('people/index.html', people=people)


@people.route('/create', methods=['GET', 'POST'])
def create():
    if request.method == 'GET':
        return render_template('people/create.html')
    elif request.method == 'POST':
        name = request.form.get('name')
        job = request.form.get('job')
        age = int(request.form.get('age'))

        person = Person(name=name, job=job, age=age)
        db.session.add(person)
        db.session.commit()
        return redirect(url_for('people.index'))
