コード例 #1
0
ファイル: login.py プロジェクト: huychau/drf-registration
class LoginView(CreateAPIView):
    """
    This is used to Login into system.
    """

    permission_classes = import_string_list(drfr_settings.LOGIN_PERMISSION_CLASSES)
    serializer_class = import_string(drfr_settings.LOGIN_SERIALIZER)

    def post(self, request, *args, **kwargs):
        """
        Override to check user login

        Args:
            request (object): The request object

        """
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)

        user = serializer.validated_data['user']

        # Update last logged in
        update_last_login(None, user)
        data = get_user_profile_data(user)

        return Response(data, status=status.HTTP_200_OK)
コード例 #2
0
ファイル: register.py プロジェクト: huychau/drf-registration
class RegisterView(CreateAPIView):
    """
    Register a new user to the system
    """

    permission_classes = import_string_list(
        drfr_settings.REGISTER_PERMISSION_CLASSES)
    serializer_class = import_string(drfr_settings.REGISTER_SERIALIZER)

    def create(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)

        user = serializer.save()
        data = get_user_profile_data(user)

        domain = get_current_domain(request)

        # Send email activation link
        if has_user_activate_token() or has_user_verify_code():
            send_verify_email(user, domain)
        else:
            send_email_welcome(user)

        return Response(data, status=status.HTTP_201_CREATED)
コード例 #3
0
class SetPasswordView(UpdateAPIView):
    """
    Set user password
    """
    permission_classes = import_string_list(drfr_settings.SET_PASSWORD_PERMISSION_CLASSES)
    serializer_class = import_string(drfr_settings.SET_PASSWORD_SERIALIZER)

    def update(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()

        # Response data include new tokens
        data = get_user_profile_data(user)

        return Response(data, status=status.HTTP_200_OK)
コード例 #4
0
def get_user_profile_data(user):
    """
    Get user refresh token and access token

    Args:
        user (dict): The user instance

    Returns:
        [dict]: The data response include the token
    """
    serializer = import_string(drfr_settings.USER_SERIALIZER)
    data = serializer(user).data

    # Add tokens to data
    if has_user_verified(user):
        data['token'] = get_user_token(user).key

    return data
コード例 #5
0
class ProfileView(RetrieveUpdateAPIView):
    """
    Get update user profile information
    """

    permission_classes = import_string_list(
        drfr_settings.PROFILE_PERMISSION_CLASSES)
    serializer_class = import_string(drfr_settings.PROFILE_SERIALIZER)
    queryset = get_all_users()

    def get_object(self):
        return self.request.user

    def update(self, request, *args, **kwargs):
        """
        Custom update user profile
        """

        # Remove write only fields when update profile
        for field in drfr_settings.USER_WRITE_ONLY_FIELDS:

            if field in request.data.keys():

                # Make it editable
                request.data._mutable = True

                request.data.pop(field)

                # Disable editable
                request.data._mutable = False

        # Support the case user can change password in profile if
        # USER_WRITE_ONLY_FIELDS not contain password field
        if 'password' in request.data.keys():
            request.data._mutable = True
            self.request.user.set_password(request.data.pop('password')[0])
            self.request.user.save()
            request.data._mutable = False

        return super(ProfileView, self).update(request, *args, **kwargs)
コード例 #6
0
class ResetPasswordView(APIView):
    """
    Reset user password by send the link to email
    """

    permission_classes = import_string_list(
        drfr_settings.RESET_PASSWORD_PERMISSION_CLASSES)
    serializer_class = import_string(drfr_settings.RESET_PASSWORD_SERIALIZER)

    def post(self, request, *args, **kwargs):
        """
        Override to check reset password request

        Args:
            request (object): The request object

        Raises:
            Http404: In the case RESET_PASSWORD_ENABLED is False
        """

        # Check in the case reset password is not supported
        if not drfr_settings.RESET_PASSWORD_ENABLED:
            raise Http404()

        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)

        # Get user from validated data
        user = serializer.validated_data['user']

        # Send reset password link to email
        domain = get_current_domain(request)
        send_reset_password_token_email(user, domain)

        return Response({'detail': _('Password reset e-mail has been sent.')},
                        status=status.HTTP_200_OK)
コード例 #7
0
    def test_import_string(self):

        user_serializer = import_string(
            'drf_registration.api.user.UserSerializer')

        self.assertTrue(user_serializer, UserSerializer)
コード例 #8
0
def get_user_serializer():
    """
    Get user serializer from settings
    """

    return import_string(drfr_settings.USER_SERIALIZER)