Ejemplo n.º 1
0
def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                if user.groups.filter(name='dev').exists():
                    return HttpResponseRedirect('../developer')
                elif user.groups.filter(name='player'):
                    return HttpResponseRedirect('../player')
            else:
                return render(
                    request, 'login.html', {
                        'form': form,
                        'msg': 'Username or password is not correct!',
                        'can_not_login': True
                    })
        else:
            return render(
                request, 'login.html', {
                    'form': form,
                    'msg': 'Username or password is not correct!',
                    'can_not_login': True
                })
    form = LoginForm()
    return render(request, 'login.html', {
        'form': form,
        'can_not_login': False
    })
Ejemplo n.º 2
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = Users.query.filter_by(username=form.username.data).first()
        if user and bc.check_password_hash(user.password, form.password.data):
            login_user(user, False)
            next_page = request.args.get('next')
            if next_page:
                return redirect(next_page)
            return redirect('/home')
    return render_template("login.html", form=form)
Ejemplo n.º 3
0
def parent_category_view(request, parent_category_slug):
    parrentCategories = ParentCategory.objects.all()
    categories = Category.objects.all()
    login_my = LoginForm(request.POST or None)
    if login_my.is_valid():
        username = login_my.cleaned_data['username']
        password = login_my.cleaned_data['password']
        login_user = authenticate(username=username, password=password)
        if login_user:
            login(request, login_user)
            return HttpResponseRedirect(reverse('base'))

    register = RegistrationForm(request.POST or None)
    if register.is_valid():
        new_user = register.save(commit=False)
        username = login_my.cleaned_data['username']
        email = register.cleaned_data['email']
        password = register.cleaned_data['password']
        first_name = register.cleaned_data['first_name']
        last_name = register.cleaned_data['last_name']
        new_user.email = email
        new_user.set_password(password)
        new_user.first_name = first_name
        new_user.last_name = last_name
        new_user.save()
        login_user = authenticate(username=username, password=password)
        if login_user:
            login(request, login_user)
            return HttpResponseRedirect(reverse('base'))
    try:
        cart_id = request.session['cart_id']
        cart = Cart.objects.get(id=cart_id)
        request.session['total'] = cart.items.count()
    except:
        cart = Cart()
        cart.save()
        cart_id = cart.id
        request.session["cart_id"] = cart_id
        cart = Cart.objects.get(id=cart_id)
    try:
        parrentCategory = ParentCategory.objects.get(slug=parent_category_slug)
    except ParentCategory.DoesNotExist:
        parrentCategory = ParentCategory.objects.all()

    context = {
        'parrentCategories': parrentCategories,
        'categories': categories,
        'parrentCategory': parrentCategory,
        'cart': cart,
        'register': register,
        'login_my': login_my
    }
    return render(request, 'parrentCategory.html', context)
Ejemplo n.º 4
0
def login():
	if current_user.is_authenticated:
		return redirect(url_for('home'))
	form = LoginForm()
	if form.validate_on_submit():
		employee = Employee.query.filter_by(email=form.email.data.lower()).first()
		if employee and bcrypt.check_password_hash(employee.password, form.password.data):
			login_user(employee, remember=form.remember.data)
			session['ROLE'] = current_user.role
			next_page = request.args.get('next')
			return redirect(next_page) if next_page else redirect(url_for('home'))
		else:
			flash('Invalid credentials. Try Again.', 'danger')
	return render_template('login.html', title='Login', form=form)
Ejemplo n.º 5
0
def login_user(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(request, username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect("/profile/")
    else:
        form = LoginForm()
    context = {'form': form}
    return render(request, 'store/login.html', context)
Ejemplo n.º 6
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = Users.query.filter_by(email=form.email.data).first(
        )  #Hvis formen er valid, find den user i databasen som har den givne email
        if user and bc.check_password_hash(
                user.password, form.password.data
        ):  #Hvis useren findes, og passwordet giver samme hash som det gemt i databasen
            login_user(user, False)  #Login den givne user, remember me=False
            return redirect(
                '/home'
            )  #return et redirect til /home efter login er succesfuldt
    return render_template(
        "login.html", form=form
    )  #Hvis formen ikke er valid, render 'login.html', giv form som skal renderes
Ejemplo n.º 7
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(
                url_for('home'))
        else:
            flash(f'Loggin Faild, Please check email and password', 'danger')
    return render_template('login.html', title='Login', form=form)
Ejemplo n.º 8
0
def sign_in(request):
    error = ""
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            user = authenticate(username=form.cleaned_data['username'],
                                password=form.cleaned_data['password'])
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return redirect("/buy/")
                return HttpResponse('Disabled account')
            error = 'Invalid login or password'
    return render(request, 'store/login.html', {
        'form': LoginForm(),
        "error": error
    })
Ejemplo n.º 9
0
def login(request, userType):
    if request.method == 'POST':
        loginForm = LoginForm(request.POST, request.FILES)
        if loginForm.is_valid():
            username = loginForm.cleaned_data['username']
            password = loginForm.cleaned_data['password']
            sql = "SELECT Password FROM store_%s WHERE Email = '%s'" % (userType, username)
            with connection.cursor() as cursor:
                cursor.execute(sql)
                pw = cursor.fetchone()
                if pw and pw[0] == password:
                    request.session['loggedIn'] = True
                    request.session['userType'] = userType
                    request.session['userName'] = username
                    return redirect('store_front')
    else:
        loginForm = LoginForm()
    return render(request, 'store/login.html', {'loginForm': loginForm, 'userType': userType})
Ejemplo n.º 10
0
def account_view(request):
    parrentCategories = ParentCategory.objects.all()
    categories = Category.objects.all()
    order = Order.objects.filter(user=request.user)
    order_active = order.filter(status='Принят в обработку').order_by('-id')
    order_finished = order.filter(status='Выполнен').order_by('-id')
    login_my = LoginForm(request.POST or None)
    if login_my.is_valid():
        username = login_my.cleaned_data['username']
        password = login_my.cleaned_data['password']
        login_user = authenticate(username=username, password=password)
        if login_user:
            login(request, login_user)
            return HttpResponseRedirect(reverse('base'))

    register = RegistrationForm(request.POST or None)
    if register.is_valid():
        new_user = register.save(commit=False)
        username = login_my.cleaned_data['username']
        email = register.cleaned_data['email']
        password = register.cleaned_data['password']
        first_name = register.cleaned_data['first_name']
        last_name = register.cleaned_data['last_name']
        new_user.email = email
        new_user.set_password(password)
        new_user.first_name = first_name
        new_user.last_name = last_name
        new_user.save()
        login_user = authenticate(username=username, password=password)
        if login_user:
            login(request, login_user)
            return HttpResponseRedirect(reverse('base'))
    conetext = {
        'order': order,
        'parrentCategories': parrentCategories,
        'categories': categories,
        'register': register,
        'login_my': login_my,
        'order_active': order_active,
        'order_finished': order_finished
    }
    return render(request, 'account.html', conetext)
Ejemplo n.º 11
0
def signin_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)

        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = authenticate(request, username=email, password=password)

            if user is not None:
                login(request, user)
                # return HttpResponseRedirect(reverse('profile_view'))
                return redirect('home')
            else:
                return redirect('signup_view')

    else:
        form = LoginForm()

    return render(request, 'login.html', {'form': form})
Ejemplo n.º 12
0
def login_user(request):
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            user = authenticate(request,
                                password=form.data.get('password'),
                                username=form.data.get('username'))

            if user is not None:
                login(request, user)
                if request.GET.get('next'):
                    return redirect(request.GET.get('next'))
                return redirect('home')
            else:
                return render(request, 'login.html',
                              {'error': 'Invalid password'})

    elif request.method == "GET":
        return render(request, 'login.html')
    else:
        return HttpResponseBadRequest()