mirror of
https://github.com/Xevion/runnerspace.git
synced 2025-12-08 14:08:20 -06:00
Revamp all form submissions with Flask-WTF forms
- Rename blueprint route 'main.user' to 'main.view_user' for clarity - Rename 'forms.py' to 'route_forms.py'
This commit is contained in:
36
auth.py
36
auth.py
@@ -2,6 +2,7 @@ from flask import Blueprint, flash, redirect, request, url_for
|
||||
from flask_login import login_required, login_user, logout_user, current_user
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
from forms import LoginForm, RegistrationForm, EditProfileForm
|
||||
from models import User
|
||||
from database import db
|
||||
|
||||
@@ -9,43 +10,44 @@ blueprint = Blueprint('auth', __name__)
|
||||
|
||||
|
||||
@blueprint.route('/user/<username>', methods=['POST'])
|
||||
def bio_post():
|
||||
bio = request.form.get('bio')
|
||||
setattr(current_user, 'bio', bio)
|
||||
setattr(current_user, 'has_bio', True)
|
||||
def edit_profile_post(username: str):
|
||||
form = EditProfileForm(request.form)
|
||||
user = User.query.filter_by(username=username).first_or_404()
|
||||
|
||||
if current_user.is_admin or user.id == current_user.id
|
||||
if form.validate():
|
||||
user.about_me = form.about_me.data
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for('main.view_user', username=user.username))
|
||||
|
||||
|
||||
@blueprint.route('/login', methods=['POST'])
|
||||
def login_post():
|
||||
username = request.form.get('username')
|
||||
password = request.form.get('password')
|
||||
remember = bool(request.form.get('remember'))
|
||||
|
||||
user = User.query.filter_by(username=username).first()
|
||||
form = LoginForm(request.form)
|
||||
user = User.query.filter_by(username=form.data.username).first()
|
||||
|
||||
# check if the user actually exists, and compare password given
|
||||
if not user or not check_password_hash(user.password, password):
|
||||
if not user or not check_password_hash(user.password, form.password.data):
|
||||
flash('Please check your login details and try again.')
|
||||
return redirect(url_for('main.login'))
|
||||
|
||||
login_user(user, remember=remember)
|
||||
login_user(user, remember=form.remember.data)
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
|
||||
@blueprint.route('/signup', methods=['POST'])
|
||||
def signup_post():
|
||||
# validate and add user to db
|
||||
username = request.form.get('username')
|
||||
name = request.form.get('name')
|
||||
password = request.form.get('password')
|
||||
form = RegistrationForm(request.form)
|
||||
|
||||
user = User.query.filter_by(username=username).first() # Check if the email exists
|
||||
user = User.query.filter_by(username=form.username.data).first() # Check if the username is already in use
|
||||
if user: # redirect back to sign-up page
|
||||
flash('Email address already exists')
|
||||
flash('This username is already in use.')
|
||||
return redirect(url_for('main.signup'))
|
||||
|
||||
# Create new user with form data
|
||||
new_user = User(username=username, name=name, password=generate_password_hash(password, method='sha256'))
|
||||
new_user = User(username=form.username.data, name=form.name.data, password=generate_password_hash(form.password.data, method='sha256'))
|
||||
|
||||
# Add new user to db
|
||||
db.session.add(new_user)
|
||||
|
||||
@@ -55,7 +55,7 @@ def search():
|
||||
|
||||
|
||||
@blueprint.route('/user/<username>/')
|
||||
def user(username: str):
|
||||
def view_user(username: str):
|
||||
user = User.query.filter_by(username=username).first_or_404()
|
||||
return render_template('pages/user.html', user=user)
|
||||
|
||||
@@ -66,7 +66,7 @@ def edit_user(username: str):
|
||||
user = User.query.filter_by(username=username).first_or_404()
|
||||
if current_user.is_admin or current_user.id == user.id:
|
||||
return render_template('pages/user_edit.html', user=user)
|
||||
return redirect(url_for('main.user', username=username))
|
||||
return redirect(url_for('main.view_user', username=username))
|
||||
|
||||
|
||||
# @blueprint.route('/blogs')
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<div class="header-login">
|
||||
{% if current_user.is_authenticated %}
|
||||
Logged in as
|
||||
<a href="{{ url_for('main.user', username=current_user.username) }}" class="username">{{ current_user.username }}</a>
|
||||
<a href="{{ url_for('main.view_user', username=current_user.username) }}" class="username">{{ current_user.username }}</a>
|
||||
<a href="{{ url_for('main.edit_user', username=current_user.username) }}"><i class="fas fa-cog fa-1x"></i></a>
|
||||
| <a href="{{ url_for('auth.logout') }}">Logout</a>
|
||||
{% else %}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<div>
|
||||
<ul>
|
||||
{% for new_user in new_users %}
|
||||
<li><a href="{{ url_for('main.user', username=new_user.username) }}">{{ new_user.username }}</a> as
|
||||
<li><a href="{{ url_for('main.view_user', username=new_user.username) }}">{{ new_user.username }}</a> as
|
||||
of {{ new_user.get_registration_delta() }} ago
|
||||
</li>
|
||||
{% endfor %}
|
||||
|
||||
@@ -13,21 +13,27 @@
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
Username
|
||||
<input class="input is-large" type="text" name="username" placeholder="Username" autofocus>
|
||||
<input class="input" type="text" name="username" placeholder="Username" autofocus>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
Name
|
||||
<input class="input is-large" type="text" name="name" placeholder="Name" autofocus>
|
||||
<input class="input" type="text" name="name" placeholder="Name" autofocus>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
Password
|
||||
<input class="input is-large" type="password" name="password" placeholder="Password">
|
||||
<input class="input" type="password" name="password" placeholder="Password">
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
Confirm Password
|
||||
<input class="input" type="password" name="confirm" placeholder="Confirm Password">
|
||||
</div>
|
||||
</div>
|
||||
<button>Sign Up</button>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<div class="user-box-top">
|
||||
|
||||
<span class="user-box-username">
|
||||
<a href="{{ url_for('main.user', username=user.username) }}">{{ user.name }}</a>
|
||||
<a href="{{ url_for('main.view_user', username=user.username) }}">{{ user.name }}</a>
|
||||
</span>
|
||||
{% with seen_text = user.get_last_seen() %}
|
||||
{% if seen_text == 'Online now!' %}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<div class="post-box">
|
||||
{{ post.text }}
|
||||
<div class="post-author no-border">
|
||||
<em>Posted by <a href="{{ url_for('main.user', username=author.username) }}">{{ author.name }}</a></em> <span
|
||||
<em>Posted by <a href="{{ url_for('main.view_user', username=author.username) }}">{{ author.name }}</a></em> <span
|
||||
title="{{ post.date_posted }}">{{ post.get_time_ago() }} ago</span>. |
|
||||
<a href="{{ url_for('main.view_post', post_id=post.id) }}"><span>{{ post.comments|length }} comments</span></a>
|
||||
</div>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
{{ post.text }}
|
||||
<br>
|
||||
<div class="post-author">
|
||||
<em>Posted by <a href="{{ url_for('main.user', username=author.username) }}">{{ author.name }}</a></em> <span
|
||||
<em>Posted by <a href="{{ url_for('main.view_user', username=author.username) }}">{{ author.name }}</a></em> <span
|
||||
title="{{ post.date_posted }}">{{ post.get_time_ago() }} ago</span>.
|
||||
</div>
|
||||
<div class="post-comments">
|
||||
@@ -30,7 +30,7 @@
|
||||
<div class="post-comment">
|
||||
|
||||
<span class="comment-text">"{{ comment.text }}"</span> — <a class="comment-author"
|
||||
href="{{ url_for('main.user', username=author.username) }}">{{ author.name }}</a>
|
||||
href="{{ url_for('main.view_user', username=author.username) }}">{{ author.name }}</a>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user