mirror of
https://github.com/Xevion/runnerspace.git
synced 2025-12-07 16:08:28 -06:00
Complete feed view, post view, and post form
This commit is contained in:
21
forms.py
21
forms.py
@@ -2,7 +2,7 @@ from flask import Blueprint, flash, redirect, request, url_for
|
||||
from flask_login import current_user, login_required
|
||||
|
||||
from .create_app import db
|
||||
from .models import User
|
||||
from .models import User, Post, Comment
|
||||
|
||||
blueprint = Blueprint('forms', __name__)
|
||||
|
||||
@@ -22,3 +22,22 @@ def edit_profile_post(username):
|
||||
|
||||
flash('Successfully updated profile.')
|
||||
return redirect(url_for('main.edit_user', username=username))
|
||||
|
||||
|
||||
@blueprint.route('/feed/new', methods=['POST'])
|
||||
@login_required
|
||||
def new_post():
|
||||
post_text = request.form.get('text')
|
||||
|
||||
if len(post_text) < 15:
|
||||
flash('Must have at least 15 characters of text.')
|
||||
return redirect(url_for('forms.new_post'))
|
||||
elif len(post_text) > 1000:
|
||||
flash('Cannot have more than 1000 characters of text.')
|
||||
return redirect(url_for('forms.new_post'))
|
||||
|
||||
post = Post(author=current_user.id, text=post_text)
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for('main.view_post', post_id=post.id))
|
||||
|
||||
14
routes.py
14
routes.py
@@ -1,7 +1,7 @@
|
||||
from flask import Blueprint, redirect, render_template, url_for
|
||||
from flask_login import current_user, login_required
|
||||
|
||||
from .models import User
|
||||
from .models import User, Post, Comment
|
||||
|
||||
blueprint = Blueprint('main', __name__)
|
||||
|
||||
@@ -24,7 +24,17 @@ def browse():
|
||||
|
||||
@blueprint.route('/feed')
|
||||
def feed():
|
||||
return render_template('pages/feed.html')
|
||||
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))
|
||||
|
||||
|
||||
@blueprint.route('/feed/<post_id>')
|
||||
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))
|
||||
|
||||
|
||||
@blueprint.route('/messages')
|
||||
|
||||
@@ -234,4 +234,38 @@ form button {
|
||||
margin: 0.5em 0;
|
||||
}
|
||||
|
||||
.post-box {
|
||||
border: 1px solid grey;
|
||||
border-radius: 3px;
|
||||
padding: 1.5em;
|
||||
padding-bottom: 0.8em;
|
||||
margin: 0.45em;
|
||||
}
|
||||
.post-box .post-author {
|
||||
margin-top: 1em;
|
||||
font-size: 0.8em;
|
||||
border-bottom: 1px solid grey;
|
||||
padding-bottom: 0.8em;
|
||||
}
|
||||
.post-box .post-author.no-border {
|
||||
border: none;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
.post-box .post-comments {
|
||||
padding-left: 2em;
|
||||
font-size: 0.8em;
|
||||
}
|
||||
.post-box .post-comments .post-comment {
|
||||
width: fit-content;
|
||||
margin: 0.4em;
|
||||
padding: 0.3em;
|
||||
border: 1px solid rgba(128, 128, 128, 0.51);
|
||||
border-radius: 3px;
|
||||
}
|
||||
.post-box .post-comments .post-comment .comment-author {
|
||||
font-size: 0.9em;
|
||||
vertical-align: center;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
/*# sourceMappingURL=styles.css.map */
|
||||
|
||||
@@ -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;;;AAON;EACE;EACA;EACA;EACA;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;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;;AAEA;EACE;EACA;;AAGF;EACE;;AAEA;EACE;;AAIJ;EACE;EACA;EACA;;;AAIJ;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","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;;;AAON;EACE;EACA;EACA;EACA;EACA;;;AAGF;EACE;EACA;EACA;;AAEA;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;;AAEA;EACE;EACA;;AAGF;EACE;;AAEA;EACE;;AAIJ;EACE;EACA;EACA;;;AAIJ;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;EACE;;AAEF;EACE;EACA;EACA;EACA;;AAEA;EACE;EACA;;AAIJ;EACE;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;;AAEA;EAEE;EACA;EACA","file":"styles.css"}
|
||||
@@ -272,3 +272,43 @@ form {
|
||||
margin: 0.5em 0;
|
||||
}
|
||||
}
|
||||
|
||||
.post-box {
|
||||
border: 1px solid grey;
|
||||
border-radius: 3px;
|
||||
padding: 1.5em;
|
||||
padding-bottom: 0.8em;
|
||||
margin: 0.45em;
|
||||
|
||||
.post-author {
|
||||
margin-top: 1em;
|
||||
font-size: 0.8em;
|
||||
border-bottom: 1px solid grey;
|
||||
padding-bottom: 0.8em;
|
||||
|
||||
&.no-border {
|
||||
border: none;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.post-comments {
|
||||
padding-left: 2em;
|
||||
font-size: 0.8em;
|
||||
|
||||
.post-comment {
|
||||
width: fit-content;
|
||||
margin: 0.4em;
|
||||
padding: 0.3em;
|
||||
border: 1px solid rgba(128, 128, 128, 0.51);
|
||||
border-radius: 3px;
|
||||
|
||||
.comment-author {
|
||||
//margin-left: 1em;
|
||||
font-size: 0.9em;
|
||||
vertical-align: center;
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,31 @@
|
||||
{% extends 'layouts/index.html' %}
|
||||
|
||||
{% block content %}
|
||||
{% if current_user.is_authenticated %}
|
||||
<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>
|
||||
|
||||
{% with messages = get_flashed_messages() %}
|
||||
{% if messages %}
|
||||
<div class="notification is-danger">
|
||||
{{ messages[0] }}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
{% endif %}
|
||||
|
||||
{% for post, author in posts_and_authors %}
|
||||
<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
|
||||
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>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endblock content %}
|
||||
|
||||
5
templates/pages/new_post.html
Normal file
5
templates/pages/new_post.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{% extends 'layouts/index.html' %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
{% endblock %}
|
||||
20
templates/pages/post.html
Normal file
20
templates/pages/post.html
Normal file
@@ -0,0 +1,20 @@
|
||||
{% extends 'layouts/index.html' %}
|
||||
{% block content %}
|
||||
<div class="post-box">
|
||||
{{ post.text }}
|
||||
<br>
|
||||
<div class="post-author">
|
||||
<em>Posted by <a href="{{ url_for('main.user', username=author.username) }}">{{ author.name }}</a></em> <span
|
||||
title="{{ post.date_posted }}">{{ post.get_time_ago() }} ago</span>.
|
||||
</div>
|
||||
<div class="post-comments">
|
||||
{% for comment, author in comments_and_authors %}
|
||||
<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>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user