コード例 #1
0
    def _update_trip(self, data):
        instance = Trip.objects.get(id=data.get('id'))

        serializer = TripSerializer(data=data)
        serializer.is_valid(raise_exception=True)

        return serializer.update(instance, serializer.validated_data)
コード例 #2
0
ファイル: views.py プロジェクト: rollykay/Batch8
 def post(self, request: Request):
     serializer = TripSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     trip = serializer.save()
     calculate_price(trip.user_id, start_of_week(trip.start_time),
                     end_of_week(trip.end_time))
     print(trip)
     return Response(data=serializer.data)
コード例 #3
0
ファイル: consumers.py プロジェクト: ParentJA/taxi-app
 def _update_trip(self, data):
     # Retrieve the Trip obj by ID
     instance = Trip.objects.get(id=data.get('id'))
     # Serialize the Trip data with update
     serializer = TripSerializer(data=data)
     # Ensure the serialized data is valid according to the model fields
     serializer.is_valid(raise_exception=True)
     # Use built-in update() passing in the Trip obj (instance) and the serializer data
     return serializer.update(instance, serializer.validated_data)
コード例 #4
0
ファイル: views.py プロジェクト: fernandoguy/trips
def trip_detail(request, pk):
    try:
        trip = Trip.objects.get(pk=pk)
    except Trip.DoesNotExist:
        return JsonResponse({'message': 'trip no  existe'},
                            status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        trip_serializer = TripSerializer(trip)
        return JsonResponse(trip_serializer.data)

    elif request.method == 'PUT':
        trip_data = JSONParser().parse(request)
        trip_serializer = TripSerializer(trip, data=trip_data)
        if trip_serializer.is_valid():
            trip_serializer.save()
            return JsonResponse(trip_serializer.data)
        return JsonResponse(trip_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        trip.delete()
        return JsonResponse(
            {'message': 'trip fue eliminado satisfactoriamente!'},
            status=status.HTTP_204_NO_CONTENT)
コード例 #5
0
ファイル: views.py プロジェクト: fernandoguy/trips
def trip_total_viajes_ciudad(request, pk):
    try:
        #trip = Trip.objects.get(pk=pk)
        count = Trip.objects.all().filter(cityname='Bogotá').count()

    except Trip.DoesNotExist:
        return JsonResponse({'message': 'La ciudad no existe'},
                            status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        trip_serializer = TripSerializer(trip)
        return JsonResponse({'message': 'Existen {} Viajes!'.format(count)},
                            status=status.HTTP_204_NO_CONTENT)

    elif request.method == 'PUT':
        trip_data = JSONParser().parse(request)
        trip_serializer = TripSerializer(trip, data=trip_data)
        if trip_serializer.is_valid():
            trip_serializer.save()
            return JsonResponse(trip_serializer.data)
        return JsonResponse(trip_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        trip.delete()
        return JsonResponse({'message': 'trip borrado exitosamente!'},
                            status=status.HTTP_204_NO_CONTENT)
コード例 #6
0
ファイル: views.py プロジェクト: fernandoguy/trips
def trip_list(request):
    if request.method == 'GET':
        trips = Trip.objects.all()

        start = request.GET.get('start', None)
        if start is not None:
            trips = trips.filter(start__icontains=start)

        trips_serializer = TripSerializer(trips, many=True)
        return JsonResponse(trips_serializer.data, safe=False)
        # 'safe=False' for objects serialization

    elif request.method == 'POST':
        trip_data = JSONParser().parse(request)
        trip_serializer = TripSerializer(data=trip_data)
        if trip_serializer.is_valid():
            trip_serializer.save()
            return JsonResponse(trip_serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(trip_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        count = Trip.objects.all().delete()
        return JsonResponse(
            {
                'message':
                '{} Viaje  fué eliminiado satisfactoriamente!'.format(count[0])
            },
            status=status.HTTP_204_NO_CONTENT)
コード例 #7
0
 def put(self, request, pk, format='json'):
     trip = self.get_object(pk)
     serializer = TripSerializer(trip, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return JsonResponse(serializer.data, safe=False)
     return JsonResponse(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST,
                         safe=False)
コード例 #8
0
 def post(self, request, format='json'):
     serializer = TripSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return JsonResponse(serializer.data,
                             status=status.HTTP_201_CREATED,
                             safe=False)
     return JsonResponse(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST,
                         safe=False)
コード例 #9
0
ファイル: views.py プロジェクト: vad2der/apichallenge
def trip_detail(request, pk):
    try:
        trip = Trip.objects.get(pk=pk)
    except Trip.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = TripSerializer(trip)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = TripSerializer(trip, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        trip.delete()
        return HttpResponse(status=204)
コード例 #10
0
 def _create_trip(self, data):
     serializer = TripSerializer(data=data)
     serializer.is_valid(raise_exception=True)
     return serializer.create(serializer.validated_data)
コード例 #11
0
 def _update_trip(self, content):
     instance = Trip.objects.get(id=content.get('id'))
     serializer = TripSerializer(data=content)
     serializer.is_valid(raise_exception=True)
     trip = serializer.update(instance, serializer.validated_data)
     return trip
コード例 #12
0
 def _create_trip(self, content):
     serializer = TripSerializer(data=content)
     serializer.is_valid(raise_exception=True)
     trip = serializer.create(serializer.validated_data)
     return trip