def post(self, request, pk): request.data['event'] = pk serializer = ReservationSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk): reservation = get_object_or_404(Reservation.objects.all(), pk=pk) serializer = ReservationSerializer(reservation, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200) return Response(status=status.HTTP_400_BAD_REQUEST)
def post(self, request, pk): request.data['event']=pk serializer=ReservationSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_invalid_reservation_not_enough_vacancies(self): test_data = self.valid_reservation_json_data test_data['paid_amount'] = 25500.0 test_data['attendee_number'] = 51 serializer = ReservationSerializer(data=test_data) self.assertFalse(serializer.is_valid()) self.assertEqual(set(serializer.errors['non_field_errors']), set(['Not enough vacancies for the reservation']))
def test_invalid_reservation_paid_amount_serializer(self): test_data = self.valid_reservation_json_data test_data[ 'paid_amount'] = 1234.0 # not valid since it should be 2*500=1000 serializer = ReservationSerializer(data=test_data) self.assertFalse(serializer.is_valid()) self.assertEqual(set(serializer.errors['non_field_errors']), set(['The paid amount is not valid']))
def test_reservation_serializer(self): serializer = ReservationSerializer( data=self.valid_reservation_json_data) self.assertTrue(serializer.is_valid()) reservation_fields = [ 'attendee_number', 'observations', 'paid_amount', 'event_occurrence' ] self.assertEqual(set(serializer.validated_data.keys()), set(reservation_fields))
def test_invalid_reservation_old_occurrence(self): occurrence = EventOccurrence.objects.create( start='2018-10-31T20:00:00', end='2018-10-31T23:00:00', vacancies=50, event=self.event) test_data = self.valid_reservation_json_data test_data['event_occurrence'] = occurrence.id serializer = ReservationSerializer(data=test_data) self.assertFalse(serializer.is_valid()) self.assertEqual(set(serializer.errors['non_field_errors']), set(['The date is no longer available']))
def test_reservation_detail_get(self): self.client.force_login(self.user) reservation = Reservation.objects.create(**self.valid_creation_data) response = self.client.get( reverse('reservations-detail', kwargs={'pk': reservation.id})) self.assertEqual(status.HTTP_200_OK, response.status_code) serializer = ReservationSerializer(reservation) self.assertEqual(response.data, serializer.data)
def test_invalid_reservation_event_cancelled(self): cancelled_event = Event.objects.create( name='Event Cancelled', description='Shouldnt be able to make a reservation', cancelled='2019-9-15T20:00:00', winery=self.winery, price=500.0, ) occurrence = EventOccurrence.objects.create( start='2030-10-31T20:00:00', end='2030-10-31T23:00:00', vacancies=50, event=cancelled_event) test_data = self.valid_reservation_json_data test_data['event_occurrence'] = occurrence.id serializer = ReservationSerializer(data=test_data) self.assertFalse(serializer.is_valid()) self.assertEqual(set(serializer.errors['non_field_errors']), set(['The event is cancelled']))
def reservations(self, request): if request.user.is_anonymous: return Response([], status=status.HTTP_200_OK) res = Reservation.objects.filter(user=request.user.id).order_by('-id') reservations = ReservationSerializer(res, many=True) return Response(reservations.data, status=status.HTTP_200_OK)
def get(self, request, pk): reservation = get_object_or_404(Reservation.objects.all(), pk=pk) serializer = ReservationSerializer(reservation, many=False) return Response(serializer.data)
def post(self, request): serializer = ReservationSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(status=status.HTTP_201_CREATED) return Response(status=status.HTTP_400_BAD_REQUEST)
def get(self, request): reservations = get_list_or_404(Reservation.objects.all()) serializer = ReservationSerializer(reservations, many=True) return Response(serializer.data)
def test_invalid_reservation_serializer(self): serializer = ReservationSerializer(data=self.invalid_reservation_data) self.assertFalse(serializer.is_valid()) self.assertEqual(set(serializer.errors), self.required_fields)
def get(self, request, pk): event = Event.objects.get(pk=pk) reservations = event.reservations serializer = ReservationSerializer(reservations, many=True) return Response(serializer.data)
def get_reservation_serializer(reservation): serializer = ReservationSerializer(reservation) content = JSONRenderer().render(serializer.data) stream = BytesIO(content) data = JSONParser().parse(stream) return ReservationSerializer(data=data)