Example #1
0
    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'] = ip = x_forwarded_for.split(
                ',')[-1].strip()
        else:
            ip = request.META.get('REMOTE_ADDR')
            data['remote_address'] = ip + "&" + request.META.get(
                'HTTP_USER_AGENT') + "&" + request.META.get('SERVER_PROTOCOL')

        data['channel'] = get_object_or_404(
            Channel,
            api_key=api_key,
            name=get_object_or_404(Element,
                                   name_id=data['name_id']).channel).pk
        serializer = DataSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)
Example #2
0
	def put(self, request, pk, format=None):
		
		data = self.get_object(pk)
		serializer = DataSerializer(data)
		
		if serializer.is_valid():
			serializer.save()
			return Response(serializer.data)
			
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #3
0
	def post(self, request, format=None):

		serializer = DataSerializer(data=request.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)
Example #4
0
 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)
Example #5
0
    def post(self, request):
        """
        Creates a new account
        :param request:
            {
                "username": "******",
                "first_name": "string",
                "last_name": "string",
                "password": "******",
                "email": "string",
                "user_type": integer,
                "access_level": integer,
                "company_id": integer,
                "invite_id": integer
            }
        :return: {message: string, user: UserSerializer, data: DataSerializer}
        """

        data = json.loads(request.body)
        user = UserSerializer(data=data)

        if user.is_valid():
            saved_user = user.save()

            try:
                userdata = Data.objects.create(
                    user=saved_user,
                    user_type=data.get('user_type'),
                    access_level=1,
                    company_id=data.get('company_id'))

                # if the user is a Client, test if it is the first one of that company
                # to change ownership

                if data.get('user_type') == Data.CLIENT:
                    company = Company.objects.get(id=data.get('company_id'))
                    if company.owner is None:
                        company.owner = saved_user
                        company.save()

                        userdata.access_level = Data.ADMIN
                        userdata.save()

                if data.get('invite_id', None):
                    invite = Invite.objects.get(id=data.get('invite_id'))
                    invite.accepted = True
                    invite.save()

                return Response(
                    {
                        'message': 'User created successfully',
                        'user': user.data,
                        'data': DataSerializer(userdata).data
                    }, status.HTTP_201_CREATED)
            except Exception as e:
                saved_user.delete()
                return Response({'message': 'User couldnt be created'},
                                status=status.HTTP_400_BAD_REQUEST)

        return Response({'message': 'User could not be created'})
Example #6
0
 def get(self, request, pk, format=None):
     """
     :param request:
     :param pk:
     :param format:
     :return:
     """
     datas = self.get_object(pk)
     serializer = DataSerializer(datas)
     return Response(serializer.data)
Example #7
0
 def get(self, request, api_key, format=None):
     """
     http --json http://127.0.0.1:8000/api/v1/data/API-KEY/
     http -a username:password --json http://127.0.0.1:8000/api/v1/data/API-KEY/
     :param request:
     :param format:
     :return:
     """
     try:
         datas = Data.objects.filter(channel=Channel.objects.get(
             api_key=api_key))
         serializer = DataSerializer(datas, many=True)
         return JSONResponse(serializer.data)
     except:
         raise Http404
Example #8
0
def jwt_response_payload_handler(token, user=None, request=None):
    try:
        data = Data.objects.get(user_id=user.id)
        serialized_company = None
        if data.company is not None:
            company = Company.objects.get(id=data.company.id)
            serialized_company = CompanySerializer(company).data

        return {
            'access_token': token,
            'user': UserSerializer(user).data,
            'data': DataSerializer(data).data,
            'company': serialized_company
        }
    except Exception as e:
        return None