def test_nested_post_comments_model(self): self.session.begin_nested() parent = PostComment(post_id=self.post.id, text="this is the parent comment", user_id=self.user.id) child_a = PostComment(post_id=self.post.id, text="this is a child comment", user_id=self.user.id, parent_id=parent.id) child_b = PostComment(post_id=self.post.id, text="this is b child comment", user_id=self.user.id, parent_id=parent.id) child_c = PostComment(post_id=self.post.id, text="this is c child comment", user_id=self.user.id, parent_id=child_a.id) parent.children.append(child_a) parent.children.append(child_b) child_a.children.append(child_c) self.session.add(parent) self.session.add(child_a) self.session.add(child_b) self.session.add(child_c) self.session.commit() comment_tree = parent.comment_tree() test_tree = [parent, [[child_a, [child_c]], child_b]] self.assertSequenceEqual(comment_tree, test_tree)
def test_post_comment_model(self): self.session.begin_nested() current_post_comment_count = len(self.post.comments) current_user_comment_count = len(self.user.post_comments) test_comment = PostComment(post_id=self.post.id, text="foobar", user_id=self.user.id) self.user.post_comments.append(test_comment) self.post.comments.append(test_comment) self.assertEqual(test_comment.user.id, self.user.id) self.assertEqual(test_comment.text, "foobar") self.assertEqual(test_comment.post_id, self.post.id) self.assertEqual(len(self.post.comments)-current_post_comment_count, 1) self.assertEqual(len(self.user.post_comments)-current_user_comment_count, 1)
def create_post_comment(post_id, parent_comment, comment_text): if g.user is None: return abort(403) if not comments["min_len"] <= len(comment_text) <= comments["max_len"]: return abort(400) post = Post.query.filter_by(id=post_id).first() new_comment = PostComment(post_id=post_id, parent_id=parent_comment, text=comment_text, user_id=g.user.id) post.comments.append(new_comment) g.user.post_comments.append(new_comment) db.session.add(new_comment) db.session.commit() return new_comment
def create_post_comment(post_id, parent_comment, comment_text): if g.user is None: return abort(403) if not comments["min_len"] <= len(comment_text) <= comments["max_len"]: return abort(400) post = Post.query.filter_by(id=post_id).first() new_comment = PostComment(post_id=post_id, parent_id=parent_comment, text=comment_text, user_id=g.user.id) post.comments.append(new_comment) g.user.post_comments.append(new_comment) db.session.add(new_comment) db.session.commit() # Get the users that we should send notification to comments_to_notify = get_comment_notification_targets(new_comment) for user_id, comment in comments_to_notify.items(): # Dont notify user if: # - is the owner of the post # - is the owner of the new comment if user_id in (post.user_id, new_comment.user_id): continue send_notification( Notification(sender_id=new_comment.user_id, target_id=new_comment.id, recipient_id=user_id, source_id=comment.id, notification_type=NotificationType.NEW_POST_COMMENT)) # Notify the owner of the post IF it isn't the same user who posted the # current comment if post.user_id != new_comment.user_id: send_notification( Notification(sender_id=new_comment.user_id, target_id=new_comment.id, recipient_id=post.user_id, source_id=None, notification_type=NotificationType.NEW_POST_COMMENT)) return new_comment