Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

registrations with DRF #4

Closed
Guest007 opened this issue Jun 3, 2015 · 4 comments
Closed

registrations with DRF #4

Guest007 opened this issue Jun 3, 2015 · 4 comments
Labels

Comments

@Guest007
Copy link

Guest007 commented Jun 3, 2015

Are you planning to make registration through django-rest-framework-social-oauth2? Or it will be only authorisation?

@PhilipGarnero
Copy link
Collaborator

What do you mean by registration ?
An user object is automatically created if you use an external token but I can't really handle registration as some people will not have the same fields, actions and behaviors for their user.

I can help you by posting some examples but I will not implement it in this package unless wanted by a lot of people.

# You can use this as a simple serializer
class UserSerializer(serializers.ModelSerializer):
    is_active = serializers.CharField(read_only=True)
    date_joined = serializers.DateTimeField(read_only=True)

    class Meta:
        model = User
        fields = ('email', 'password', 'username', 'first_name', 'last_name', 'is_active', 'date_joined',)
        extra_kwargs = {'password': {'write_only': True}}

# And this viewset
class UserAPIView(GenericViewSet, RetrieveModelMixin, UpdateModelMixin, CreateModelMixin):
    """
    ### Interact with users

    - **Methods**:
        - list: *Post*
        - detail: *Get*, *Put*, *Patch*
        - me: *Get*
            - returns the current user
    - **Permissions**:
        - authenticated user required on detail
    - **Returns**:
        - the user
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_object(self):
        obj = super(UserAPIView, self).get_object()
        if obj != self.request.user:
            raise Http404
        return obj

    @list_route()
    def me(self, request):
        user = get_object_or_404(self.queryset, pk=request.user.id)
        serializer = self.serializer_class(user)
        return Response(serializer.data)

# Add these to your urls
    url(r'^api/', include('rest_framework_social_oauth2.urls')),
    url(r'^api/users/?$', UserAPIView.as_view({'post': 'create'}), name='user-list'),
    url(r'^api/users/me/?$', UserAPIView.as_view({'get': 'me'}), name='user-me'),
    url(r'^api/users/(?P<pk>\d+)$', UserAPIView.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update'}), name='user-detail'),

@Guest007
Copy link
Author

Guest007 commented Jun 3, 2015

I mean creating new user from FB account's data

@PhilipGarnero
Copy link
Collaborator

This should work already. Python social auth is used in the authentication backend and will automatically create an user if it doesn't exist.

@PhilipGarnero
Copy link
Collaborator

I'm closing this since it's the current behavior.
Feel free to reopen if I didn't answer your questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants