Ejemplo n.º 1
0
 def test_LoginForm_invalid_username(self):
     login_data = {'username': '******', 'password': '******', 'captcha_0': self.hashkey,
                   'captcha_1': self.response}
     login_form = LoginForm(login_data)
     err_msg = "This value may contain only letters, numbers and @/./+/-/_ characters."
     self.assertEqual(login_form.errors['username'], [err_msg])
     self.assertFalse(login_form.is_valid())
Ejemplo n.º 2
0
def login_view(request):
    """
    Handle login
    """
    # Only allow users to access login page if they are not logged in
    if not request.user.is_anonymous():
        return redirect('user:profile', username=request.user.username)

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

        # Validate the form
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

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

            # If not authenticated, user will be None
            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('app_library')
                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 login form
        form = LoginForm()

    # Determine if signup is disabled or not
    signup_enabled = settings.ENABLE_OPEN_SIGNUP if hasattr(
        settings, 'ENABLE_OPEN_SIGNUP') else False

    context = {'form': form, 'signup_enabled': signup_enabled}

    return render(request, 'tethys_portal/accounts/login.html', context)
Ejemplo n.º 3
0
 def test_LoginForm_invalid(self):
     login_invalid_data = {
         'username': '******',
         'password': '******',
         'captcha_0': self.hashkey,
         'captcha_1': ''
     }
     login_form = LoginForm(login_invalid_data)
     self.assertFalse(login_form.is_valid())
Ejemplo n.º 4
0
 def test_LoginForm(self):
     login_data = {
         'username': '******',
         'password': '******',
         'captcha_0': self.hashkey,
         'captcha_1': self.response
     }
     login_form = LoginForm(login_data)
     self.assertTrue(login_form.is_valid())
Ejemplo n.º 5
0
def login_view(request):
    """
    Handle login
    """
    # Only allow users to access login page if they are not logged in
    if not request.user.is_anonymous():
        return redirect('user:profile', username=request.user.username)

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

        # Validate the form
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

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

            # If not authenticated, user will be None
            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('app_library')
                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 login form
        form = LoginForm()

    # Determine if signup is disabled or not
    signup_enabled = settings.ENABLE_OPEN_SIGNUP if hasattr(settings, 'ENABLE_OPEN_SIGNUP') else False

    context = {'form': form,
               'signup_enabled': signup_enabled}

    return render(request, 'tethys_portal/accounts/login.html', context)