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 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 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 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()