def car_list(request): if request.method == 'POST': data = JSONParser().parse(request) data_old = None print data['uid'] for x in Car.objects.all(): if x.uid == data['uid']: if data['latitude'] == x.latitude and data[ 'longitude'] == x.longitude: data['latitude_old'] = x.latitude_old data['longitude_old'] = x.longitude_old else: data['latitude_old'] = x.latitude data['longitude_old'] = x.longitude x.delete() serializer = CarSerializer(data=data) if serializer.is_valid(): if authorize(data['uid']): serializer.save() processData(data) return JSONResponse(serializer.data, status=201) return JSONResponse( { 'status': False, 'message': "Usuario no registrado" }, status=401) return JSONResponse(serializer.errors, status=400)
def create(self, request, *args, **kwargs): serializer = CarSerializer(data=request.data) if serializer.is_valid(): serializer.save() return JsonResponse({ 'message': 'Create a new Car successful!' }, status=status.HTTP_201_CREATED) return JsonResponse({ 'message': 'Create a new Car unsuccessful!' }, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, *args, **kwargs): car = get_object_or_404(Car, id=kwargs.get('pk')) serializer = CarSerializer(car, data=request.data) if serializer.is_valid(): serializer.save() return JsonResponse({ 'message': 'Update Car successful!' }, status=status.HTTP_200_OK) return JsonResponse({ 'message': 'Update Car unsuccessful!' }, status=status.HTTP_400_BAD_REQUEST)
def test_get_special_car_without_on_demand_data(self): response = self.client.get(reverse(self.endpoint_name), {"id": self.special_id}) from_db = Car.objects.get(special_id=self.special_id) serializer = CarSerializer(from_db) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertNotEqual(response.data, serializer.data)
def test_retrieve_car_list(self): """Test retriving a list of cars""" sample_car() sample_car() res = self.client.get(CAR_URL) cars = Car.objects.all().order_by('-make_name') serializer = CarSerializer(cars, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def listAllCars(request): if request.method == 'GET': carros = Car.objects.all() serializer = CarSerializer(carros, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = CarSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def test_get_special_car_all_data(self): response = self.client.get( reverse(self.endpoint_name), { "id": self.special_id, "show_class": "True", "show_hybrid_or_electric": "True", }, ) from_db = Car.objects.get(special_id=self.special_id) serializer = CarSerializer(from_db) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, serializer.data)
def test_create_valid_payload(self): payload = { "registration_number": "AAA 1000", "max_passengers": 5, "production_year": 2010, "brand": "Brand", "model": "Model", "type": "Sedan", "car_class": "ECONOMIC", "hybrid_or_electric": True, } response = self.client.post(reverse(self.endpoint_name), payload, format="json") from_db = Car.objects.latest("pk") serializer = CarSerializer(from_db) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual( serializer.data["registration_number"], payload["registration_number"].upper(), )
def getCarDataByID(request, pk): try: carro = Car.objects.get(pk=pk) print(carro) except Car.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = CarSerializer(carro) return JSONResponse(serializer.data) elif request.method == 'PUT': print('PK => ' + str(pk)) data = JSONParser().parse(request) serializer = CarSerializer(carro, data=data) if serializer.is_valid(): print('Todo bien') serializer.save() print('ya no paso xd') return JSONResponse(serializer.data, status=200) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': print('Entraaa') carro.delete()