def test_nested_answer_comments_model(self): self.session.begin_nested() parent = AnswerComment(answer_id=self.answer.id, text="this is the parent comment", user_id=self.commentor.id) child_a = AnswerComment(answer_id=self.answer.id, text="this is a child comment", user_id=self.commentor.id, parent_id=parent.id) child_b = AnswerComment(answer_id=self.answer.id, text="this is b child comment", user_id=self.commentor.id, parent_id=parent.id) child_c = AnswerComment(answer_id=self.answer.id, text="this is c child comment", user_id=self.commentor.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 create_answer_comment(answer_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) answer = Answer.query.filter_by(id=answer_id).first() new_comment = AnswerComment(answer_id=answer_id, parent_id=parent_comment, text=comment_text, user_id=g.user.id) answer.comments.append(new_comment) g.user.answer_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 (because we will try to notify this ) # person in a later section of code # - is the owner of the new comment if user_id in (answer.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_ANSWER_COMMENT)) # Notify the owner of the post IF it isn't the same user who posted the # current comment if answer.user_id != new_comment.user_id: send_notification( Notification( sender_id=new_comment.user_id, target_id=new_comment.id, recipient_id=answer.user_id, source_id=None, notification_type=NotificationType.NEW_ANSWER_COMMENT)) return new_comment
def test_answer_comment_model(self): self.session.begin_nested() current_answer_comment_count = len(self.answer.comments) current_user_comment_count = len(self.user.answer_comments) test_comment = AnswerComment(answer_id=self.answer.id, text="foobar", user_id=self.user.id) self.user.answer_comments.append(test_comment) self.answer.comments.append(test_comment) self.assertEqual(test_comment.user.id, self.user.id) self.assertEqual(test_comment.text, "foobar") self.assertEqual(test_comment.answer_id, self.answer.id) self.assertEqual(len(self.answer.comments)-current_answer_comment_count, 1) self.assertEqual(len(self.user.answer_comments)-current_user_comment_count, 1)
def create_answer_comment(answer_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) answer = Answer.query.filter_by(id=answer_id).first() new_comment = AnswerComment(answer_id=answer_id, parent_id=parent_comment, text=comment_text, user_id=g.user.id) answer.comments.append(new_comment) g.user.answer_comments.append(new_comment) db.session.add(new_comment) db.session.commit() return new_comment