def add_comment(request): """ Add a comment :param request: HttpResponse :return: JsonResponse """ status, msg = False, "Invalid Request" if request.user.is_authenticated: comment = json.loads(request.body)['comment'] type_of_post = json.loads(request.body)['type_of_post'].upper() post_id = json.loads(request.body)['post_id'] # check type_of_post is valid check = False if type_of_post == "IM": i = Image_DB.objects.filter(image_id=post_id).first() if i: check = True elif type_of_post == "CM": c = Comment_DB.objects.filter(comment_id=post_id).first() if c: check = True if check: c = Comment_DB( username=request.user.username, type_of_post=type_of_post, post_id=post_id, content=comment) c.save() status, msg = True, "Success" return JsonResponse({"status":status, "msg":msg})
def update(self, instance: Comment, validated_data: dict) -> Comment: """ :param instance: An instance of Comment that we want to update fields values. :param validated_data: Dict type :return: An instance of Comment Model. """ instance.name = validated_data.get('name', instance.name) instance.text = validated_data.get('text', instance.text) instance.save() return instance
def comments(): if request.method == 'POST': comment = str(request.form.get('comment')) if comment == 'None': abort(400) tone = watson_tone(comment) comment = Comment(comment=comment, tone=tone) comment.save() response = jsonify(comment.serialize()) response.status_code = 201 else: comments = Comment.query.all() results = [comment.serialize() for comment in comments] response = jsonify(results) response.status_code = 200 return response
def create_comment(self, request, format=None, *args, **kwargs): user = request.user article = request.data['article_id'] article = Article.objects.get(article_id=article) recipient = None if request.data['recipient']: recipient = request.data['recipient'] recipient = MyUser.objects.get(id=recipient) if user: comment = Comment(article_id=article, title=request.data['title'], text=request.data['text'], user=user, recipient=recipient) comment.save() return (Response('Comment added'))
def comments(request): try: user = request.session['user'] order = request.data.get('order_id', '') content_star = request.data.get('content_star', '') content = request.data.get('content', '') with atomic(): comment = Comment() comment.user_id = user comment.order_id = order comment.content = content comment.content_star = content_star comment.save() data = {'code': 200, 'mes': '评价成功'} except: data = {'code': 300, 'mes': '评价失败,请稍后再试'} return JsonResponse(data)
def post(self, request, spot_id): content = request.data['content'] score = request.data['score'] if content is None: raise ParseError('내용을 입력해주세요.') if score is None: raise ParseError('별점을 입력해주세요.') comment = Comment(spot_id=spot_id, writer=request.user, content=content, score=score) comment.save() return Response(status=200, data={'data': CommentSerializer(comment).data})
def post(request): """ Request: { "page": "http://foo.bar.com/blog/page/1", "content": "Hello, 来自三体世界的评论", "parent" : 0, } """ comment = Comment() comment.user_id = request.user.id comment.comment_date = timezone.now() comment.comment_page = _parse_url(request.POST.get('page'))['url'] comment.comment_content = request.POST.get('content') if request.POST.get('parent', None): try: comment.comment_parent = int(request.POST.get('parent')) except: comment.comment_type = Comment.TYPE_NORMAL else: comment.comment_type = Comment.TYPE_REPLY if comment.comment_parent > 0: try: parent = Comment.objects.get(comment_id=parent) except Comment.DoesNotExist: from . import response return response.error(400, _('Bad data'), request.POST.get('callback')) try: comment.clean_fields() except ValidationError as e: from . import response return response.error(400, _('Bad data'), request.POST.get('callback'), inner_data = e.message_dict if settings.DEBUG else None) comment.save() return JsonpResponse(data = DATA_OK, callback = request.POST.get('callback'), **KWARGS_JSON)
def post(self, request, room_number, question_id): request.session.save() json = JSONParser().parse(request) question = get_or_none(Question, pk=question_id) if question and question.room.number == room_number: comment = Comment(question=question, message=json['message']) comment.save() Group('room-%s' % question.room.number).send({ 'text': dumps({ 'type': 'question', 'action': 'update', 'data': QuestionSerializer(question).data }) }) return Response({}, status=204) else: return Response({'error': 'no matching question found'}, status=404)
def comment_strangething(self, request, pk=None): if 'text' in request.data: user = request.user strangething = Strangething.objects.get(id=pk) text = request.data['text'] try: comment = Comment(user=user, strangething=strangething, text=text) comment.save() response = {'message': 'Commentaire posté !'} except: comment = Comment.objects.get(user=user, strangething=strangething, text=text) response = { 'message': 'Vous avez déjà posté un commentaire identique' } return Response(response, status=status.HTTP_200_OK) else: response = {'message': 'Votre commentaire est vide..!'} return Response(response, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, post_id, format=None): username_data = get_username_from_jwt(request) if username_data['username'] != False: data = JSONParser().parse(request) serializer = CommentSerializer(data=data) if serializer.is_valid(): try: post = Post.objects.get(pk=post_id) except Post.DoesNotExist: return JsonResponse({'msg': 'Post was not found'}, status=404) comment = Comment(text=serializer.data['text'], post=post, username=username_data['username']) comment.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) return JsonResponse({'msg': username_data['error_msg']}, status=400)
from api.serializers import UserSerializer, ReportSerializer, ImageReportSerializer, CommentSerializer from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser user = User(name='Horacio', email='*****@*****.**', password='******', profile_picture='api/media/profile_pic.png') user.save() #get id report = Report(user_fk=user, name_subject='Antonio Campos', status='encontrado', gender='masculino', birth_date='1993-02-02', state='Nayarit', city='San Blas', missing_date='2014-04-02', description='Le encanta la polla al mojo de ajo') report.save() imageReport = ImageReport(report_fk=report, image='api/media/default/missing_pic.png') imageReport.save() comment = Comment(report_fk=report, comment_date='2014-04-02', content='Me encontre un calcetin mojado') comment.save() userSerializer = UserSerializer(user) userSerializer.data rs = ReportSerializer(report) rs.data irs = ImageReportSerializer(imageReport) irs.data cs = CommentSerializer(comment) cs.data content = JSONRenderer().render(userSerializer.data) content
def create(self, validated_data): comment = Comment(**validated_data) comment.save() return comment
gender='masculino', birth_date='1993-02-02', state='Nayarit', city='San Blas', missing_date='2014-04-02', description='Le encanta la polla al mojo de ajo') report.save() imageReport = ImageReport(report_fk=report, image='api/media/default/missing_pic.png') imageReport.save() comment = Comment(report_fk=report, comment_date='2014-04-02', content='Me encontre un calcetin mojado') comment.save() userSerializer = UserSerializer(user) userSerializer.data rs = ReportSerializer(report) rs.data irs = ImageReportSerializer(imageReport) irs.data cs = CommentSerializer(comment) cs.data content = JSONRenderer().render(userSerializer.data) content
def mutate_and_get_payload(cls, root, info, comment_data): comment = Comment(**comment_data) comment.save() return IntroduceComment(comment=comment)
def create(self, validated_data): comment = Comment(**validated_data) comment.author = self.context['request'].user comment.save() return comment
def create(self, validated_data): # {'name': 'new category 4'} # name='new category 4' comment = Comment(**validated_data) comment.save() return comment
def blog_comments(request): token = request.POST.get('token') blog_id = request.POST.get('id') comments = request.POST.get('comments') comments_id = request.POST.get('comments_id') if token != '': try: obj_token = Token.objects.get(token=token) try: data = {} _id = int(blog_id) blog = Blog.objects.get(pk=_id) # create comment comment_blog = Comment(blog=blog, user=obj_token.user, comments=comments, status=1) if comments_id != None: try: comment_reply_obj = Comment.objects.get(pk=comments_id) comment_blog.comments_reply = comment_reply_obj comment_blog.save() except Comment.DoesNotExist: response = { 'result': 0, 'message': 'Error comments id' } return JsonResponse(response) else: comment_blog.save() if obj_token.user != blog.author: # create notification notifi = Notification(blog=blog, user=obj_token.user, comment=comment_blog) notifi.save() # create notification count try: notifi_count = NotificationCount.objects.get( user=blog.author) notifi_count.count += 1 notifi_count.save() except NotificationCount.DoesNotExist: notifi_count = NotificationCount(user=blog.author, count=1) notifi_count.save() data = { 'id': notifi.id, 'user_id': notifi.user.id, 'username': notifi.user.username, 'firstName': notifi.user.first_name, 'lastName': notifi.user.last_name, 'blog_id': notifi.blog.id, 'blog_title': notifi.blog.title, 'blog_author': get_token_user(notifi.blog.author.username), 'comments_name': notifi.comment.comments, 'status': notifi.status, 'comments_id': notifi.comment.id, 'created_by_image': get_profile_image(notifi.user.username) } response = {'result': 1, 'data': data, 'message': 'success'} except Blog.DoesNotExist: response = {'result': 0, 'message': 'Error id'} except Exception as e: response = {'result': 0, 'message': 'Error token'} else: response = {'result': 0, 'message': '403'} return JsonResponse(response)