def route_create_comment(post_id: str): uid = request.headers.get("uid", None) if not uid: abort(401) # if exists, create a comment as a sub comment comment_id = request.form.get("comment_id", None) comment = request.form.get("comment", "") post = Post.objects.get_or_404(id=post_id) user = User.objects.get_or_404(uid=uid) comment_to_create = Comment( user=user, comment=comment, sub_comments=[], created_at=pendulum.now().int_timestamp).save() if comment_id: comment = next( (comment for comment in post.comments if str(comment.id) == comment_id), None) if not comment: raise ValueError("Not found an appropriate comment.") comment_to_update = Comment.objects.get_or_404(id=comment_id) comment_to_update.update(push__sub_comments=comment_to_create) else: post.update(push__comments=comment_to_create) return Response(comment_to_create.to_json(follow_reference=True, max_depth=1), mimetype="application/json")
def test_insert_an_comments(self): post = Post.objects.order_by("-created_at").first() user = User.objects.first() comment = Comment( user=user, comment="아무말이나 적어 놉니다 3.", created_at=pendulum.now().int_timestamp, favorite=False ) comment.save() post.update(push__comments=comment) post.save()
def add_comment(db_session, article_id, comment): max_floor = CommentService.get_max_floor(db_session, article_id) floor = max_floor + 1 comment_to_add = Comment(content=comment['content'], author_name=comment['author_name'], author_email=comment['author_email'], article_id=article_id, comment_type=comment['comment_type'], rank=comment['rank'], floor=floor, reply_to_id=comment['reply_to_id'], reply_to_floor=comment['reply_to_floor']) db_session.add(comment_to_add) db_session.commit() return comment_to_add
def comment_reply(): """Comment Reply Action. Used for reply guests comment and send notification email. Methods: POST Args: author: guest_name did: diaryObjectID title: diary_title email: guest_email content: reply content Returns: status: {success: true/false , reason: Exception} """ if request.method == 'POST': author = request.form['author'] did = request.form['did'] title = request.form['title'] email = request.form['email'] content = request.form['content'] post = Diary.objects(pk=did) commentEm = CommentEm( author=u'博主回复', content=content, ) post.update_one(push__comments=commentEm) ''' Save in Comment model for admin manage''' comment = Comment(content=content) comment.diary = post[0] comment.author = current_user.name comment.save(validate=False) try: send_email_task( email, u'您评论的文章《' + title + u'》收到了来自\ 博主的回复, 请查收', content, did, author, title) return json.dumps({'success': 'true'}) except Exception as e: return json.dumps({'success': 'false', 'reason': str(e)})
def comment_add(): """ Comment Add AJAX Post Action. designed for ajax post and send reply email for admin Args: username: guest_name did: diary ObjectedId email: guest_email content: comment content Return: email_status: success """ if request.method == 'POST': name = request.form['username'] did = request.form['did'] email = request.form['email'] content = request.form['comment'] post = Diary.objects(pk=did) diary_title = post[0].title commentEm = CommentEm(author=name, content=content, email=email) post.update_one(push__comments=commentEm) comment = Comment(content=content) comment.diary = post[0] comment.email = email comment.author = name comment.save(validate=False) try: send_email_task(Config.EMAIL, Config.MAIN_TITLE + u'收到了新的评论, 请查收', content, did, name, diary_title) response = make_response(json.dumps({'success': 'true'})) response.set_cookie('guest_name', name) response.set_cookie('guest_email', email) return response except Exception as e: return str(e)
def route_create_comment(post_id: str): uid = request.headers.get("uid", None) if not uid: abort(401) # if exists, create a comment as a sub comment comment_id = request.form.get("comment_id", None) # parent_comment_id comment = request.form.get("comment", "") post = Post.objects.get_or_404(id=post_id) user = User.objects.get_or_404(uid=uid) comment_to_create = Comment( post_id=post_id, user_id=user.id, comment=comment, comments=[], # child comments created_at=pendulum.now().int_timestamp, is_deleted=False).save() post.add_comment(comment_to_create, parent_id=comment_id) alarm = Alarm.create_alarm( user_from=user, user_to=post.author, event=Alarm.Event.COMMENT, post=post, comment=comment_to_create, message="{nickname} 님이 당신의 게시물에 댓글을 남겼습니다.".format( nickname=user.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, post.author) comment = comment_to_create.to_mongo() comment["commenter"] = User.get(id=user.id).to_mongo() response = encode(comment) return Response(response, mimetype="application/json")
def reply_comment(self, author, diary_id, email, content): diary = Diary.objects(pk=diary_id) diary_title = diary.first().title comment_em = CommentEm( author=u'博主回复', content=content, ) diary.update_one(push__comments=comment_em) ''' Save in Comment model for admin manage''' comment = Comment(content=content) comment.diary = diary.first() comment.author = UserDispatcher().get_profile().name comment.save(validate=False) try: send_email_task( email, u'您评论的文章《' + diary_title + u'》收到了来自\ 博主的回复, 请查收', content, diary_id, author, diary_title) return json.dumps({'success': 'true'}) except Exception as e: return json.dumps({'success': 'false', 'reason': str(e)})
def add_comment(self, author, diary_id, email, content): diary = Diary.objects(pk=diary_id) diary_title = diary.first().title comment_em = CommentEm(author=author, content=content, email=email) diary.update_one(push__comments=comment_em) comment = Comment(content=content) comment.diary = diary.first() comment.email = email comment.author = author comment.save(validate=False) try: send_email_task(Config.EMAIL, Config.MAIN_TITLE + u'收到了新的评论, 请查收', content, diary_id, author, diary_title) response = make_response(json.dumps({'success': 'true'})) response.set_cookie('guest_name', author) response.set_cookie('guest_email', email) return response except Exception as e: return str(e)
def add_data(): Course.objects.all().delete() Faculty.objects.all().delete() Professor.objects.all().delete() Comment.objects.all().delete() LikeModel.objects.all().delete() course_arr = [] prof_arr = [] fac_arr = [] like_arr = [] for i in range(N): length = random.randint(10, 50) course_name = random.sample(set(['Введение в', 'Рассказ о', 'Начало'])) + random.sample(set(SUBJECT), 1) +unicode(i) lector_name = 'профессор' + randoms.sample(set(['йцукенгшщзхъфывапролджэячсмитьбю '], 15)) + unicode(i) faculty_name = 'факультет ' + unicode(i) faculty = Faculty( name = faculty_name) professor = Professor( name = lector_name, faculty = faculty) object_id = random.randint(1, i -1) course = Course(title = name, professor = professor, faculty = faculty) like_arr.append(Like(content_type = random.sample(set(['Faculty', 'Professor', 'Course']))), object_id = object_id) content = randoms.sample(set(['йцукенгшщзхъфывапролджэячсмитьбю '], 50)) + random.sample(set(SUBJECT), 1) +unicode(i) comm_arr.append(Comment(content = content, content_type = random.sample(set(['Faculty', 'Professor', 'Course'])), object_id = object_id)) fac_arr.append(faculty) prof_arr.append(professor) course_arr.append(obj) Faculty.objects.bulk_create(fac_arr) Professor.objects.bulk_create(prof_arr) Course.objects.bulk_create(course_arr) Comment.objects.bulk_create(comm_arr) LikeModel.objects.bulk_create(like_arr)