diff --git a/bikeshop_project/bike/tests.py b/bikeshop_project/bike/tests.py index 11fe5fd..4a2c772 100644 --- a/bikeshop_project/bike/tests.py +++ b/bikeshop_project/bike/tests.py @@ -300,3 +300,47 @@ class TestGet(TestCase): result = client.put('/api/v1/bikes/{bike_id}/purchase/'.format(bike_id=bike.id), data={'member': member.id}, format='json') self.assertEqual(result.status_code, status.HTTP_400_BAD_REQUEST) + + def test_scrap_transition(self): + data = { + "colour": "black", + "make": "Miyata", + "serial_number": "12345676", + "source": Bike.COS_BIKE_DIVERSION_PILOT, + "donated_by": "Greg", + "donated_at": "2017-01-01", + "size": Bike.SMALL, + "price": Decimal('68.00'), + "state": BikeState.ASSESSED, + "stolen": False, + "cpic_searched_at": timezone.now(), + "stripped": False + } + bike = Bike.objects.create(**data) + client = APIClient() + client.force_authenticate(user=self.user, token='blah') + result = client.put(f'/api/v1/bikes/{bike.id}/scrap/') + + self.assertEqual(result.status_code, status.HTTP_200_OK) + + def test_transfer_to_police_transition(self): + data = { + "colour": "black", + "make": "Miyata", + "serial_number": "12345676", + "source": Bike.COS_BIKE_DIVERSION_PILOT, + "donated_by": "Greg", + "donated_at": "2017-01-01", + "size": Bike.SMALL, + "price": Decimal('68.00'), + "state": BikeState.ASSESSED, + "stolen": False, + "cpic_searched_at": timezone.now(), + "stolen": True + } + bike = Bike.objects.create(**data) + client = APIClient() + client.force_authenticate(user=self.user, token='blah') + result = client.put(f'/api/v1/bikes/{bike.id}/stolen/') + + self.assertEqual(result.status_code, status.HTTP_200_OK) diff --git a/bikeshop_project/bike/views.py b/bikeshop_project/bike/views.py index 0cadc20..725b5f0 100644 --- a/bikeshop_project/bike/views.py +++ b/bikeshop_project/bike/views.py @@ -81,3 +81,29 @@ class BikeViewSet(viewsets.ModelViewSet): serializer = BikeSerializer(bike, context={'request': request}) return Response(serializer.data, status=status.HTTP_200_OK) + + @detail_route(methods=['put']) + def scrap(self, request, pk): + bike = get_object_or_404(Bike, pk=pk) + state = BikeState.SCRAPPED + if not can_proceed(bike.scrap): + raise ValidationError(detail=f'Transition from {bike.state} to {state}') + + bike.scrap() + bike.save() + + serializer = BikeSerializer(bike, context={'request': request}) + return Response(serializer.data, status=status.HTTP_200_OK) + + @detail_route(methods=['put']) + def stolen(self, request, pk): + bike = get_object_or_404(Bike, pk=pk) + state = BikeState.TRANSFERRED_TO_POLICE + if not can_proceed(bike.transfer_to_police): + raise ValidationError(detail=f'Transition from {bike.state} to {state}') + + bike.transfer_to_police() + bike.save() + + serializer = BikeSerializer(bike, context={'request': request}) + return Response(serializer.data, status=status.HTTP_200_OK)