Skip to content
Snippets Groups Projects
home.py 1 KiB
Newer Older
Alex's avatar
Alex committed
from flask import Blueprint, render_template, request, redirect
from config import url_for2
from database.db import db
Alex's avatar
Alex committed
from models.entity import Entity

home_blueprint = Blueprint('home', __name__, url_prefix='/')

@home_blueprint.route('')
def home():
    return render_template('index.html')

@home_blueprint.route('/hello/<name>')
def hello(name: str):
    return "Hello, " + name


# Example CRUD route

@home_blueprint.route('/entities', methods=['GET', 'POST'])
def entities():
    if request.method == "GET":
        try:
            rows = ""
            for e in db.session.query(Entity).all():
                rows += str(e) + "<br>"

            return rows
        except:
Alex's avatar
Alex committed
            return "App database error"
    else:
Alex's avatar
Alex committed
        name = request.form.get('name')
        age = request.form.get('age')
Alex's avatar
Alex committed
        try:
Alex's avatar
Alex committed
            db.session.add(Entity(name=name, age=int(age)))
Alex's avatar
Alex committed
            db.session.commit()
            return redirect(url_for2('.entities'))
        except:
Alex's avatar
Alex committed
            return "Could not add entity."