def view(self): year, month, day, route = self._get_route_args() post = Post.by_route(route) if post is None: return HTTPNotFound("No such post") form = BlogView.CommentForm(self.request.POST) if self.request.method == 'POST' and form.validate(): if form.parent.data == '': post_comment = PostComment() post_comment.comment = Comment(form.name.data, form.email.data, form.body.data) post.comments.append(post_comment) else: parent_id = int(form.parent.data) parent_comment = Comment.by_id(parent_id) parent_comment.childs.append( Comment( form.name.data, form.email.data, form.body.data, parent_id, ) ) return HTTPFound( location=self.request.route_url('post_view', year=year, month=month, day=day, route=RouteAbleMixin.url_quote(post.title)) ) return dict( form=form, post=post, pages=Page.all(), logged_in=authenticated_userid(self.request), )
def add_comment(request): if request.method == "POST": comment = request.POST['comment'] post = request.POST['post_id'] new_comment = PostComment( user = request.user, post = Post.objects.get(id=post), comment = comment, ) #return HttpResponse(json.dumps(post)) new_comment.save() return HttpResponse(True) else: return HttpResponse("Error")
def get(self, c_id): c = PostComment.by_id(int(c_id)) p_id = c.post_id p = Post.by_id(p_id) c.delete() p.comment.remove(int(c_id)) p.put() time.sleep(0.5) self.redirect('/blog/{}'.format(p_id))
def get_posts(): posts = [p.as_dict() for p in Post.query.order_by(desc(Post.id)).all()] for p in posts: p["user"] = User.by_id(p["user_id"]).as_dict() like_users = PostLike.user_ids(p["id"]) p["num_of_likes"] = len(like_users) p["liked"] = (request.user.id in like_users) p["num_of_comments"] = len(PostComment.comments(p["id"])) return jsonify(posts)
def remove_comment(comment_id): post_comment = PostComment.by_id(comment_id) if not post_comment: return make_response(jsonify({"message": "Not Found."}), 404) if post_comment.user_id != request.user.id: return make_response(jsonify({"message": "Not Found."}), 404) db.session.delete(post_comment) db.session.commit() return make_response(jsonify({"message": "Deleted."}), 200)
def submit_comment_reply(): message = None if request.method == 'POST': isAnonym = True data = json.loads(request.data) commentText = data['replyText'] inReplyTo = int(data['id']) commentorId = 0 postId = int(data['postId']) if Shared.isLoggedIn(): #return redirect(url_for('index')) user = Shared.getLoggedInUser() isAnonym = False name = user.name email = user.email commentorId = user.id else: name = 'Anonym' email = '' commentorId = 0 pc = PostComment() pc.commentorEmail = email pc.commentorName = name pc.commentText = commentText pc.commentorId = commentorId pc.inReplyTo = inReplyTo pc.postId = postId pc.commentDate = Common.getCurrentTimeMil() db.session.add(pc) db.session.commit() resp = make_response( render_template('blog/singleComment.html', comment=pc)) return json.dumps({ 'html': resp.data.decode("utf-8"), "id": str(inReplyTo) }) #else: # return render_template('blog/singleComment.html', comment = pc) return ""
def get(self, post_id): msg_error = self.request.get('error') post = Post.get_by_id(int(post_id)) c = [] for comment_id in post.comment: c.append(PostComment.by_id(comment_id)) if self.user: self.render("permalink.html", post=post, username=self.user.name, comment=c, error=msg_error) else: self.render("permalink.html", post=post, comment=c, error=msg_error)
def post(self, c_id): content = self.request.get('content') username = self.user.name c = PostComment.by_id(int(c_id)) p_id = c.post_id if content: c.content = content c.put() time.sleep(0.5) self.redirect('/blog/{}'.format(p_id)) else: error = "Content please!" self.render("edit-comment.html", username=username, error=error)
def post_comment(): form = CommentForm() if request.method == 'POST' and form.validate_on_submit(): user_id = int(request.form['user_id']) post_id = int(request.form['post_id']) comment_text = request.form['comment_text'] else: user_id = int(request.args['user_id']) post_id = int(request.args['post_id']) comment_text = request.args['comment'] comment_text = comment_text.strip() user = User.query.get(user_id) # @UndefinedVariable post = Post.query.get(post_id) # @UndefinedVariable if user and post: comment = PostComment(user_id=user_id, post_id=post_id, comment_text=comment_text) db.session.add(comment) # @UndefinedVariable db.session.flush( ) # Flush db session to generate comment id @UndefinedVariable #Add notification if comment made by user other than post creator if user_id != post.created_by_id: user_notified = post.created_by_id current_notif = Notification.query.filter_by( post_id=post_id, user_id=user_notified, seen=False).count() # @UndefinedVariable #check if unseen notificaiton already exists for this user and post if current_notif == 0: project = Project.query.get(post.project_id) url = project.get_url() notif_msg = user.get_full_name() + " commented on your post" dom_element_id = 'comment' + str(comment.id) notif = Notification(message=notif_msg, user_id=user_notified, url=url, post_id=post_id, dom_element_id=dom_element_id) db.session.add(notif) # @UndefinedVariable db.session.commit() # @UndefinedVariable return '{"success":true,"comment_id":"' + str(comment.id) + '"}' else: return "{'success':false, 'error_msg':'Invalid user or post', 'post_id':" + str( post_id) + ", 'user_id':" + str(user_id) + "}"
def add_comment(post_id): # バリデーション. comment = request.json.get("comment") if not comment: return make_response(jsonify({"message": "comment must be set."}), 400) post = Post.by_id(post_id) if not post: return make_response(jsonify({"message": "Not Found."}), 404) post_comment = PostComment() post_comment.post_id = post_id post_comment.user_id = request.user.id post_comment.comment = comment db.session.add(post_comment) db.session.commit() post_comment.id # データにタッチすることで、主キーを取得. return make_response(jsonify(post_comment.as_dict()), 201)
def post(self, post_id): p = Post.by_id(int(post_id)) content = self.request.get('comment_content') comment = [] username = self.user.name msg_error = "Error: Comment can not be empty!" if content: c = PostComment(author=self.user, content=content, post_id=int(post_id)) c.put() p.comment.append(c.key().id()) p.put() msg_error = None for comment_id in p.comment: comment.append(PostComment.by_id(comment_id)) self.render("permalink.html", post=p, username=username, comment=comment, error=msg_error)
def viewpost(id): if request.method == 'POST': if 'formAddComment' in request.form: name, email = None, None commentorId = 0 if not Shared.isLoggedIn(): name = request.form['commentorName'] email = request.form['commentorEmail'] else: user = Shared.getLoggedInUser() name = user.name email = user.email commentorId = user.id commentText = request.form['commentText'] pc = PostComment() pc.commentorEmail = email pc.commentorName = name pc.commentText = commentText pc.commentorId = commentorId pc.inReplyTo = 0 pc.postId = id pc.commentDate = Common.getCurrentTimeMil() db.session.add(pc) db.session.commit() post = BlogPost.query.filter_by(id=id).first() comments = PostComment.query.filter_by(postId=id) #if comments.count()<=0: # comments = None commentsDict = None commentReplies = None if comments: commentsDict = {comment.id: comment for comment in comments} commentReplies = {} #ht = "" for comment in comments: commentReplies[comment.id] = [] for comment in comments: if comment.inReplyTo == 0: continue commentReplies[comment.inReplyTo].append(comment) #if comment.inReplyTo == 0: # parents.append(comment) # p = 0 # ht+='<div class="row">'+ comment.commentText +'<a href="reply('+str(comment.id)+')" class="replyfont"> Reply </a>'+'</div>' # ht = checkChilds(comment,ht,comments, p) #else: # depths[comment.id] = depths[comment.inReplyTo]+1 return render_template( 'blog/viewpost.html', post=post, comments=comments, commentsDict=commentsDict, commentReplies=commentReplies) # comments = comments, ht = ht)
def get(self, c_id): c = PostComment.by_id(int(c_id)) username = self.user.name content = c.content self.render("edit-comment.html", content=content, username=username)
def get_comment(post_id): comments = [c.as_dict() for c in PostComment.comments(post_id)] for c in comments: c["user"] = User.by_id(c["user_id"]).as_dict() return jsonify(comments)