From f401ed442a210351f6f495b54121e5c89276f46f Mon Sep 17 00:00:00 2001 From: Abid Saleem Date: Thu, 9 Jul 2020 23:29:26 +0530 Subject: [PATCH] added comment section --- blogexample/blueprints/blog/models.py | 28 ++++++++++++ blogexample/blueprints/blog/views.py | 27 ++++++++++- blogexample/templates/posts.html | 66 +++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/blogexample/blueprints/blog/models.py b/blogexample/blueprints/blog/models.py index 81ce426..a04128b 100644 --- a/blogexample/blueprints/blog/models.py +++ b/blogexample/blueprints/blog/models.py @@ -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): """ @@ -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) diff --git a/blogexample/blueprints/blog/views.py b/blogexample/blueprints/blog/views.py index c58ca71..8b4caee 100644 --- a/blogexample/blueprints/blog/views.py +++ b/blogexample/blueprints/blog/views.py @@ -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 @@ -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(): @@ -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/') def detail(url): #####WORKING diff --git a/blogexample/templates/posts.html b/blogexample/templates/posts.html index 5e3b162..04f6ebf 100644 --- a/blogexample/templates/posts.html +++ b/blogexample/templates/posts.html @@ -37,6 +37,12 @@ #signout a{ padding: 2px 0px 2px 20px; } + textarea { + width: 100%; + } + input { + width: 100%; + } @@ -68,6 +74,66 @@

{{ message }}

Published: {{ each_post.visible }}

Last updated: {{ each_post.updated_on.strftime('%m/%d/%Y') }}

+ + +
+
+
+ +
+
+ +
+ + + +
+
+ + + +
+ +

+ Recent Comments

+
+
+
    + {% for each_comm in comment %} + {% if each_comm.user_id==each_post.id %} +
  • +
    +
    +
    +
    + +
    +

    {{ each_comm.comment }}

    +
    +
    +
    +
  • + {% endif %} + {% endfor %} +
+
+ + {% endfor %}