From 7b4d29269119d2f2529980aaafc96855727f9414 Mon Sep 17 00:00:00 2001 From: Goutte Date: Fri, 18 Oct 2019 12:35:11 +0200 Subject: [PATCH] Rework the controllers. --- flaskr/controllers/main.py | 42 ------------------------------------------ flaskr/controllers/main_controller.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ flaskr/controllers/user_controller.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 42 deletions(-) delete mode 100644 flaskr/controllers/main.py create mode 100644 flaskr/controllers/main_controller.py create mode 100644 flaskr/controllers/user_controller.py diff --git a/flaskr/controllers/main.py b/flaskr/controllers/main.py deleted file mode 100644 index 97a4d5f..0000000 --- a/flaskr/controllers/main.py +++ /dev/null @@ -1,42 +0,0 @@ -from flask import Blueprint, render_template, flash, request, redirect, url_for -from flask_login import login_user, logout_user, login_required - -from flaskr.extensions import cache -from flaskr.forms import LoginForm -from flaskr.models import User - -main = Blueprint('main', __name__) - - -@main.route('/') -@cache.cached(timeout=1000) -def home(): - return render_template('index.html') - - -@main.route("/login", methods=["GET", "POST"]) -def login(): - form = LoginForm() - - if form.validate_on_submit(): - user = User.query.filter_by(username=form.username.data).one() - login_user(user) - - flash("Logged in successfully.", "success") - return redirect(request.args.get("next") or url_for(".home")) - - return render_template("login.html", form=form) - - -@main.route("/logout") -def logout(): - logout_user() - flash("You have been logged out.", "success") - - return redirect(url_for(".home")) - - -@main.route("/restricted") -@login_required -def restricted(): - return "You can only see this if you are logged in!", 200 diff --git a/flaskr/controllers/main_controller.py b/flaskr/controllers/main_controller.py new file mode 100644 index 0000000..46b7e11 --- /dev/null +++ b/flaskr/controllers/main_controller.py @@ -0,0 +1,44 @@ +from flask import Blueprint, render_template, flash, request, redirect, url_for + +from flaskr.extensions import cache +from flaskr.forms import LoginForm, EstimateForm +from flaskr.models import db, User, Estimation + +from flaskr.core import generate_unique_id + +main = Blueprint('main', __name__) + + +@main.route('/') +@cache.cached(timeout=1000) +def home(): + return render_template('index.html') + + +@main.route("/estimate", methods=["GET", "POST"]) +def estimate(): + form = EstimateForm() + + if form.validate_on_submit(): + + # FIXME: do things here with the form + + id = generate_unique_id() + + # estimation = form.data + + estimation = Estimation() + estimation.email = form.email.data + estimation.first_name = form.first_name.data + estimation.last_name = form.last_name.data + estimation.status = 'pending' + + db.session.add(estimation) + db.session.commit() + + flash("Estimation request submitted successfully.", "success") + return redirect(url_for(".home")) + # return render_template("estimate-debrief.html", form=form) + + return render_template("estimate.html", form=form) + diff --git a/flaskr/controllers/user_controller.py b/flaskr/controllers/user_controller.py new file mode 100644 index 0000000..82cecea --- /dev/null +++ b/flaskr/controllers/user_controller.py @@ -0,0 +1,35 @@ +from flask import Blueprint, render_template, flash, request, redirect, url_for +from flask_login import login_user, logout_user, login_required + +from flaskr.forms import LoginForm +from flaskr.models import db, User + +from flaskr.controllers.main_controller import main + + +@main.route("/login", methods=["GET", "POST"]) +def login(): + form = LoginForm() + + if form.validate_on_submit(): + user = User.query.filter_by(username=form.username.data).one() + login_user(user) + + flash("Logged in successfully.", "success") + return redirect(request.args.get("next") or url_for(".home")) + + return render_template("login.html", form=form) + + +@main.route("/logout") +def logout(): + logout_user() + flash("You have been logged out.", "success") + + return redirect(url_for(".home")) + + +# @main.route("/restricted") +# @login_required +# def restricted(): +# return "You can only see this if you are logged in!", 200 -- libgit2 0.21.2