3

I am try to implement the Django Oauth for generating Access token with respect to user creation with class based method.

serializer.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')

views.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)

permission.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      

If I am using the above implicit method at which access token is generated with respect to the user created but not the best practice. So for making the custom authentication class in APIView, what's the standard procedure, either with respect to decorator or through BaseAuthentication class what should i change.

When I am Not using the Decorator

  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

when I use the Decorator

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__'

How to debug the above issue, either resolving the decorator issue or altering the class variables?

James Z
  • 12,209
  • 10
  • 24
  • 44
ganesh
  • 31
  • 1

0 Answers0