예제 #1
0
파일: views.py 프로젝트: jingyuyao/kaas
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return responses.data_response(serializer.data, status.HTTP_201_CREATED)

        return responses.error_response(serializer.errors)
예제 #2
0
파일: views.py 프로젝트: jingyuyao/kaas
 def retrieve(self, request, *args, **kwargs):
     token = self.get_object()
     try:
         self._replace_expired_token(token)
         serializer = self.get_serializer(token)
         return responses.data_response(serializer.data)
     except ValueError as e:
         token.delete()
         return responses.error_response(str(e))
예제 #3
0
파일: views.py 프로젝트: jingyuyao/kaas
    def create(self, request, *args, **kwargs):
        """
        Create a token using the given code and associates it with the given oauth_client and user

        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        user = request.user
        try:
            code = request.data.get('code')
            oauth_client = request.data.get('oauth_client')

            # actually get the oauth_client object
            # Can take either name or id
            try:
                oauth_client = OauthClient.objects.get(id=int(oauth_client))
            except ValueError:
                oauth_client = OauthClient.objects.get(name=oauth_client)

            if None in (code, oauth_client):
                raise ValueError(request.data)

            payload = {
                'grant_type': 'authorization_code',  # OAuth 2.0 specification
                'code': code,
                'redirect_uri': oauth_client.redirect_uri
            }

            auth = None

            if oauth_client.authorize_using_header:
                auth = (oauth_client.client_id, oauth_client.client_secret)

            else:
                payload['client_id'] = oauth_client.client_id
                payload['client_secret'] = oauth_client.client_secret

            r = requests.post(oauth_client.token_url, payload, auth=auth, headers=self.HEADERS)

            if r.status_code == 200:
                # Painful debugging note: Yea... it returns a tuple.
                token, created = Token.objects.update_or_create(user=user, oauth_client=oauth_client,
                                                                defaults=r.json())
                serializer = TokenSerializer(token)

                return responses.data_response(serializer.data)
            else:
                raise ValueError('failed get token request')

        except OauthClient.DoesNotExist:
            return responses.error_response('Invalid oauth_client_id.')
        except ValueError:
            return responses.INVALID_DATA_RESPONSE
예제 #4
0
파일: views.py 프로젝트: jingyuyao/kaas
    def post(request, *args, **kwargs):
        username = request.data.get('username')
        password = request.data.get('password')
        user = authenticate(username=username, password=password)

        if user is not None:
            login(request, user)
            serializer = UserSerializer(user)
            return responses.data_response(serializer.data)
        else:
            return responses.error_response('Invalid credentials.')
예제 #5
0
파일: views.py 프로젝트: jingyuyao/kaas
    def list(self, request, *args, **kwargs):
        """
        Lists all the tokens owned by the user

        :param request:
        :param args:
        :param kwargs:
        :return:
        """

        tokens = Token.objects.filter(user=request.user)
        self._replace_expired_tokens(tokens)
        serializer = TokenSerializer(tokens, many=True)
        return responses.data_response(serializer.data)
예제 #6
0
파일: views.py 프로젝트: jingyuyao/kaas
    def get(request, *args, **kwargs):
        """
        Returns serialized user data if user is authenticated else
        returns serialized anonymous user data.

        The use of anonymous user over responding with a 401 is better suited
        for clients expecting a valid response status when calling this view.

        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        if request.user.is_authenticated():
            serializer = UserSerializer(request.user)
        else:
            serializer = UserSerializer(AnonymousUser())

        return responses.data_response(serializer.data)
예제 #7
0
 def create(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=False)
     book = book_services.create_book(**serializer.validated_data)
     serializer = self.get_serializer(instance=book)
     return data_response(201, {"book": serializer.data})
예제 #8
0
 def update(self, request, *args, **kwargs):
     book = book_services.update_book(self.kwargs['id'], **request.data)
     serializer = self.get_serializer(instance=book)
     return data_response(200, serializer.data)
예제 #9
0
 def destroy(self, request, *args, **kwargs):
     book = book_services.get_a_book(book_id=kwargs['id'])
     book_services.delete_book(book_id=kwargs['id'])
     return data_response(204, [], "success",
                          f"The book {book.name} was deleted successfully")
예제 #10
0
 def list(self, request, *args, **kwargs):
     objects = self.get_queryset()
     serializer = BookSerializer(objects, many=True)
     return data_response(200, serializer.data)
예제 #11
0
 def retrieve(self, request, *args, **kwargs):
     instance = self.get_object()
     serializer = self.get_serializer(instance)
     return data_response(200, serializer.data)