-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ | |
current_app, make_response | ||
from flask_login import login_required, current_user | ||
from . import main | ||
from .forms import EditProfileForm, EditProfileAdminForm, PostForm | ||
from .forms import EditProfileForm, EditProfileAdminForm, PostForm,\ | ||
CommentForm | ||
from .. import db | ||
from ..models import Permission, Role, User, Post | ||
from ..models import Permission, Role, User, Post, Comment | ||
from ..decorators import admin_required, permission_required | ||
|
||
|
||
|
@@ -91,10 +92,28 @@ def edit_profile_admin(id): | |
return render_template('edit_profile.html', form=form, user=user) | ||
|
||
|
||
@main.route('/post/<int:id>') | ||
@main.route('/post/<int:id>', methods=['GET', 'POST']) | ||
def post(id): | ||
post = Post.query.get_or_404(id) | ||
return render_template('post.html', posts=[post]) | ||
form = CommentForm() | ||
if form.validate_on_submit(): | ||
comment = Comment(body=form.body.data, | ||
post=post, | ||
author=current_user._get_current_object()) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
miguelgrinberg
Author
Owner
|
||
db.session.add(comment) | ||
db.session.commit() | ||
flash('Your comment has been published.') | ||
return redirect(url_for('.post', id=post.id, page=-1)) | ||
page = request.args.get('page', 1, type=int) | ||
if page == -1: | ||
page = (post.comments.count() - 1) // \ | ||
current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 | ||
pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( | ||
page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'], | ||
error_out=False) | ||
comments = pagination.items | ||
return render_template('post.html', posts=[post], form=form, | ||
comments=comments, pagination=pagination) | ||
|
||
|
||
@main.route('/edit/<int:id>', methods=['GET', 'POST']) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ class User(UserMixin, db.Model): | |
backref=db.backref('followed', lazy='joined'), | ||
lazy='dynamic', | ||
cascade='all, delete-orphan') | ||
comments = db.relationship('Comment', backref='author', lazy='dynamic') | ||
|
||
@staticmethod | ||
def add_self_follows(): | ||
|
@@ -264,6 +265,7 @@ class Post(db.Model): | |
body_html = db.Column(db.Text) | ||
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) | ||
author_id = db.Column(db.Integer, db.ForeignKey('users.id')) | ||
comments = db.relationship('Comment', backref='post', lazy='dynamic') | ||
|
||
@staticmethod | ||
def on_changed_body(target, value, oldvalue, initiator): | ||
|
@@ -275,3 +277,24 @@ def on_changed_body(target, value, oldvalue, initiator): | |
tags=allowed_tags, strip=True)) | ||
|
||
db.event.listen(Post.body, 'set', Post.on_changed_body) | ||
|
||
|
||
class Comment(db.Model): | ||
__tablename__ = 'comments' | ||
id = db.Column(db.Integer, primary_key=True) | ||
body = db.Column(db.Text) | ||
body_html = db.Column(db.Text) | ||
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) | ||
disabled = db.Column(db.Boolean) | ||
author_id = db.Column(db.Integer, db.ForeignKey('users.id')) | ||
post_id = db.Column(db.Integer, db.ForeignKey('posts.id')) | ||
|
||
@staticmethod | ||
def on_changed_body(target, value, oldvalue, initiator): | ||
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i', | ||
'strong'] | ||
target.body_html = bleach.linkify(bleach.clean( | ||
markdown(value, output_format='html'), | ||
tags=allowed_tags, strip=True)) | ||
|
||
db.event.listen(Comment.body, 'set', Comment.on_changed_body) | ||
This comment has been minimized.
Sorry, something went wrong.
ezebunandu
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<ul class="comments"> | ||
{% for comment in comments %} | ||
<li class="comment"> | ||
<div class="comment-thumbnail"> | ||
<a href="{{ url_for('.user', username=comment.author.username) }}"> | ||
<img class="img-rounded profile-thumbnail" src="{{ comment.author.gravatar(size=40) }}"> | ||
</a> | ||
</div> | ||
<div class="comment-content"> | ||
<div class="comment-date">{{ moment(comment.timestamp).fromNow() }}</div> | ||
<div class="comment-author"><a href="{{ url_for('.user', username=comment.author.username) }}">{{ comment.author.username }}</a></div> | ||
<div class="comment-body"> | ||
{% if comment.body_html %} | ||
{{ comment.body_html | safe }} | ||
{% else %} | ||
{{ comment.body }} | ||
{% endif %} | ||
</div> | ||
</div> | ||
</li> | ||
{% endfor %} | ||
</ul> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
{% extends "base.html" %} | ||
{% import "bootstrap/wtf.html" as wtf %} | ||
{% import "_macros.html" as macros %} | ||
|
||
{% block title %}Flasky - Post{% endblock %} | ||
|
||
{% block page_content %} | ||
{% include '_posts.html' %} | ||
<h4 id="comments">Comments</h4> | ||
{% if current_user.can(Permission.COMMENT) %} | ||
<div class="comment-form"> | ||
{{ wtf.quick_form(form) }} | ||
</div> | ||
{% endif %} | ||
{% include '_comments.html' %} | ||
{% if pagination %} | ||
<div class="pagination"> | ||
{{ macros.pagination_widget(pagination, '.post', fragment='#comments', id=posts[0].id) }} | ||
</div> | ||
{% endif %} | ||
{% endblock %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""comments | ||
Revision ID: 51f5ccfba190 | ||
Revises: 2356a38169ea | ||
Create Date: 2014-01-01 12:08:43.287523 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '51f5ccfba190' | ||
down_revision = '2356a38169ea' | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
def upgrade(): | ||
### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('comments', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('body', sa.Text(), nullable=True), | ||
sa.Column('body_html', sa.Text(), nullable=True), | ||
sa.Column('timestamp', sa.DateTime(), nullable=True), | ||
sa.Column('disabled', sa.Boolean(), nullable=True), | ||
sa.Column('author_id', sa.Integer(), nullable=True), | ||
sa.Column('post_id', sa.Integer(), nullable=True), | ||
sa.ForeignKeyConstraint(['author_id'], ['users.id'], ), | ||
sa.ForeignKeyConstraint(['post_id'], ['posts.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_index('ix_comments_timestamp', 'comments', ['timestamp'], unique=False) | ||
### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index('ix_comments_timestamp', 'comments') | ||
op.drop_table('comments') | ||
### end Alembic commands ### |
1 comment
on commit a6f626f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is helpful. I will ise it to update my post
Should the arguments passed to the Comment class here not be the same as those we defined in the class constructor (i.e. post_id, and author_id)?