mirror of
https://github.com/Xevion/runnerspace.git
synced 2025-12-06 01:16:13 -06:00
Apply new form validation features to New Post form
- Use TextAreaField instead of StringField on forms.NewPostForm - Fixed incorrect remember_me field and redirect in login - Added placeholder usage to field rendering macro
This commit is contained in:
4
auth.py
4
auth.py
@@ -34,8 +34,8 @@ def login():
|
||||
flash('Please check your login details and try again.')
|
||||
return redirect(url_for('auth.login'))
|
||||
|
||||
login_user(user, remember=form.remember.data)
|
||||
return redirect(url_for('auth.index'))
|
||||
login_user(user, remember=form.remember_me.data)
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
return render_template('pages/auth/login.html', form=form)
|
||||
|
||||
|
||||
6
forms.py
6
forms.py
@@ -1,4 +1,4 @@
|
||||
from wtforms import Form, BooleanField, StringField, PasswordField, validators
|
||||
from wtforms import Form, BooleanField, StringField, PasswordField, TextAreaField, validators
|
||||
|
||||
from validators import NoProfanity
|
||||
|
||||
@@ -22,11 +22,11 @@ class LoginForm(Form):
|
||||
|
||||
class EditProfileForm(Form):
|
||||
name = RegistrationForm.name
|
||||
about_me = StringField('About Me', [validators.Optional(), NoProfanity()])
|
||||
about_me = StringField('About Me', [validators.Optional(), NoProfanity()], description='Tell us about yourself',)
|
||||
|
||||
|
||||
class NewPostForm(Form):
|
||||
text = StringField('Text', [validators.Length(min=15, max=1000), NoProfanity()])
|
||||
text = TextAreaField('Text', [validators.Length(min=15, max=1000), NoProfanity()], description='Express yourself.')
|
||||
|
||||
|
||||
class NewCommentForm(Form):
|
||||
|
||||
@@ -28,21 +28,6 @@ def edit_profile_post(username):
|
||||
return redirect(url_for('main.edit_user', username=username))
|
||||
|
||||
|
||||
@blueprint.route('/feed/new', methods=['POST'])
|
||||
@login_required
|
||||
def new_post():
|
||||
form = NewPostForm(request.form)
|
||||
|
||||
if form.validate():
|
||||
post = Post(author=current_user.id, text=form.text.data)
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for('main.view_post', post_id=post.id))
|
||||
else:
|
||||
redirect(url_for('main.feed'))
|
||||
|
||||
|
||||
@blueprint.route('/feed/<post_id>/comment', methods=['POST'])
|
||||
@login_required
|
||||
def add_comment(post_id: int):
|
||||
|
||||
17
routes.py
17
routes.py
@@ -1,7 +1,9 @@
|
||||
from flask import Blueprint, redirect, render_template, url_for
|
||||
from flask import Blueprint, redirect, render_template, url_for, request
|
||||
from flask_login import current_user, login_required
|
||||
|
||||
from models import User, Post, Comment
|
||||
from forms import NewPostForm
|
||||
from database import db
|
||||
|
||||
blueprint = Blueprint('main', __name__)
|
||||
|
||||
@@ -28,11 +30,20 @@ def browse():
|
||||
return render_template('pages/browse.html', users=users)
|
||||
|
||||
|
||||
@blueprint.route('/feed')
|
||||
@blueprint.route('/feed', methods=['GET', 'POST'])
|
||||
def feed():
|
||||
posts = Post.query.all()
|
||||
authors = [User.query.get_or_404(post.author) for post in posts]
|
||||
return render_template('pages/feed.html', posts_and_authors=zip(posts, authors))
|
||||
form = NewPostForm(request.form)
|
||||
|
||||
if request.method == 'POST' and form.validate():
|
||||
post = Post(author=current_user.id, text=form.text.data)
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for('main.view_post', post_id=post.id))
|
||||
|
||||
return render_template('pages/feed.html', posts_and_authors=zip(posts, authors), form=form)
|
||||
|
||||
|
||||
@blueprint.route('/feed/<post_id>')
|
||||
|
||||
@@ -216,20 +216,24 @@ nav .links li:not(:last-child)::after, footer .links li:not(:last-child)::after
|
||||
filter: drop-shadow(5px 7px 6px rgba(0, 0, 0, 0.2));
|
||||
}
|
||||
|
||||
.profile-form {
|
||||
.form {
|
||||
margin: 0 auto;
|
||||
width: 80%;
|
||||
}
|
||||
.profile-form textarea {
|
||||
.form textarea {
|
||||
margin: 0.5em auto;
|
||||
height: 10em;
|
||||
width: 100%;
|
||||
resize: vertical;
|
||||
}
|
||||
.profile-form button {
|
||||
.form.post-form {
|
||||
border: none;
|
||||
}
|
||||
.form button {
|
||||
margin: 0.3em auto;
|
||||
text-align: center;
|
||||
}
|
||||
.profile-form label {
|
||||
.form label {
|
||||
margin: 0.5em 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"sourceRoot":"","sources":["styles.scss"],"names":[],"mappings":"AAAA;EACE;EACA;;AAGF;EACE;EACA;EACA;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;EAOE;;AALA;EAEE;;;AASJ;EACE;EACA;EACA;EACA;EACA;;AAGF;EACE;;;AAKJ;EACE;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;;AAEA;EACE;EACA;;AAIJ;EACE;EACA;EACA;EACA;;AAEA;EACE;EACA;;;AAKN;EACE;EACA;EACA;EACA;;AAEA;EACE;;;AAIJ;EACE;;;AAGF;EACE;;;AAGF;EACE;;;AAIA;EACE;EACA;;AAEA;EACE;;AAEA;EACE;;;AAMR;EACE;EACA;;;AAGF;EACE;EACA;EACA;EACA;;;AAGF;EACE;;;AAIA;EACE;EACA;;;AAIJ;EACE;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;EACE;EACA;;AAGF;EACE;EACA;;;AAIJ;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;EACE;EACA;;AAGF;EACE;EACA;EACA;EACA;;;AAIJ;EACE;;;AAGF;EACE;EACA;;AAEA;EACE;EACA;EACA;;;AAMF;EACE;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;EACA;EAGA;EACA;EACA;EACA;;AAKF;EACE;EACA;EACA;EACA;;;AAMR;EACE;EACA;;AAEA;EACE;EACA;EACA;;AAGF;EACE;EACA;;AAGF;EACE;;;AAIJ;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;;AAIJ;EACE;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;;AAEA;EACE;EACA;;AAIJ;EAEE;EACA;EACA;;;AAMR;EACE;;AAEA;EACE;EACA;EACA;EACA;;AAEA;EACE;;;AAKN;EACE;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;EACE;EACA;;;AAIJ;EACE;EACA;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;;AAGF;EAKE;EACA;EACA;EAEA;EACA;EACA;;AAVA;EACE;;;AAaN;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EAEA;EACA;EACA;EACA;;;AAGF;EACE","file":"styles.css"}
|
||||
{"version":3,"sourceRoot":"","sources":["styles.scss"],"names":[],"mappings":"AAAA;EACE;EACA;;AAGF;EACE;EACA;EACA;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;EAOE;;AALA;EAEE;;;AASJ;EACE;EACA;EACA;EACA;EACA;;AAGF;EACE;;;AAKJ;EACE;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;;AAGF;EACE;EACA;;AAEA;EACE;EACA;;AAIJ;EACE;EACA;EACA;EACA;;AAEA;EACE;EACA;;;AAKN;EACE;EACA;EACA;EACA;;AAEA;EACE;;;AAIJ;EACE;;;AAGF;EACE;;;AAGF;EACE;;;AAIA;EACE;EACA;;AAEA;EACE;;AAEA;EACE;;;AAMR;EACE;EACA;;;AAGF;EACE;EACA;EACA;EACA;;;AAGF;EACE;;;AAIA;EACE;EACA;;;AAIJ;EACE;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;EACE;EACA;;AAGF;EACE;EACA;;;AAIJ;EACE;;;AAGF;EACE;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;EACE;EACA;;AAGF;EACE;EACA;EACA;EACA;;;AAIJ;EACE;;;AAGF;EACE;EACA;;AAEA;EACE;EACA;EACA;;;AAMF;EACE;EACA;;AAGF;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;EACA;EAGA;EACA;EACA;EACA;;AAKF;EACE;EACA;EACA;EACA;;;AAMR;EACE;EACA;;AAEA;EACE;EACA;EACA;EACA;;AAGF;EACE;;AAGF;EACE;EACA;;AAGF;EACE;;;AAIJ;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;;AAIJ;EACE;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;;AAEA;EACE;;AAEA;EACE;EACA;;AAIJ;EAEE;EACA;EACA;;;AAMR;EACE;;AAEA;EACE;EACA;EACA;EACA;;AAEA;EACE;;;AAKN;EACE;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;EACE;EACA;;;AAIJ;EACE;EACA;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;;AAGF;EAME;EACA;EACA;EAEA;EACA;EACA;;AAXA;EACE;;;AAcN;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EAEA;EACA;EACA;EACA;;;AAGF;EACE","file":"styles.css"}
|
||||
@@ -256,7 +256,7 @@ nav, footer {
|
||||
}
|
||||
}
|
||||
|
||||
.profile-form {
|
||||
.form {
|
||||
margin: 0 auto;
|
||||
width: 80%;
|
||||
|
||||
@@ -264,6 +264,11 @@ nav, footer {
|
||||
margin: 0.5em auto;
|
||||
height: 10em;
|
||||
width: 100%;
|
||||
resize: vertical;
|
||||
}
|
||||
|
||||
&.post-form {
|
||||
border: none;
|
||||
}
|
||||
|
||||
button {
|
||||
@@ -374,6 +379,7 @@ form {
|
||||
&[type="submit"] {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
//background: #ecf0f1;
|
||||
border: #ccc 1px solid;
|
||||
border-bottom: #ccc 2px solid;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
{% macro render_field(field) %}
|
||||
{{ field.label }}
|
||||
{{ field(**kwargs)|safe }}
|
||||
{% macro render_field(field, show_label=True) %}
|
||||
{% if show_label %}
|
||||
{{ field.label }}
|
||||
{% endif %}
|
||||
{{ field(placeholder=field.description, **kwargs)|safe }}
|
||||
{% if field.errors %}
|
||||
<ul class=errors>
|
||||
{% for error in field.errors %}
|
||||
|
||||
@@ -1,23 +1,16 @@
|
||||
{% extends 'layouts/index.html' %}
|
||||
{% from 'macros.html' import render_field %}
|
||||
|
||||
{% block content %}
|
||||
{% if current_user.is_authenticated %}
|
||||
{% with messages = get_flashed_messages() %}
|
||||
{% if messages or true %}
|
||||
<span class="error-message center-message">
|
||||
{{ messages[0] }}
|
||||
</span>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
<form method="POST" action="{{ url_for('forms.new_post') }}" class="profile-form">
|
||||
<label>
|
||||
<textarea type="text" name="text" placeholder="Write a post between 15 and 1000 characters. Express yourself."></textarea>
|
||||
</label>
|
||||
<button class="button">Create Post</button>
|
||||
<form method="POST" class="form post-form">
|
||||
{{ render_field(form.text, show_label=False) }}
|
||||
<input type=submit value="Create Post">
|
||||
</form>
|
||||
{% endif %}
|
||||
|
||||
<hr style="margin: 2em 0">
|
||||
|
||||
{% for post, author in posts_and_authors %}
|
||||
<div class="post-box">
|
||||
{{ post.text }}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
{% block content %}
|
||||
<h3>Edit Profile</h3>
|
||||
<form method="POST" action="{{ url_for('forms.edit_profile_post', username=user.username) }}" class="profile-form">
|
||||
<form method="POST" action="{{ url_for('forms.edit_profile_post', username=user.username) }}" class="form">
|
||||
<label>
|
||||
Name<br>
|
||||
<input type="text" name="name" value="{{ user.name }}">
|
||||
|
||||
Reference in New Issue
Block a user