Ejemplo n.º 1
0
def register(request):
    """
    Handle new user registration
    """
    # Only allow users to access register page if they are not logged in
    if not request.user.is_anonymous():
        return redirect('user:profile', username=request.user.username)

    # Disallow access to this page if open signup is disabled
    if not hasattr(settings, 'ENABLE_OPEN_SIGNUP') or not settings.ENABLE_OPEN_SIGNUP:
        return redirect('accounts:login')

    # Handle form
    if request.method == 'POST' and 'register-submit' in request.POST:
        # Create form bound to request data
        form = RegisterForm(request.POST)

        # Validate the form
        if form.is_valid():
            # Validate username and password using form methods
            username = form.clean_username()
            email = form.clean_email()
            password = form.clean_password2()

            # If no exceptions raised to here, then the username is unique and the passwords match.
            # Commit the new user to database
            form.save()

            # Authenticate the new user
            user = authenticate(username=username, password=password)

            if user is not None:
                # The password has been verified for the user
                if user.is_active:
                    # The user is valid, active, and authenticated, so login in the user
                    login(request, user)

                    # Redirect after logged in using next parameter or default to user profile
                    if 'next' in request.GET:
                        return redirect(request.GET['next'])
                    else:
                        return redirect('user:profile', username=user.username)
                else:
                    # The password is valid, but the user account has been disabled
                    # Return a disabled account 'error' message
                    messages.error(request, "Sorry, but your account has been disabled. Please contact the site "
                                            "administrator for more details."
                    )
            else:
                # User was not authenticated, return errors
                 messages.warning(request, "Whoops! We were not able to log you in. Please check your username and "
                                           "password and try again."
                 )

    else:
        # Create new empty form
        form = RegisterForm()

    context = {'form': form}
    return render(request, 'tethys_portal/accounts/register.html', context)
Ejemplo n.º 2
0
def register(request):
    """
    Handle new user registration
    """
    # Only allow users to access register page if they are not logged in
    if not request.user.is_anonymous:
        return redirect('user:profile', username=request.user.username)

    # Disallow access to this page if open signup is disabled
    if not hasattr(settings, 'ENABLE_OPEN_SIGNUP') or not settings.ENABLE_OPEN_SIGNUP:
        return redirect('accounts:login')

    # Handle form
    if request.method == 'POST' and 'register-submit' in request.POST:
        # Create form bound to request data
        form = RegisterForm(request.POST)

        # Validate the form
        if form.is_valid():
            # Validate username and password using form methods
            username = form.clean_username()
            email = form.clean_email()  # noqa: F841
            password = form.clean_password2()

            # If no exceptions raised to here, then the username is unique and the passwords match.
            # Commit the new user to database
            form.save()

            # Authenticate the new user
            user = authenticate(request, username=username, password=password)

            if user is not None:
                # The password has been verified for the user
                if user.is_active:
                    # The user is valid, active, and authenticated, so login in the user
                    login(request, user)

                    # Redirect after logged in using next parameter or default to user profile
                    if 'next' in request.GET:
                        return redirect(request.GET['next'])
                    else:
                        return redirect('user:profile', username=user.username)
                else:
                    # The password is valid, but the user account has been disabled
                    # Return a disabled account 'error' message
                    messages.error(request, "Sorry, but your account has been disabled. Please contact the site "
                                            "administrator for more details.")
            else:
                # User was not authenticated, return errors
                messages.warning(request, "Whoops! We were not able to log you in. Please check your username and "
                                          "password and try again.")

    else:
        # Create new empty form
        form = RegisterForm()

    context = {'form': form}
    return render(request, 'tethys_portal/accounts/register.html', context)
Ejemplo n.º 3
0
    def test_RegisterForm_save(self):
        register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abc123',
                         'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response}

        register_form = RegisterForm(data=register_data)

        ret = register_form.save()

        # Also try to get from database after it's saved
        ret_database = User.objects.get(username='******')

        # Check result
        self.assertIsInstance(ret, User)
        self.assertIsInstance(ret_database, User)
        self.assertEqual('user1', ret.username)
        self.assertEqual('user1', ret_database.username)