Example #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)
Example #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)
Example #3
0
    def test_RegisterForm_clean_username(self):
        register_data = {'username': '******', 'email': '*****@*****.**', 'password1': 'abc123',
                         'password2': 'abc123', 'captcha_0': self.hashkey, 'captcha_1': self.response}

        register_form = RegisterForm(data=register_data)

        self.assertTrue(register_form.is_valid())

        ret = register_form.clean_username()

        self.assertEqual('user', ret)