3

我正在尝试实现 Django Oauth 以使用基于类的方法生成关于用户创建的访问令牌。

序列化程序.py

class UserCreateSerializer(ModelSerializer):            

    def create(self, validated_data):
        user = User.objects.create_user(validated_data['username'], 
            validated_data['email'],
            validated_data['password'])
        return user

    class Meta:
        model = User
        fields = ('username', 'email' ,'password')

视图.py

class User_Create_view(CreateAPIView):
    serializer_class = UserCreateSerializer
    queryset = User.objects.all()
    permission_classes = [AllowAny]
    authentication_classes = Has_Access_Token

    def create(self, request):
        serializers =self.serializer_class(data=request.data)
        if serializers.is_valid():
            # pdb.set_trace()
            serializers.save()
            #Has_Access_Token.access_token()
            return Response(serializers.data)

权限.py

class Has_Access_Token(BaseAuthentication):
   def access_token(request):
     app = Application.objects.get(name="testing")
     tok = generate_token()

     acce_token=AccessToken.objects.create(
            user=User.objects.all().last(),
            application=app,
            expires=datetime.datetime.now() + datetime.timedelta(days=365),
            token=tok)
    return acce_token

 # @method_decorator(access_token)
    def authenticate(self):
        return request      

如果我使用上述隐式方法,在该方法中生成的访问令牌是针对创建的用户而不是最佳实践。因此,对于在 APIView 中制作自定义身份验证类,标准程序是什么,无论是关于装饰器还是通过 BaseAuthentication 类我应该改变什么。

当我不使用装饰器时

  File "/home/allwin/Desktop/response/env/local/lib/python2.7/site-packages/rest_framework/views.py", line 262, in get_authenticators
    return [auth() for auth in self.authentication_classes]
    TypeError: 'type' object is not iterable

当我使用装饰器时

File "/usr/lib/python2.7/functools.py", line 33, in update_wrapper
    setattr(wrapper, attr, getattr(wrapped, attr))
AttributeError: 'tuple' object has no attribute '__module__'

如何调试上述问题,解决装饰器问题或更改类变量?

4

0 回答 0