コード例 #1
0
 def post(self, request, *args, **kwargs):
     response_data = {}
     serializer = ConfirmResetPasswordSerializer(data=request.data)
     if serializer.is_valid(raise_exception=True):
         user = serializer.save()
         context = {"user": user}
         to = [get_user_email(user)]
         PasswordChangedConfirmationEmail(self.request, context).send(to)
         response_data['response'] = 'password reset done successfully.'
     else:
         response_data = serializer.errors
     return Response(response_data)
コード例 #2
0
 def post(self, request, *args, **kwargs):
     response_data = {}
     serializer = ResetPasswordSerializer(data=request.data)
     if serializer.is_valid(raise_exception=True):
         user = serializer.validated_data["user"]
         if user:
             context = {"user": user}
             to = [get_user_email(user)]
             PasswordResetEmail(self.request, context).send(to)
             response_data["response"] = "password reset link has been sent"
     else:
         response_data = serializer.errors
     return Response(response_data)
コード例 #3
0
 def post(self, request):
     response_data = {}
     serializer = ChangePasswordSerializer(
         data=request.data, context={'request': request.auth})
     if serializer.is_valid(raise_exception=True):
         data = serializer.validated_data
         new_password = data["new_password"]
         self.request.user.set_password(new_password)
         self.request.user.save()
         context = {"user": self.request.user}
         to = [get_user_email(self.request.user)]
         PasswordChangedConfirmationEmail(self.request, context).send(to)
         response_data["response"] = "password changed successfully"
     else:
         response_data = serializer.errors
     return Response(response_data)
コード例 #4
0
 def post(self, request, *args, **kwargs):
     response_date = {}
     serializer = UserActivationSerializer(data=request.data)
     if serializer.is_valid(raise_exception=True):
         user = serializer.validated_data
         user.is_active = True
         user.email_verified = True
         user.save()
         signals.user_activated.send(sender=self.__class__,
                                     user=user,
                                     request=self.request)
         context = {"user": user}
         to = [get_user_email(user)]
         ConfirmationEmail(self.request, context).send(to)
         response_date["response"] = "user activated succefully"
     else:
         response_date = serializer.errors
     return Response(response_date)
コード例 #5
0
    def post(self, request, format=None):
        response_data = {}
        serializer = UserRegistrationSerializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            account = serializer.save()
            response_data['response'] = 'successfully registered new user.'
            response_data['email'] = account.email
            response_data['username'] = account.username
            response_data['pk'] = account.pk
            signals.user_registered.send(sender=self.__class__,
                                         user=account,
                                         request=self.request)
            #after creating user send and activation mail to user
            context = {"user": account}
            to = [get_user_email(account)]
            ActivationEmail(self.request, context).send(to)
        else:
            response_data = serializer.errors

        return Response(response_data)
コード例 #6
0
    def test_view_register(self):
        view_name = 'account_register'

        email = create_test_email()
        password = create_test_password()

        user = get_user_by_email(email)
        self.assertIsNone(user,
                          'The user with email [%s] should not exist before registration.' %
                          email)

        params = {
            'email' : email,
            'password1' : password,
            'password2' : password,
        }
        response = self._post(view_name, params=params, follow=True)
        redirect_chain = response.redirect_chain
        self.assertTrue(len(redirect_chain) > 0,
                        'Registration should have succeeded')

        user = get_user_by_email(email)
        self.assertIsNotNone(user,
                             'The user with email [%s] should exist after registration.' %
                             email)

        self.assertFalse(user.is_active,
                         'User should not be active until email address is confirmed')
        self.assertEqual(email,
                         user.email,
                         'User email does not match registration email')

        # should not be able to register twice
        response = self._post(view_name, params=params, follow=True)
        redirect_chain = response.redirect_chain
        self.assertTrue(len(redirect_chain) == 0,
                        'Repeat registration should have failed.')
        self.assertEqual(200,
                         response.status_code,
                         'Repeat registration should have failed.')

        
        client = Client()
        login_success = client.login(username=user.username, password=password)
        self.assertFalse(login_success,
                        'Should not be able to log in before account is activated, after registration')

        user_email = get_user_email(user, email)
        user_email.confirm_and_activate_account()

        client = Client()
        login_success = client.login(username=user.username, password=password)
        self.assertTrue(login_success,
                        'Should be able to log in after account is activated, after registration')

        # register post when already logged in
        # this can happen if someone is logged out, goes to register page
        # and then logs in in a separate tab, leaving register page open
        self._check_view_redirects_to_another(view_name,
                                              'account_home',
                                              client=client,
                                              params=params)