def put(self, request, id): try: comment = Comment.objects.get(pk=id) data = dict(request.data) ser = CommentSerializer(comment, data, partial=True) if ser.is_valid(): ser.save() return JsonResponse(message='Comment Updated') return JsonResponse(message=ser.errors, status=status.HTTP_400_BAD_REQUEST) except Comment.DoesNotExist: return JsonResponse(status=status.HTTP_404_NOT_FOUND, message="wrong id - comment not found")
def post(self, request, format=None): teacherId = request.POST['teacherId'] courseId = request.POST['courseId'] serializer = CommentSerializer(Comment.objects.filter( teacher_id=teacherId, course_id=courseId), many=True) return Response(serializer.data)
def get(self, request, id): try: return JsonResponse( data=CommentSerializer(Comment.objects.get(pk=id)).data) except Comment.DoesNotExist: return JsonResponse(status=status.HTTP_404_NOT_FOUND, message="wrong id - Comment not found")
def get_comments(self, request, **kwargs): offer_comment = Comment.objects.filter( offer=kwargs.get('offer_id')).order_by('-date') serializer = CommentSerializer(offer_comment, many=True, context={'request': request}) paginator = LimitOffsetPagination() data = paginator.paginate_queryset(serializer.data, request) return paginator.get_paginated_response(data=data)
class ProductSerializer(serializers.ModelSerializer): comments = CommentSerializer(many=True, source='product_comments') images = ProductImageSerializer(many=True, source='product_images') class Meta: model = Product fields = ['name', 'type', 'status', 'unit_type', 'id', 'comments', "images", "price", "info" ]
def get(self, request, id): comment = Comment.objects.select_related( 'booked_room__book_room__room').filter( booked_room__book_room__room_id=id) data = CommentSerializer(comment, exclude=('room_rate', ), many=True).data room_rate = Comment.objects.select_related('booked_room__room').filter( booked_room__book_room__room_id=id).aggregate( room_rate=Avg('rate'))['room_rate'] if request.query_params.get('return') == 'txt': comments_body = ',\n'.join([ c.body + ' from ' + c.booked_room.user.username for c in comment ]) if comment.count(): return HttpResponse( f"""this room got {comment.count()} comment and {room_rate} avg score have this. overviews:\n {comments_body}""") return HttpResponse("this room haven't any comment") return JsonResponse(data=data, extra={'room_rate': room_rate})