def post(self, request, pk): bike = Bike.objects.get(id=pk) self.check_object_permissions(request, bike) contracts = bike.contract_set.filter(time_end__isnull=True) data = request.data.copy() if 'secret' in data: if data['secret'] == '1': del data['secret'] if len(contracts) != 0 and 'secret' in data: contract = contracts[0] if 'timestamp' in request.data: timestamp = request.data['timestamp'] end_time = datetime.datetime.fromtimestamp(int(timestamp), tz=pytz.utc) else: end_time = datetime.datetime.now() serializer = serializers.ContractSerializer(contract, data={'time_end': end_time}, partial=True) serializer.is_valid(raise_exception=True) serializer.save() if 'gpgga' in request.data: gpgga = request.data['gpgga'] msg = pynmea2.parse(gpgga) if gpgga.gps_qual == 1: data.update({'last_longitude': round(msg.longitude, 6), 'last_laltitude': round(msg.latitude, 6)}) if 'last_laltitude' in data and 'last_longitude' in data: if float(data['last_longitude']) == 0.0 and float(data['last_laltitude']) == 0.0: del data['last_laltitude'] del data['last_longitude'] serializer = serializers.BikeSerializer(bike, data=data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def post(self, request, pk): bike = Bike.objects.get(id=pk) self.check_object_permissions(request, bike) serializer = serializers.BikeSerializer(bike, data=request.data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def get(self, request): bikes = Bike.objects.all() serializer = serializers.BikeSerializer(bikes, many=True) return Response(serializer.data)
def get(self, request, pk): bike = Bike.objects.get(id=pk) self.check_object_permissions(request, bike) serializer = serializers.BikeSerializer(bike) return Response(serializer.data)
def post(self, request, userid, bikeid): user = User.objects.get(id=userid) bike = Bike.objects.get(id=bikeid) bike.owners.add(user) serializer = serializers.BikeSerializer(bike) return Response(serializer.data)