Ejemplo n.º 1
0
def basket():
    session = db_session.create_session()
    user = session.query(User).filter(User.id == current_user.id).first()
    basket = session.query(Order).filter(Order.client_id == user.id,
                                         Order.status == 0).first()
    basket.update_total_cost()
    session.commit()
    if request.method == 'POST':
        req_form = dict(request.form)
        if req_form['act'] == 'up':
            position = session.query(Position).filter(
                Position.id == int(req_form['position_id'])).first()
            position.count += 1
            position.update_point_cost()
            session.merge(position)
            session.commit()
        elif req_form['act'] == 'down':
            position = session.query(Position).filter(
                Position.id == int(req_form['position_id'])).first()
            position.count = max(0, position.count - 1)
            position.update_point_cost()
            session.merge(position)
            if position.count == 0:
                session.delete(position)
            session.commit()
        elif req_form['act'] == 'delete':
            position = session.query(Position).filter(
                Position.id == int(req_form['position_id'])).first()
            session.delete(position)
            session.commit()
        elif req_form['act'] == 'do order':
            basket.address_data = req_form['address']
            basket.payment_way = req_form['pay_radio']
            basket.commentary = req_form['comment-offer']
            basket.order_date = datetime.today().date()
            basket.status += 1
            session.merge(basket)
            session.commit()
            basket = Order(client=user)
            basket.update_total_cost()
            session.add(basket)
            session.commit()
        return redirect('/basket')
    return render_template('basket.html',
                           title='Корзина',
                           current_user=user,
                           basket=basket)
Ejemplo n.º 2
0
def client_login():
    form = ClientLoginForm()
    if form.validate_on_submit():
        session = db_session.create_session()
        user = session.query(User).filter(User.role == 'client').filter(
            User.phone_number == form.phone_number.data).first()
        if user:
            login_user(user, remember=True)
            return redirect('/')
        user = User(phone_number=form.phone_number.data, role='client')
        cart = Order(client=user)
        cart.update_total_cost()
        session.add(user)
        session.add(cart)
        session.commit()
        login_user(user, remember=True)
        return redirect('/')
    return render_template('login.html', title='Авторизация', form=form)