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 get(self, request, format=None): """ :param request: :param format: :return: """ debug('get') debug(request.GET['data']) if request.GET['data'] == 'first': datas = Data.objects.order_by('pub_date')[:1] elif request.GET['data'] == 'last': datas = Data.objects.order_by('-pub_date')[:1] else: datas = Data.objects.all() serializer = DataSerializer(datas, many=True) return Response(serializer.data)
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)