|
1 | 1 | # project/server/user/views.py |
2 | 2 |
|
3 | 3 |
|
4 | | -from flask import render_template, Blueprint, url_for, \ |
5 | | - redirect, flash, request |
| 4 | +from flask import render_template, Blueprint, url_for, redirect, flash, request |
6 | 5 | from flask_login import login_user, logout_user, login_required |
7 | 6 |
|
8 | 7 | from project.server import bcrypt, db |
9 | 8 | from project.server.models import User |
10 | 9 | from project.server.user.forms import LoginForm, RegisterForm |
11 | 10 |
|
12 | 11 |
|
13 | | -user_blueprint = Blueprint('user', __name__,) |
| 12 | +user_blueprint = Blueprint("user", __name__) |
14 | 13 |
|
15 | 14 |
|
16 | | -@user_blueprint.route('/register', methods=['GET', 'POST']) |
| 15 | +@user_blueprint.route("/register", methods=["GET", "POST"]) |
17 | 16 | def register(): |
18 | 17 | form = RegisterForm(request.form) |
19 | 18 | if form.validate_on_submit(): |
20 | | - user = User( |
21 | | - email=form.email.data, |
22 | | - password=form.password.data |
23 | | - ) |
| 19 | + user = User(email=form.email.data, password=form.password.data) |
24 | 20 | db.session.add(user) |
25 | 21 | db.session.commit() |
26 | 22 |
|
27 | 23 | login_user(user) |
28 | 24 |
|
29 | | - flash('Thank you for registering.', 'success') |
| 25 | + flash("Thank you for registering.", "success") |
30 | 26 | return redirect(url_for("user.members")) |
31 | 27 |
|
32 | | - return render_template('user/register.html', form=form) |
| 28 | + return render_template("user/register.html", form=form) |
33 | 29 |
|
34 | 30 |
|
35 | | -@user_blueprint.route('/login', methods=['GET', 'POST']) |
| 31 | +@user_blueprint.route("/login", methods=["GET", "POST"]) |
36 | 32 | def login(): |
37 | 33 | form = LoginForm(request.form) |
38 | 34 | if form.validate_on_submit(): |
39 | 35 | user = User.query.filter_by(email=form.email.data).first() |
40 | 36 | if user and bcrypt.check_password_hash( |
41 | | - user.password, request.form['password']): |
| 37 | + user.password, request.form["password"] |
| 38 | + ): |
42 | 39 | login_user(user) |
43 | | - flash('You are logged in. Welcome!', 'success') |
44 | | - return redirect(url_for('user.members')) |
| 40 | + flash("You are logged in. Welcome!", "success") |
| 41 | + return redirect(url_for("user.members")) |
45 | 42 | else: |
46 | | - flash('Invalid email and/or password.', 'danger') |
47 | | - return render_template('user/login.html', form=form) |
48 | | - return render_template('user/login.html', title='Please Login', form=form) |
| 43 | + flash("Invalid email and/or password.", "danger") |
| 44 | + return render_template("user/login.html", form=form) |
| 45 | + return render_template("user/login.html", title="Please Login", form=form) |
49 | 46 |
|
50 | 47 |
|
51 | | -@user_blueprint.route('/logout') |
| 48 | +@user_blueprint.route("/logout") |
52 | 49 | @login_required |
53 | 50 | def logout(): |
54 | 51 | logout_user() |
55 | | - flash('You were logged out. Bye!', 'success') |
56 | | - return redirect(url_for('main.home')) |
| 52 | + flash("You were logged out. Bye!", "success") |
| 53 | + return redirect(url_for("main.home")) |
57 | 54 |
|
58 | 55 |
|
59 | | -@user_blueprint.route('/members') |
| 56 | +@user_blueprint.route("/members") |
60 | 57 | @login_required |
61 | 58 | def members(): |
62 | | - return render_template('user/members.html') |
| 59 | + return render_template("user/members.html") |
0 commit comments