Skip to content

added comment section #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions blogexample/blueprints/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ def create_blogpost(cls, params):

return True


@classmethod
def create_comment(cls, params):

comm_params = {}
comm_params['username'] = params['username']
comm_params['comment'] = params['comment']
comm_params['user_id'] = params['user_id']


comm_post = Comment(**comm_params)

db.session.add(comm_post)
db.session.commit()

return True


@classmethod
def update_blogpost(cls, params):
"""
Expand Down Expand Up @@ -189,6 +207,16 @@ class Tag(ResourceMixin, db.Model):
tag = db.Column(db.String(64), unique=True)


class Comment(db.Model):

__tablename__ = 'comment'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False)
comment = db.Column(db.String(), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('posts.id'), nullable=False)
posts = db.relationship('Post', backref=db.backref('posts', cascade="all,delete",lazy=True))


# class Post_To_Tag(ResourceMixin, db.Model):
# post = ForeignKeyField(Post)
# tag = ForeignKeyField(Tag)
Expand Down
27 changes: 25 additions & 2 deletions blogexample/blueprints/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy import text

from . import blog
from .models import Post, Tag, post_tags_table
from .models import Post, Tag, post_tags_table, Comment
from .forms import AddPostForm, UpdatePostForm


Expand All @@ -21,7 +21,8 @@ def show_posts():
# flash('User is not Authenticated')
# return redirect(url_for('index'))
posts = Post.query.all()
return render_template('posts.html', posts=posts)
comment = Comment.query.all()
return render_template('posts.html', posts=posts, comment=comment)

@blog.route('/blog')
def published():
Expand Down Expand Up @@ -55,6 +56,28 @@ def add_post():
return render_template('add.html', form=form, blogpost=blogpost)


@blog.route('/posts', methods=['GET', 'POST'])
def add_comment():

if request.method == 'POST':
username = request.form.get('username')
message = request.form.get('comment')
user_id = request.form.get('id')

params = {
'username': username,
'comment': message,
'user_id' : user_id
}

if Post.create_comment(params):
flash('Your comment has submitted.', 'success')
return redirect(url_for('blog.show_posts'))


return render_template('posts.html')


@blog.route('/detail/<url>')
def detail(url):
#####WORKING
Expand Down
66 changes: 66 additions & 0 deletions blogexample/templates/posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#signout a{
padding: 2px 0px 2px 20px;
}
textarea {
width: 100%;
}
input {
width: 100%;
}
</style>
</head>
<body>
Expand Down Expand Up @@ -68,6 +74,66 @@ <h3 class="flashes" align="center" style="color:#337ab7">{{ message }}</h3>
<p>Published: {{ each_post.visible }}</p>
<p>Last updated: {{ each_post.updated_on.strftime('%m/%d/%Y') }}</p>
</div>


<div class="comment-form">
<form method="POST">
<div class="form-group">
<input
type="text"
class="comment-form"
name="username"
v-model="username"
placeholder="Enter username">
</div>
<div class="form-group">
<textarea
class="comment-form"
name="comment"
v-model="comment"
rows="3"></textarea>
</div>
<input
type="hidden"
class="comment-form"
name="id"
value = {{ each_post.id }}>

<button type="submit" class="btn btn-primary btn-block">Add comment</button>
</form>
</div>



<div class="panel-heading">
<span class="glyphicon glyphicon-comment"></span>
<h3 class="panel-title">
Recent Comments</h3>
</div>
<div class="panel-body">
<ul class="list-group">
{% for each_comm in comment %}
{% if each_comm.user_id==each_post.id %}
<li class="list-group-item">
<div class="row">
<div class="col-xs-2 col-md-1">
<img src="http://placehold.it/80" class="img-circle img-responsive" alt="" /></div>
<div class="col-xs-10 col-md-11">
<div class="mic-info">
By: <a href="#">{{ each_comm.username }}</a>
</div>
<div class="comment-text">
<p>{{ each_comm.comment }}</p>
</div>
</div>
</div>
</li>
{% endif %}
{% endfor %}
</ul>
</div>


</div>
{% endfor %}
</div>
Expand Down