Beispiel #1
0
    def _auth_handler(self, request, data):
        serializer = self.get_serializer(data=data)

        if serializer.is_valid():
            user = serializer.object.get('user') or request.user
            token = serializer.object.get('token')
            response_data = jwt_response_payload_handler(token, user, request)

            return Response(response_data)

        #return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        return Response(err_msg=str(serializer.errors),
                        err_code=105)  # TODO: 少了Token相关的code
Beispiel #2
0
 def create(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     self.perform_create(serializer)
     headers = self.get_success_headers(serializer.data)
     return Response(serializer.data,
                     status=status.HTTP_201_CREATED,
                     headers=headers)
Beispiel #3
0
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response({'list': serializer.data})
Beispiel #4
0
def user_register(request):
    if request.method == 'POST':
        params = request.POST
    elif request.method == 'GET':
        params = request.GET
    else:
        return Response(err_code=106, err_msg='请求不合法')

    username = params['username']
    password = params['pwd']

    try:
        user_obj = User.objects.create_user(username=username,
                                            password=password)
        user_obj.save()
    except Exception as e:
        return Response(err_code=104, err_msg='没法注册,原因你懂得')

    return Response({'uid': user_obj.pk})
Beispiel #5
0
def get_user_id(request):
    '''
    获得用户 id
    '''
    if request.method == 'POST':
        username = request.POST.get('username')
    elif request.method == 'GET':
        username = request.GET.get('username')
    else:
        return Response(err_code=101, err_msg='用户名输入错误')

    user_obj = User.objects.filter(Q(username=username)
                                   | Q(email=username))  # TODO: 加入手机号啥的

    # TODO: 命中两个怎么办?
    if not user_obj:
        return Response(err_code=101, err_msg='用户不存在')

    obj = UserIdSerializer(user_obj[0])
    return Response(obj.data)
Beispiel #6
0
    def update(self, request, *args, **kwargs):
        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        serializer = self.get_serializer(instance,
                                         data=request.data,
                                         partial=partial)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        if getattr(instance, '_prefetched_objects_cache', None):
            # If 'prefetch_related' has been applied to a queryset, we need to
            # forcibly invalidate the prefetch cache on the instance.
            instance._prefetched_objects_cache = {}

        return Response({'update': serializer.data})
Beispiel #7
0
 def destroy(self, request, *args, **kwargs):
     instance = self.get_object()
     self.perform_destroy(instance)
     return Response(status=status.HTTP_204_NO_CONTENT)
Beispiel #8
0
 def retrieve(self, request, *args, **kwargs):
     instance = self.get_object()
     serializer = self.get_serializer(instance)
     return Response(serializer.data)