def test_review_create_new_company_if_the_id_company_doesnt_exist(self): data = self._default_review_data() serializer = ReviewSerializer(data=data) serializer.is_valid() serializer.save(ip_address=self.faker.ipv4(), reviewer=self._user_recipe.make()) self.assertEquals(Company.objects.count(), 1)
def save_review(request): if request.method == "POST": try: data = JSONParser().parse(request) serializer = ReviewSerializer(data=data) user_no = data['user_no'] food_no = Food.objects.get(food_name=data['food_name']).food_no serializer.initial_data['food_no'] = food_no # 먹은 음식의 후기 작성 여부 변경 eaten_food = MapUserEat.objects.filter(user_no=user_no).get( food_no=food_no) if eaten_food.is_written == 1: return JsonResponse({"message": "ALREADY_WRITTEN"}, status=401) eaten_food.is_written = 1 eaten_food.save() # 유효성 검사 if not serializer.is_valid(): print(serializer.errors) return JsonResponse({"message": "INVALID_FORM"}, status=402) # 리뷰 저장 serializer.save() # 최근에 저장된 리뷰를 가져옴 review = Review.objects.order_by('rev_no').last() # 음식 정보 업데이트 (평균 별점, 후기 수) food = Food.objects.get(food_no=food_no) star = food.food_star spicy = food.food_spicy cnt = food.food_review_count food.food_spicy = round( ((spicy * cnt) + serializer.validated_data['rev_spicy']) / (cnt + 1), 1) food.food_star = round( ((star * cnt) + serializer.validated_data['rev_star']) / (cnt + 1), 1) food.food_review_count = cnt + 1 food.save() # 음식 태그 추가 tags = data['tag_no'] for i in range(len(tags)): MapFoodTag(rev_no=Review.objects.get(rev_no=review.rev_no), food_no=Food.objects.get(food_no=food_no), tag_no=Tag.objects.get(tag_no=tags[i])).save() return JsonResponse({"message": "SAVE_SUCCESS"}, status=200) except KeyError: return JsonResponse({"message": "INVALID_KEY"}, status=400)
def reviewCreate_view(request): customer = Customer.objects.get(id = request.user.id) if request.method == 'POST': serializer = ReviewSerializer(data = request.data) if serializer.is_valid(): serializer.save(customer=customer) serializer.save() logger.debug(f'Review object created, ID: {serializer.instance}') return Response(serializer.data, status=201) logger.warning(f'Review object was not created') logger.error(f'Server error') return Response(serializer.errors, status=500)
def test_submission_date_is_automatically_setted_on_create(self): data = self._default_review_data() serializer = ReviewSerializer(data=data) serializer.is_valid() instance = serializer.save(ip_address=self.faker.ipv4(), reviewer=self._user_recipe.make()) self.assertEquals(instance.submission_date, date.today())
def test_review_update_data_company_if_the_id_company_exists(self): company = self._company_recipe.make() data = self._default_review_data( company__company_id=company.company_id, company__name='new name') serializer = ReviewSerializer(data=data) serializer.is_valid() review = serializer.save(ip_address=self.faker.ipv4(), reviewer=self._user_recipe.make()) self.assertEquals(review.company.name, 'new name')
def review(self, request, *args, **kwargs): instance = self.get_object() if instance.status != Order.COMPLETED: detail = _('Only completed order can be reviewed.') raise exceptions.ValidationError(detail=detail) data = request.data data.update({'order': instance.pk}) context = self.get_serializer_context() serializer = ReviewSerializer(data=data, context=context) serializer.is_valid(raise_exception=True) serializer.save() instance.status = Order.REVIEWED instance.save() serializer = self.get_serializer(instance) return Response(serializer.data)
def post(self, request, *args, **kwargs): data = request.data booking_id = data.pop('booking_id') if Booking.objects.get(pk=booking_id).owner.id != request.user.id: return Response({}, status=status.HTTP_401_UNAUTHORIZED) serializer = ReviewSerializer(data=data) if serializer.is_valid(): serializer.save() review = serializer.data.get('id') # Save review to booking booking = Booking.objects.get(pk=booking_id) booking.review_id = review booking.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_200_OK)
def create(self, request): ''' Create a new review using the user authenticated by token ''' request_data = request.data request_data['reviewer'] = { 'user': { 'username': request.user.username, 'email': request.user.email }, 'key': '-' } # GET request IP 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)