Ejemplo n.º 1
0
    def suggest_products_for(self, products, max_results=3):
        product_ids = [p.id for p in products]
        if len(products) == 1:
            suggestions = r.zrange(self.get_product_key(product_ids[0]),
                                   0,
                                   -1,
                                   desc=True)[:max_results]

        else:
            flat_ids = ''.join([str(id) for id in product_ids])
            tmp_key = 'tmp_{}'.format(flat_ids)
            keys = [self.get_product_key(id) for id in product_ids]
            r.zunionstore(tmp_key, keys)
            r.zrem(tmp_key, *product_ids)
            suggestions = r.zrange(tmp_key, 0, -1, desc=True)[:max_results]
            r.delete(tmp_key)

        suggested_products_ids = [int(id) for id in suggestions]

        suggested_products = list(
            Product.query.filter(Product.id.in_(suggested_products_ids)))
        suggested_products.sort(
            key=lambda x: suggested_products_ids.index(x.id))

        return suggested_products
Ejemplo n.º 2
0
def product(id):

    if 'cart' not in session:
        session['cart'] = {}

    product = Product.query.filter_by(id=id).first()
    cart_product = None
    form = AddToCart()

    # List most popular items
    product_ranking = r.zrange('product_ranking', 0, -1, desc=True)[:3]
    product_ranking_ids = [int(id) for id in product_ranking]

    most_popular = list(
        Product.query.filter(Product.id.in_(product_ranking_ids)))
    most_popular.sort(key=lambda x: product_ranking_ids.index(x.id))

    # List recommended items
    recommended_products = Recommender().suggest_products_for([product])

    # Add item to cart

    if form.validate_on_submit():
        try:
            cart_product = Product.query.filter_by(size=form.data['size'],
                                                   name=product.name).first()

            if cart_product is None:
                raise Exception(
                    "We're sorry, but that size is currently out of stock")
            elif form.data['quantity'] > cart_product.stock:
                raise Exception(
                    "We're sorry, but we do not have that much in stock!")

        except Exception as e:
            # Flash message that product is not available for whatever reason
            flash(str(e))
            return redirect(url_for('product', id=product.id))

        cart_product_id = str(cart_product.id)

        if cart_product_id in session['cart']:
            session['cart'][cart_product_id]['quantity'] += form.data[
                'quantity']
        else:
            session['cart'][cart_product_id] = {
                'quantity': form.data['quantity']
            }

        session.modified = True

        return redirect(url_for('cart_list'))

    return render_template('product.html',
                           product=product,
                           form=form,
                           most_popular=most_popular,
                           recommended=recommended_products)
Ejemplo n.º 3
0
Archivo: 123.py Proyecto: JJYYYY/policy
def index():
    ip = request.remote_addr
    useragent = request.headers.get('User-Agent')
    m = hashlib.md5()
    m.update(bytes(ip + useragent, encoding="utf-8"))
    cookie = m.hexdigest()
    search_history = r.zrange(name=cookie, start=0, end=-1, desc=True, withscores=True)
    search_history = [i[0].decode("utf8") for i in search_history]
    items = []
    for i in search_history:
        if i not in items:
            items.append(i)
    if len(items) > 10:
        items = items[0:10]
    context = {
        "items": items
    }
    response = make_response(render_template("v1/index.html", **context))
    response.set_cookie("id", cookie)
    return response