Ejemplo n.º 1
0
 def test_invalid_form(self):
     data = {
         'username': '',
         'password': '',
     }
     form = LoginForm(data=data)
     self.assertFalse(form.is_valid())
Ejemplo n.º 2
0
def login(request):
    """Backend for AJAX login.

    POST:
    username -- username to login
    password -- password of the username

    Renders JSON: (besides 'status' or 'err')
    username -- (on 'OK') username of the logged in user
    name     -- (on 'OK') name of the logged in user
    detail   -- (on 'Error') error list from form validation

    POST data will be validated by LoginForm.
    """
    form = LoginForm(request.POST)
    if form.is_valid():
        user = auth.authenticate(
            username=form.cleaned_data['username'],
            password=form.cleaned_data['password'],
            )
        if user is not None:
            if user.is_active:
                try:
                    user.myuser
                except MyUser.DoesNotExist as err:
                    return render_JSON_Error('Root cannot login here.')
                auth.login(request, user)
                return render_JSON_OK({
                    'username': user.username,
                    'name': user.myuser.name,
                    })
        return render_JSON_Error('Login failed.')
    return render_JSON_Error('Login syntax error.', {
        'detail': form.errors,
        })
Ejemplo n.º 3
0
 def test_valid_form(self):
     username = self.user.username
     password = self.user.password
     data = {
         'username': username,
         'password': password,
     }
     form = LoginForm(data=data)
     self.assertTrue(form.is_valid())
Ejemplo n.º 4
0
def auth(request):
    form = LoginForm(request.POST)
    if form.is_valid():
        d = form.cleaned_data
        print(d)
    else:
        print('Some field missing')
    # username = request.POST['username']
    # password = request.POST['password']
    '''l = Student.objects.filter(username=username, password=password)
Ejemplo n.º 5
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, rember=form.remember.data)
            flash(f'Logged In as email')
            return redirect(url_for('./about'))
        else:
            flash(f"Login unsuccessful. Please Check Email and Password")
    return render_template('login.html', title='login', form=form)
Ejemplo n.º 6
0
def login():
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        user = User.query.filter(User.username == form.username.data).first()
        if user is None:
            form.username.errors.append('Invalid username')
        elif md5(app.config['MD5_SALT'] + form.password.data).hexdigest() != user.password:
            form.password.errors.append('Invalid password')
        else:
            session['logged_in'] = True
            # session['moderator'] = user.is_moderator
            session['username'] = user.username
            flash('Logged in successfully')
            return redirect(url_for('main_page'))
    return render_template('login.html',  form=form)
Ejemplo n.º 7
0
def user_login(request):
    if request.user.is_authenticated:
        return HttpResponseRedirect('/')

    state = None

    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = auth.authenticate(username=username, password=password)

        if user:
            if user.is_active:
                auth.login(request, user)
                return HttpResponseRedirect('/')
            else:
                return HttpResponse(u'Your account is disabled.')
        else:
            state = 'not_exist_or_password_error'

    context = {
        'loginForm': LoginForm(),
        'state': state,
    }

    return render(request, 'library/login.html', context)
Ejemplo n.º 8
0
def login():
    form = LoginForm()
    if request.method == 'POST' and form.validate_on_submit():

        user = app.config['USERS_COLLECTION'].find_one(
            {"_id": form.username.data})
        if user and User.validate_login(user['password'], form.password.data):
            # user_obj = User(user['_id'], user['couple'])
            user_obj = User(user)
            login_user(user_obj)
            flash("Logged in successfully!", category='success')

            return redirect(request.args.get("next") or url_for("home"))

        flash("Wrong username or password!", category='error')

    return render_template('login.html', title='login', form=form)
Ejemplo n.º 9
0
def show_login(request):
    loginform = LoginForm()
    return render(request, 'library/public/login.html', {'form': loginform})