コード例 #1
0
 def wrapper(*args, **kwargs):
     if not current_user.is_authenticated or current_user is None:
         flash(f'You must login to access that page!', 'danger')
         return redirect(url_for('auth.login'))
     if not current_user.active:
         flash(
             f'Your account has been deactivated. Please speak with your supervisor to regain access.',
             'danger')
         current_app.logger.warning(
             f"Authentication Failure: {current_user.username}'s account is inactive."
         )
         return redirect(url_for('auth.logout'))
     if not current_user.in_group(groups):
         message = ("Permissions Warning:\n"
                    f"    url = {request.url}\n"
                    f"    current_user = {current_user.username},\n"
                    f"    groups required = {groups}\n"
                    f"    {current_user.username} is not in the correct"
                    " group to access to this page.\n")
         current_app.logger.warning(message)
         flash(
             f'You are not authorized to visit that page!<br /><small>Groups allowed: <b>{groups}</b></small>',
             'danger')
         return redirect(url_for(
             'auth.login')) if not fail_redirect else fail_redirect
     return func(*args, **kwargs)
コード例 #2
0
ファイル: routes.py プロジェクト: treetrnk/flask_webstore
def select_order(obj_id):
    order = Order.query.filter_by(id=obj_id, status='Incomplete')
    if not current_user.in_group('admin'):
        order = order.filter_by(user_id=current_user.id)
    order = order.first()
    if not order:
        flash('Failed to select the requested cart.', 'warning')
        return redirect('auth.account')
    session['order_id'] = obj_id
    session['cart_item_count'] = order.total_items()
    return redirect(url_for('auth.account'))
コード例 #3
0
def index():
    if current_user.is_authenticated:
        if current_user.in_group('admin'):
            return redirect(url_for('admin.orders'))
        return redirect(url_for('main.index'))
    return redirect(url_for('auth.login'))
コード例 #4
0
ファイル: routes.py プロジェクト: treetrnk/flask_webstore
def product(obj_id, slug=''):
    product = Product.query.filter_by(id=obj_id).first()
    if not product.active:
        if not current_user.is_authenticated or not current_user.in_group(
                'admin'):
            flash(
                'The product you are looking for is either inactive or no longer available.',
                'warning')
            return redirect(url_for('shop.index'))
        else:
            flash(
                'This listing is inactive. You can only see this because you are an administrator.',
                'info')
    current_app.logger.debug(session)
    form = AddToCartForm()
    if form.validate_on_submit():
        # Validate Option/Product pairing
        option = Option.query.filter_by(id=form.option_id.data).first()
        if option.product_id != product.id:
            flash(
                'Unable to add item to cart. Please try again. If the problem persists, please contact us at <a href=""></a>.',
                'danger')
            return redirect(
                url_for('shop.product', obj_id=form.product_id.data))
        order = Order.query.filter_by(id=session.get('order_id'),
                                      status="Incomplete").first()
        if not order and current_user.is_authenticated:
            order = Order.query.filter_by(user_id=current_user.id,
                                          status='Incomplete').order_by(
                                              Order.created.desc()).first()
        if not order:
            order = Order()
            if current_user.is_authenticated:
                order.user_id = current_user.id
            db.session.add(order)
            db.session.commit()
            session['order_id'] = order.id

        option = Option.query.filter_by(id=form.option_id.data).first()

        # Validate option availability
        if order.in_cart(option.id):
            item = order.get_item(option.id)
            if item.amount == option.available:
                flash(
                    f'Unable to add more of the <b>{product.name} - {option.name}</b> to your cart. Your cart already has all availble stock for the selected option.',
                    'info')
                return redirect(
                    url_for('shop.product', obj_id=form.product_id.data))
            if item.amount + form.amount.data > option.available:
                added_amount = option.available - item.amount
                item.amount = option.available

                flash(
                    f'We only added {added_amount} of the <b>{product.name} - {option.name}</b> to your cart, since it is all we have available at the moment.',
                    'info')
            else:
                item.amount += form.amount.data
        else:
            item = Item(
                order_id=order.id,
                product_id=form.product_id.data,
                option_id=form.option_id.data,
                amount=form.amount.data,
            )
            db.session.add(item)
        db.session.commit()
        flash(
            f'<b>{product.name} - {option.name} (x{form.amount.data})</b> has been added to your cart.',
            'success')
        session['cart_item_count'] = order.total_items()
        return redirect(url_for('shop.cart'))
    form.product_id.data = product.id
    form.option_id.data = product.options[0].id if product.options else None
    return render_template(
        'shop/product.html',
        product=product,
        form=form,
        head_data=product.head_data(),
    )