Beispiel #1
0
def home():
    form = LoginForm()
    search_form = SearchForm()
    products = Product.query.all()
    categories = Category.query.all()
    brands = Brand.query.all()

    if "cart" in session:
        items = session["cart"]
    else:
        items = 0

    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and check_password_hash(user.password, form.password.data):
            login_user(user)
            next_page = request.args.get('next')
            flash(f'Welcome back, { user.fname }', 'success')
            return redirect(next_page) if next_page else redirect(
                url_for('home'))
        else:
            flash(f'Login unsuccessful. Either email or password is wrong!',
                  'warning')
            return redirect('{}?openLogin'.format(url_for('home')))
    if search_form.validate_on_submit():
        to_search = re.sub('[^A-Za-z0-9]+', '',
                           search_form.search_pattern.data)
        products = Product.query.filter(Product.name.contains(to_search)).all()
        categories = Category.query.all()
        brands = Brand.query.all()
        page_title = 'Search results for {}'.format(to_search)
        return render_template('searchresults.html',
                               title=page_title,
                               products=products,
                               form=form,
                               categories=categories,
                               brands=brands,
                               pattern=to_search,
                               search_form=search_form,
                               items=items)
    return render_template('index.html',
                           title='Home',
                           form=form,
                           products=products,
                           categories=categories,
                           brands=brands,
                           search_form=search_form,
                           items=items)
Beispiel #2
0
def checkout():
    search_form = SearchForm()
    checkout_form = CheckOut()
    if search_form.validate_on_submit():
        to_search = re.sub('[^A-Za-z0-9]+', '',
                           search_form.search_pattern.data)
        products = Product.query.filter(Product.name.contains(to_search)).all()
        categories = Category.query.all()
        brands = Brand.query.all()
        page_title = 'Search results for {}'.format(to_search)
        return render_template('checkout.html',
                               title=page_title,
                               products=products,
                               categories=categories,
                               brands=brands,
                               pattern=to_search,
                               search_form=search_form)
    if "cart" not in session:
        items = []
        return render_template('checkout.html',
                               title="Checkout",
                               search_form=search_form,
                               checkout_form=checkout_form,
                               items=items)
    else:
        items = session["cart"]
        cart = {}
        total_price = 0
        total_quantity = 0
        counter = dict((i, items.count(i)) for i in items)
        item_count = 0
        for item in items:
            item_count = item_count + 1
            product = Product.query.get_or_404(item)
            discounted_price = 0
            if str(product.is_discounted) == '1':
                discounted_price = int(
                    count_new_price(product.price,
                                    product.discount_percentage))
                total_price += discounted_price
            else:
                total_price += product.price

            total_quantity = counter[product.id]
            if str(product.is_discounted) == '1':
                cart[product.id] = {
                    "quantity":
                    1,
                    "title":
                    product.name,
                    "id":
                    product.id,
                    "price":
                    int(
                        count_new_price(product.price,
                                        product.discount_percentage)),
                    "image":
                    get_first_image(product.images),
                    "total_quantity":
                    total_quantity
                }
            else:
                cart[product.id] = {
                    "quantity": 1,
                    "title": product.name,
                    "id": product.id,
                    "price": product.price,
                    "image": get_first_image(product.images),
                    "total_quantity": total_quantity
                }
        return render_template('checkout.html',
                               title="Checkout",
                               search_form=search_form,
                               checkout_form=checkout_form,
                               display_cart=cart,
                               total=round(total_price, 2),
                               item_count=item_count)
Beispiel #3
0
def viewcart():
    search_form = SearchForm()
    if search_form.validate_on_submit():
        to_search = re.sub('[^A-Za-z0-9]+', '',
                           search_form.search_pattern.data)
        products = Product.query.filter(Product.name.contains(to_search)).all()
        categories = Category.query.all()
        brands = Brand.query.all()
        page_title = 'Search results for {}'.format(to_search)
        return render_template('searchresults.html',
                               title=page_title,
                               products=products,
                               categories=categories,
                               brands=brands,
                               pattern=to_search,
                               search_form=search_form)
    if "cart" not in session:
        flash('There is nothing in your cart!', 'info')
        return render_template("viewcart.html",
                               display_cart={},
                               total=0,
                               search_form=search_form)
    else:
        items = session["cart"]
        cart = {}
        total_price = 0
        total_quantity = 0
        counter = dict((i, items.count(i)) for i in items)
        item_count = 0
        for item in items:
            item_count = item_count + 1
            product = Product.query.get_or_404(item)
            discounted_price = 0
            if str(product.is_discounted) == '1':
                discounted_price = int(
                    count_new_price(product.price,
                                    product.discount_percentage))
                total_price += discounted_price
            else:
                total_price += product.price

            total_quantity = counter[product.id]
            if str(product.is_discounted) == '1':
                cart[product.id] = {
                    "quantity":
                    1,
                    "title":
                    product.name,
                    "price":
                    int(
                        count_new_price(product.price,
                                        product.discount_percentage)),
                    "image":
                    get_first_image(product.images),
                    "total_quantity":
                    total_quantity
                }
            else:
                cart[product.id] = {
                    "quantity": 1,
                    "title": product.name,
                    "price": product.price,
                    "image": get_first_image(product.images),
                    "total_quantity": total_quantity
                }
        global cart_price, cart_num
        cart_price = round(total_price, 2)
        cart_num = item_count
        return render_template('viewcart.html',
                               title='View Cart',
                               search_form=search_form,
                               display_cart=cart,
                               total=round(total_price, 2),
                               item_count=item_count)

    return render_template('viewcart.html',
                           title='View Cart',
                           search_form=search_form)
Beispiel #4
0
def wishlist():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and check_password_hash(user.password, form.password.data):
            login_user(user)
            next_page = request.args.get('next')
            flash(f'Welcome back, { user.fname }', 'success')
            return redirect(next_page) if next_page else redirect(
                url_for('home'))
        else:
            flash(f'Login unsuccessful. Either email or password is wrong!',
                  'warning')
            return redirect('{}?openLogin'.format(url_for('home')))
    search_form = SearchForm()
    if search_form.validate_on_submit():
        to_search = re.sub('[^A-Za-z0-9]+', '',
                           search_form.search_pattern.data)
        products = Product.query.filter(Product.name.contains(to_search)).all()
        categories = Category.query.all()
        brands = Brand.query.all()
        page_title = 'Search results for {}'.format(to_search)
        return render_template('searchresults.html',
                               title=page_title,
                               products=products,
                               categories=categories,
                               brands=brands,
                               pattern=to_search,
                               search_form=search_form,
                               form=form)
    if "wish" not in session:
        flash('There is nothing in wishlist!', 'info')
        return render_template("wishlist.html",
                               wishlist_arr={},
                               total=0,
                               search_form=search_form,
                               form=form)
    else:
        items = session["wish"]
        wishlist = {}
        total_price = 0
        total_quantity = 0
        counter = dict((i, items.count(i)) for i in items)
        item_count = 0
        for item in items:
            item_count = item_count + 1
            product = Product.query.get_or_404(item)
            discounted_price = 0
            if str(product.is_discounted) == '1':
                discounted_price = int(
                    count_new_price(product.price,
                                    product.discount_percentage))
                total_price += discounted_price
            else:
                total_price += product.price

            total_quantity = counter[product.id]
            if str(product.is_discounted) == '1':
                wishlist[product.id] = {
                    "quantity":
                    1,
                    "title":
                    product.name,
                    "price":
                    int(
                        count_new_price(product.price,
                                        product.discount_percentage)),
                    "image":
                    get_first_image(product.images),
                    "total_quantity":
                    total_quantity
                }
            else:
                wishlist[product.id] = {
                    "quantity": 1,
                    "title": product.name,
                    "price": product.price,
                    "image": get_first_image(product.images),
                    "total_quantity": total_quantity
                }
        global wish_counter
        wish_counter = total_quantity
        return render_template('wishlist.html',
                               title='Wishlist',
                               search_form=search_form,
                               wishlist_arr=wishlist,
                               total=round(total_price, 2),
                               item_count=item_count,
                               form=form)

    return render_template('wishlist.html',
                           title='Wishlist',
                           search_form=search_form,
                           form=form)