def comment_pin(pin_id): if g.user_id: content = request.form['content'] user = User.objects(id=g.user_id).first() pin = Pin.objects(id=pin_id).first() comment = Comment(content=content, author=user, pin=pin, create_at=datetime.utcnow()) comment.save() if pin.comments_count == 0: pin.first_comment = content pin.first_comment_user = user pin.first_comment_create_at = datetime.utcnow() pin.save() elif pin.comments_count >= 1: pin.last_comment = content pin.last_comment_user = user pin.last_comment_create_at = datetime.utcnow() pin.save() pin.update(inc__comments_count=1) comment_item = {} comment_item['comment_id'] = str(comment.id) comment_item['content'] = comment.content comment_item['author_id'] = str(comment.author.id) comment_item['author_name'] = comment.author.nickname comment_item['avatar'] = comment.author.avatar comment_item['create_at'] = comment.create_at.strftime('%Y-%m-%d %H:%M:%S') return (json.dumps(comment_item), 200) return ('comment pin session timeout', 400)
def del_comment(comment_id): if g.user_id: comment = Comment.objects(id=comment_id).first() comment.pin.update(inc__comments_count=-1) comment.delete() return ('del comment success', 200) return ('del comment session timeout', 400)
def comments_user(user_id, page_num): if g.user_id: limit = 5 start = (page_num - 1) * limit end = page_num * limit comments = Comment.objects(author=user_id)[start:end].order_by('-create_at') res_data = comments_pack(comments) return (json.dumps(res_data), 200) return ('comments mine session timeout', 400)