def post(self, request, api_key, format=None): """ http -a username:password --json POST http://127.0.0.1:8000/api/v1/data/API-KEY/ name_id="123" value="0" :param request: :param format: :return: """ data = JSONParser().parse(request) data['owner'] = self.request.user.pk x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: data['remote_address'] = x_forwarded_for.split(',')[-1].strip() else: data['remote_address'] = request.META.get( 'REMOTE_ADDR') + "&" + request.META.get( 'HTTP_USER_AGENT') + "&" + request.META.get( 'SERVER_PROTOCOL') name = get_object_or_404(Element, name_id=data['name_id']).channel data['channel'] = get_object_or_404(Channel, api_key=api_key, name=name).pk serializer = DataSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def post(self, request, format=None): """ :param request: :param format: :return: """ data = JSONParser().parse(request) data['owner'] = self.request.user.pk x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: data['remote_address'] = x_forwarded_for.split(',')[-1].strip() else: data['remote_address'] = request.META.get( 'REMOTE_ADDR') + "&" + request.META.get( 'HTTP_USER_AGENT') + "&" + request.META.get( 'SERVER_PROTOCOL') data['channel'] = get_object_or_404(Channel, api_key=data['api_key']).pk debug(data) serializer = DataSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): """ :param request: :param format: :return: """ data = JSONParser().parse(request) data['owner'] = self.request.user.pk x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: data['remote_address'] = x_forwarded_for.split(',')[-1].strip() else: data['remote_address'] = request.META.get('REMOTE_ADDR') + "&" + request.META.get('HTTP_USER_AGENT') + "&" + request.META.get('SERVER_PROTOCOL') data['channel'] = get_object_or_404(Channel, api_key=data['api_key']).pk debug(data) serializer = DataSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): datas = self.get_object(pk) serializer = DataSerializer(datas, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): """ :param request: :param pk: :param format: :return: """ datas = self.get_object(pk) serializer = DataSerializer(datas, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): try: api_key = request.data['api_key'] device = get_object_or_404(Device, api_key=api_key, enable=True) request.data['device'] = device.pk request.data['remote_address'] = ip_address(request) serializer = DataSerializer(data=request.data) debug(serializer) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) except: msg_err = {'err': 'API KEY not found!'} return Response(msg_err, status=status.HTTP_400_BAD_REQUEST)