def list(self, request): reviwer = Token.objects.get(user=request.user) queryset = Review.objects.filter( reviewer=reviwer).order_by('submission_date') serializer = ReviewSerializer(queryset, many=True) return Response(serializer.data)
def create(self, request): request_data = request.data request_data['reviewer'] = { 'user': { 'username': request.user.username, 'email': request.user.email }, 'key': '-' } request_data['ip_address'] = request.META.get( 'HTTP_X_FORWARDED_FOR', request.META.get('REMOTE_ADDR')).split(',')[0] serializer = ReviewSerializer(data=request_data) if not serializer.is_valid(): return Response(serializer.errors, status=500) serializer.save() return Response(serializer.data, status=201)
def post(self, request): comp_id = request.data.pop('company') usr = request.user cur_company = Company.objects.get(id=comp_id) cur_reviewer = Reviewer.objects.get(user=usr) serializer = ReviewSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save(reviewer=cur_reviewer, company=cur_company) return Response(serializer.data, status=status.HTTP_200_OK)
def list(self, request, *args, **kwargs): queryset = self.queryset propId = self.request.query_params.get('property') if not propId: return Response(data={ "error": "Key 'property' not found in request query params" }, status=status.HTTP_400_BAD_REQUEST) queryset = queryset.filter(Q(bookings__property=propId)) serializer = ReviewSerializer(queryset, many=True) return Response(serializer.data)
def get_delete_book_review(request, pk, pk2): try: books = Book.objects.get(id=pk) except ObjectDoesNotExist: return Response({"status": "Book does not exist"}, status=status.HTTP_404_NOT_FOUND) try: review = books.reviews.get(id=pk2) except ObjectDoesNotExist: return Response({"status": "Reviews of this Book does not exist"}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = ReviewSerializer(review) return Response(serializer.data, status=status.HTTP_200_OK) elif request.method == 'DELETE': review.delete() return Response({"status": "Successfully deleted review"}, status=status.HTTP_204_NO_CONTENT) return Response({'error': 'bad request'}, status=status.HTTP_404_NOT_FOUND)
def show_add_book_reviews(request, pk): try: books = Book.objects.get(id=pk) except ObjectDoesNotExist: return Response({"status": "Book does not exist"}, status=status.HTTP_404_NOT_FOUND) try: reviews = books.review_set.all() except ObjectDoesNotExist: return Response({"status": "Reviews of this Book does not exist"}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': ser = ReviewSerializer(reviews, many=True) return Response(ser.data, status=status.HTTP_200_OK) elif request.method == 'POST': data = json.loads(request.body) serializer = ReviewSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) logger.error('Something went wrong!') return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response({'error': 'bad request'}, status=status.HTTP_404_NOT_FOUND)
def get(self, request): print(request.user) reviews = Review.objects.filter(reviewer__user=request.user) serializer = ReviewSerializer(reviews, many=True) return Response(serializer.data, status=status.HTTP_200_OK)