diff --git a/app.py b/app.py index 1d3dfa3..d3dca17 100644 --- a/app.py +++ b/app.py @@ -109,7 +109,7 @@ def create_app(): post_count: int = 0 for author in random.choices(all_users, k=count // 2): - new_post = Post(author=author.id, text=fake.paragraph(nb_sentences=2)) + new_post = Post(author=author, text=fake.paragraph(nb_sentences=2)) db.session.add(new_post) post_count += 1 @@ -119,7 +119,7 @@ def create_app(): comment_count: int = 0 for post in Post.query.all(): for _ in range(random.randint(3, len(all_users) // 4)): - new_comment = Comment(text=fake.paragraph(nb_sentences=1), author=random.choice(all_users).id, post=post.id) + new_comment = Comment(text=fake.paragraph(nb_sentences=1), author=random.choice(all_users), post=post) db.session.add(new_comment) comment_count += 1 diff --git a/auth.py b/auth.py index f24cf00..d809e49 100644 --- a/auth.py +++ b/auth.py @@ -14,7 +14,7 @@ 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 current_user.is_admin or user == current_user: if form.validate(): user.about_me = form.about_me.data db.session.commit() @@ -24,6 +24,9 @@ def edit_profile_post(username: str): @blueprint.route('/login', methods=['GET', 'POST']) def login(): + if current_user.is_authenticated: + return redirect(url_for('main.index')) + form = LoginForm(request.form) if request.method == 'POST' and form.validate(): @@ -44,7 +47,9 @@ def login(): @blueprint.route('/signup', methods=['GET', 'POST']) def signup(): - # validate and add user to db + if current_user.is_authenticated: + return redirect(url_for('main.index')) + form = RegistrationForm(request.form) if request.method == 'POST' and form.validate(): diff --git a/models.py b/models.py index 0817099..ffdbd95 100644 --- a/models.py +++ b/models.py @@ -21,8 +21,8 @@ class User(UserMixin, db.Model): last_seen = db.Column(db.DateTime, nullable=False, server_default=func.now()) last_ip = db.Column(db.String(64), nullable=True) is_admin = db.Column(db.Boolean, default=False) - posts = db.relationship("Post") - comments = db.relationship("Comment") + posts = db.relationship("Post", backref='author') + comments = db.relationship("Comment", backref='author') def get_last_seen(self) -> str: delta: datetime.timedelta = datetime.datetime.utcnow() - self.last_seen @@ -40,14 +40,15 @@ class User(UserMixin, db.Model): def display_about(self) -> str: return self.about_me or "This user hasn't written a bio yet." + class Post(db.Model): id = db.Column(db.Integer, primary_key=True) - author = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) text = db.Column(db.Text) date_posted = db.Column(db.DateTime, server_default=func.now()) date_updated = db.Column(db.DateTime, nullable=True) likes = db.Column(db.Text, default='[]') - comments = db.relationship("Comment") + comments = db.relationship("Comment", backref='post') def get_likes(self) -> List[int]: """Return the IDs of the Users who have liked this post.""" @@ -72,6 +73,6 @@ class Post(db.Model): class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.Text, nullable=False) - author = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) - post = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) date_posted = db.Column(db.DateTime, server_default=func.now()) diff --git a/route_forms.py b/route_forms.py index 23afa7d..2920904 100644 --- a/route_forms.py +++ b/route_forms.py @@ -1,4 +1,4 @@ -from flask import Blueprint, flash, redirect, request, url_for +from flask import Blueprint, flash, redirect, request, url_for, render_template from flask_login import current_user, login_required from profanity_filter import ProfanityFilter from forms import RegistrationForm, EditProfileForm, NewPostForm, NewCommentForm @@ -15,7 +15,7 @@ def edit_profile_post(username): user = db.session.query(User).filter_by(username=username).first_or_404() # Allow admins to edit profiles, but deny other users - if not current_user.is_admin and current_user.id != user.id: + if not current_user.is_admin and current_user != user: return redirect(url_for('main.view_user', username=username)) form = RegistrationForm(request.form) @@ -34,13 +34,11 @@ def add_comment(post_id: int): post = Post.query.get_or_404(post_id) form = NewCommentForm(request.form) - if form.validate(): - if not pf.is_clean(form.text.data): - flash('Sorry, profanity is not allowed on runnerspace.') - return redirect(url_for('main.view_post', post_id=post_id)) - - comment = Comment(post=post.id, author=current_user.id, text=form.text.data) + if form.validate_on_submit(): + comment = Comment(post=post, author=current_user, text=form.text.data) db.session.add(comment) db.session.commit() - return redirect(url_for('main.view_post', post_id=post.id)) + return redirect(url_for('main.view_post', post_id=post.id)) + + return render_template('pages/post.html', form=form, post=post) diff --git a/routes.py b/routes.py index eb7624c..5cafb39 100644 --- a/routes.py +++ b/routes.py @@ -2,7 +2,7 @@ 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, EditProfileForm +from forms import NewPostForm, NewCommentForm, EditProfileForm from database import db blueprint = Blueprint('main', __name__) @@ -32,27 +32,23 @@ def browse(): @blueprint.route('/feed', methods=['GET', 'POST']) def feed(): - posts = Post.query.all() - authors = [User.query.get_or_404(post.author) for post in posts] + posts = Post.query.order_by(Post.date_posted.desc()).all() form = NewPostForm(request.form) if request.method == 'POST' and form.validate(): - post = Post(author=current_user.id, text=form.text.data) + post = Post(author=current_user, 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) + return render_template('pages/feed.html', posts=posts, form=form) @blueprint.route('/feed/') def view_post(post_id: int): post = Post.query.get_or_404(post_id) - comments = post.comments - comment_authors = [User.query.get_or_404(comment.author) for comment in comments] - return render_template('pages/post.html', post=post, author=User.query.get_or_404(post.author), - comments_and_authors=zip(comments, comment_authors)) + return render_template('pages/post.html', form=NewCommentForm(), post=post) # @blueprint.route('/messages') @@ -79,7 +75,7 @@ def edit_user(username: str): if request.method == 'POST': if form.validate(): - if current_user.is_admin or current_user.id == user.id: + if current_user.is_admin or current_user == user: user.about_me = form.about_me.data user.name = form.name.data diff --git a/templates/pages/feed.html b/templates/pages/feed.html index 520a131..e350b4b 100644 --- a/templates/pages/feed.html +++ b/templates/pages/feed.html @@ -4,6 +4,7 @@ {% block content %} {% if current_user.is_authenticated %}
+ {{ form.csrf_token }} {{ render_field(form.text, show_label=False) }}
@@ -11,13 +12,15 @@
- {% for post, author in posts_and_authors %} + {% for post in posts %}
{{ post.text }}
{% endfor %} diff --git a/templates/pages/post.html b/templates/pages/post.html index 324a39c..2867562 100644 --- a/templates/pages/post.html +++ b/templates/pages/post.html @@ -4,33 +4,25 @@ {{ post.text }}
- Posted by {{ author.name }} Posted by {{ post.author.name }} {{ post.get_time_ago() }} ago.
{% if current_user.is_authenticated %} - {% with messages = get_flashed_messages() %} - {% if messages %} - - {{ messages[0] }} - - {% endif %} - {% endwith %} -
- - + {{ form.csrf_token }} + {{ form.text }} +
{% endif %} - {% for comment, author in comments_and_authors %} + {% for comment in post.comments %}
- - "{{ comment.text }}"{{ author.name }} + "{{ comment.text }}" — + {{ comment.author.name }} +
{% endfor %}
diff --git a/templates/pages/user.html b/templates/pages/user.html index ffba627..9af2bbb 100644 --- a/templates/pages/user.html +++ b/templates/pages/user.html @@ -3,7 +3,7 @@ {% block content %}
{{ user.name }} - {% if current_user.is_admin or current_user.id == user.id %} + {% if current_user.is_admin or current_user == user %} {% endif %} {% with seen_text = user.get_last_seen() %} @@ -22,7 +22,7 @@ Registered {{ user.get_registration_delta() }} ago
{# 0 likes
#} {% with post_count = user.get_post_count() %} - {{ post_count }} post{% if post_count > 1 %}s{% endif %}
+ {{ post_count }} post{{ post_count|pluralize() }}
{% endwith %}