def post(self): args = parser.parse_args() content = args['content'] resource_id = args['resource_id'] username = args['author_id'] comment_model = CommentModel(content=content, resource_id=resource_id) comment_model.author = UserModel.query.filter_by(username=username).first() db.session.add(comment_model) db.session.commit() return 'ok', 200
def comment(event_id, message): """ Comments an event Paramters --------- event_id: the id of the event to comment message: the comment gived to the event """ com = CommentModel(event_id, message) try: com.save() return "Your comment is saved." except: return "No event with the id {} founded".format(event_id)
def comment(comment_id): comments= CommentsModel.get_by_id(comment_id) if request.method == "PUT" and users.is_current_user_admin(): print request.form if request.form.get('index'): comments.comments.remove(comments.comments[int(request.form.get('index'))]) elif request.method == "DELETE" and users.is_current_user_admin(): comments.comments=[]; else: comment = CommentModel() comment.comment = request.form.get('comment') comment.author = request.form.get('author') comment.ip = request.remote_addr print request.remote_addr comments.comments.append(comment) comments.put() return redirect(request.referrer)
def post(self, id): data = parser.parse_args() user_id = actual_user_id() if is_authentified() != True: return {"message": "Unauthorized"}, 401 result = UserModel.get_user_by_id(user_id) if result and is_user_connected(user_id): # TODO : Rajouter une condition pour check que la vidéo existe new_comment = CommentModel(body=data['body'], user_id=user_id, video_id=id) try: new_comment.save_to_db() return {'message': 'OK', 'data': data['body']}, 201 except: return { 'message': 'Bad Request', 'code': '3001 => Video doesn\'t exist', 'data': data['body'] }, 400
def add_comment(request): if not request.user.is_authenticated(): return HttpResponse('{"status":"fail","msg":"用户未登录"}',content_type='application/json') article_id=request.POST.get('article_id',0) comment=request.POST.get('comment','') if article_id > 0 and comment: article_comment=CommentModel() article=ArticleModel.objects.get(id=int(article_id)) article_comment.article=article article_comment.comment=comment article_comment.user=request.user article_comment.save() return HttpResponse('{"status":"success","msg":"添加成功"}',content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"添加失败"}',content_type='application/json')
def mutate(cls, instance, args, info): session.begin() new_comment = CommentModel( text = args.get('text'), user_id = args.get('user_id'), product_id = args.get('product_id'), ) session.add(new_comment) session.commit() return CreateComment( comment=Comment(new_comment), ok=True, )
def get(self, video_id): parser = reqparse.RequestParser() parser.add_argument('page', help='This field cannot be blank', required=False) parser.add_argument('perPage', help='This field cannot be blank', required=False) json = parser.parse_args() result = CommentModel.get_all_comments_by_video_id(video_id) page = json['page'] perPage = json['perPage'] datum = [] for data in result: datum.append({ 'id': data.id, 'body': data.body, 'user_id': data.user_id, }) if page is None: page = 1 if perPage is None: perPage = 100 results = paging(datum, int(page), int(perPage)) total_page = number_page(datum, int(perPage)) if results: return { 'message': 'OK', 'data': results, 'pager': { 'current': page, 'total': total_page } }, 200 else: return {'message': 'Not found'}, 404
def create_one(article_id, content): comment = CommentModel(article_id=article_id, content=content) DBSession().add(comment) DBSession().flush() return comment
def post(self, blog_id): """ This method is used to handle comment post/edit/delete on a post """ user = getUserFromRequest(self.request) # type: UserModel if user: post = BlogModel.get_by_id(long(blog_id)) # type: BlogModel if not post: # no post found. so redirect to default page. self.redirect('/blog/all') return logging.info(self.request) commentBtnAction = self.request.get('commentBtn') # type:str # update/create a new comment if commentBtnAction: # check if the comment body is valid content = self.request.get('content') if content: if commentBtnAction == 'comment': # create comment cmnt = CommentModel(author=user, post=post, content=content) cmnt.put() # making the page to refresh after putting content; because of db eng consistency problems time.sleep(0.1) elif commentBtnAction.isdigit(): # update comment by the id cmnt = CommentModel.get_by_id( long(commentBtnAction)) # type: CommentModel # confirm that the comment is updated by its author if cmnt and user.key() == cmnt.author.key(): cmnt.content = content cmnt.put() time.sleep(0.1) # delete comment elif self.request.get('deleteComment'): commentId = self.request.get('deleteComment') # type:str if commentId and commentId.isdigit(): cmnt = CommentModel.get_by_id( long(commentId)) # type: CommentModel # confirm that the comment is deleted by its author if cmnt and user.key() == cmnt.author.key(): logging.info('cmnt %s' % cmnt) cmnt.delete() time.sleep(0.1) # like or dislike posts elif user.key().id() != post.author.key().id(): # author of the post can't like his own if self.request.get('likeComment'): # add the like count if the user already didn't liked if user.key().id() not in post.likes: post.likes.append(user.key().id()) post.likescount = int(post.likescount) + 1 # persist changes post.put() time.sleep(0.1) elif self.request.get('dislikeComment'): # reduce the like count and pop user id from list if user.key().id() in post.likes: post.likes.remove(user.key().id()) post.likescount = int(post.likescount) - 1 # persist changes post.put() time.sleep(0.1) self.redirect('/blog/%s' % blog_id) else: self.redirect('/signin')