Exemplo n.º 1
0
def remove_from_cart(user, product):
    sess = create_session()
    user = sess.query(User).filter(User.login == user)
    product = sess.query(Product).filter(Product.name == product)
    user.products.remove(product)
    product.amount += 1
    product.is_aviable = (bool(max(0, product.amount)))

    sess.commit()
Exemplo n.º 2
0
def add_to_cart(id):
    form = ProductForm()
    db_sess = db_session.create_session()
    prod = db_sess.query(Product).filter(Product.id == id).first()
    user = current_user
    amount = form.amount.data
    user.products.append(prod, amount)
    db_sess.merge(user)
    db_sess.commit()
    return render_template('added_.html')
Exemplo n.º 3
0
def create_product(name, pics, content):
    sess = create_session()
    if not sess.query(Product).filter(Product.name == name).count():
        product = Product()
        product.name = name
        product.pics = pics
        product.content = content
        sess.add(product)
        sess.commit()
        return sess.query(Product).filter(Product.name == name).first().id
    else:
        raise AddError('Invalid name')
Exemplo n.º 4
0
def confirm(token):
    logout_user()
    id = User.verify_token(token)
    db_sess = db_session.create_session()
    user = load_user(id)
    if not user:
        return render_template('expired_.html')
    user.is_confirmed = True
    db_sess.merge(user)
    db_sess.commit()
    login_user(user)
    return render_template('confirmed_.html')
Exemplo n.º 5
0
def search_in_db(name):
    words = name.split()
    for i in words:
        if not i:
            words.pop(words.index(''))
    db_sess = db_session.create_session()
    if not words:
        return db_sess.query(Product).all()
    q = db_sess.query(Product).filter(Product.name.like(f'%{words[0]}%'))
    for i in words[1:]:
        q = q.filter(Product.name.like(f'%{i}%'))
    return q
Exemplo n.º 6
0
def init_reset():
    form = InitResetForm()
    db_sess = db_session.create_session()
    if form.validate_on_submit():
        user = db_sess.query(User).filter(
            User.email == form.email.data).first()
        if not user:
            return render_template('init_reset_.html',
                                   title='Сброс пароля',
                                   form=form)
        mail.send_reset_email(user)
        return render_template('please_check_email_.html')
    return render_template('init_reset_.html', title='Сброс пароля', form=form)
Exemplo n.º 7
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        user = db_sess.query(User).filter(User.login == form.name.data).first()
        if not user:
            return render_template('login_.html', title='', form=form)
        if user.check_password(form.password.data) and user.is_confirmed != 1:
            mail.send_confirmation_email(user)
            return render_template('please_confirm_.html', title='Confirm')
        elif user.check_password(form.password.data):
            login_user(user)
            return redirect('/')
    return render_template('login_.html', title='Вход', form=form)
Exemplo n.º 8
0
def reset(token):
    id = User.verify_token(token)
    user = load_user(id)
    if not user:
        return render_template('expired_.html')
    form = ResetForm()
    if form.validate_on_submit():
        if form.password.data != form.password_again.data:
            return render_template('reset_.html',
                                   title='Сброс пароля',
                                   form=form,
                                   message="Пароли не совпадают")
        db_sess = db_session.create_session()
        user.set_password(form.password.data)
        db_sess.merge(user)
        db_sess.commit()
        return redirect('/login')
    return render_template('reset_.html', title='Сброс пароля', form=form)
Exemplo n.º 9
0
def signup():
    form = RegisterForm()
    if form.validate_on_submit():
        if form.password.data != form.password_again.data:
            return render_template('signup_.html',
                                   title='Регистрация',
                                   form=form,
                                   message="Пароли не совпадают")
        db_sess = db_session.create_session()
        if db_sess.query(User).filter(User.email == form.email.data).count() or \
                db_sess.query(User).filter(User.login == form.name.data).count():
            return render_template('signup_.html',
                                   title='Регистрация',
                                   form=form,
                                   message="Такой пользователь уже есть")
        user = User(login=form.name.data, email=form.email.data)
        user.set_password(form.password.data)
        db_sess.add(user)
        db_sess.commit()
        return redirect('/login')
    return render_template('signup_.html', title='Регистрация', form=form)
Exemplo n.º 10
0
def change_amount(product, new):
    sess = create_session()
    product = sess.query(Product).filter(Product.name == product)
    product.amount = new
    sess.commit()
Exemplo n.º 11
0
def get_amount(product):
    sess = create_session()
    product = sess.query(Product).filter(Product.name == product)
    return product.amount
Exemplo n.º 12
0
def is_aviable(product):
    return create_session().query(Product).filter(
        Product.name == product).first().is_aviable
Exemplo n.º 13
0
def load_user(user_id):
    db_sess = db_session.create_session()
    return db_sess.query(User).filter(User.id == user_id).first()
Exemplo n.º 14
0
def product(id):
    db_sess = db_session.create_session()
    prod = db_sess.query(Product).filter(Product.id == id).first()
    return render_template('product_.html', **{'prod': prod})
Exemplo n.º 15
0
def get_best_prods(n, category="Акксесуары", page=1):
    db_sess = db_session.create_session()
    q = [db_sess.query(Product).get(i) for i in range(1, n + 1)]
    # q = [a[i] for i in range(1 + (page - 1) * 9, 10 + (page - 1) * 9)]
    # print(q)
    return q