Exemple #1
0
def my_context_processor():
    category_all = Category.query.all()
    categoryList = redis_cache.get('categoryList')
    if categoryList is None:
        categorys = Category.query.all()
        categoryList = []
        for category in categorys:
            category = Category.category_json(category)
            categoryList.append(category)
        json_dumps = json.dumps(categoryList, ensure_ascii=False)
        print(json_dumps)
        redis_cache.set("categoryList", json_dumps)
    else:
        categoryList = categoryList.decode('utf8')
        categoryList = json.loads(categoryList)
        print(categoryList)
    uid = session.get("uid")
    last_time = ""
    if uid is not None:
        user = User.query.get(uid)
        if user.shop_time is not None:
            result_time = datetime.now() - user.shop_time
            resultTime = str(result_time).split(':')
            # print(resultTime[0], resultTime[1], result_time, resultTime)
            if int(resultTime[0][0]) > 0 or int(resultTime[1]) >= 20:
                for shopCart in user.shopcarts:
                    product = Product.query.get(shopCart.pid)
                    product.counts = product.counts + shopCart.count
                    db.session.delete(shopCart)
                    db.session.commit()
                user.shop_time is None
                db.session.commit()
                last_time = ""
            else:
                second = resultTime[2]
                index = str(second).find('.')
                if index != -1:
                    second = second[0:index]
                last_time = "" + str(19 - int(resultTime[1])) + ":" + str(
                    60 - int(second))
        length = 0
        for shopCart in user.shopcarts:
            length += shopCart.count

        return {
            "uid": uid,
            "username": user.username,
            'user_img': user.img_url,
            "categorys": categoryList,
            "category_all": category_all,
            "last_time": last_time,
            "length": length
        }
    else:
        return {
            "categorys": categoryList,
            "category_all": category_all,
            "uid": "",
            "length": "0"
        }
Exemple #2
0
def index():
    print("主页。。。。。。。。。。。。")
    productList = Product.query.filter(Product.is_sell == 1,
                                       Product.is_pass == 2).order_by(
                                           Product.click_count.desc()).slice(
                                               0, 10)
    productList1 = Product.query.filter(Product.is_sell == 1,
                                        Product.is_pass == 2).order_by(
                                            Product.pdate.desc()).slice(0, 10)
    productList2 = redis_cache.get('productList2')
    category = Category.query.all()
    # 推广产品
    if productList2 is None:
        products = Product.query.filter(
            Product.is_hot == 2, Product.is_sell == 1,
            Product.is_pass == 2).order_by(Product.pdate.desc()).slice(0, 3)
        productList2 = []
        for product in products:
            product = Product.product_json(product)
            productList2.append(product)
        json_dumps = json.dumps(productList2, ensure_ascii=False)
        print(json_dumps)
        redis_cache.set("productList2", json_dumps)
    else:
        productList2 = productList2.decode('utf8')
        productList2 = json.loads(productList2)
        print(productList2)
    return render_template('user/index.html',
                           hot_products=productList,
                           new_products=productList1,
                           extend_products=productList2,
                           categorys=category)
Exemple #3
0
def my_context_processor():
    category_all = Category.query.all()
    categoryList = redis_cache.get('categoryList')
    if categoryList is None:
        categorys = Category.query.all()
        categoryList = []
        for category in categorys:
            category = Category.category_json(category)
            categoryList.append(category)
        json_dumps = json.dumps(categoryList, ensure_ascii=False)
        print(json_dumps)
        redis_cache.set("categoryList", json_dumps)
    else:
        categoryList = categoryList.decode('utf8')
        categoryList = json.loads(categoryList)
        print(categoryList)
    uid = session.get("uid")
    last_time = ""
    if uid is not None:
        user = User.query.get(uid)
        if user.shop_time is not None:
            shop_time = user.shop_time
            offset = timedelta(minutes=20)
            result_time = (shop_time + offset) - datetime.now()
            if result_time.days < 0:
                for shopCart in user.shopcarts:
                    product = Product.query.get(shopCart.pid)
                    product.counts = product.counts + shopCart.count
                    db.session.delete(shopCart)
                    db.session.commit()
                user.shop_time is None
                db.session.commit()
                last_time = ""
            else:
                m, s = divmod(result_time.seconds, 60)
                last_time = "" + str(m) + ":" + str(s)
        length = 0
        for shopCart in user.shopcarts:
            length += shopCart.count

        return {
            "uid": uid,
            "username": user.username,
            'user_img': user.img_url,
            "categorys": categoryList,
            "category_all": category_all,
            "last_time": last_time,
            "length": length,
            "user": user
        }
    else:
        return {
            "categorys": categoryList,
            "category_all": category_all,
            "uid": "",
            "length": "0"
        }
Exemple #4
0
def about():
    userList = redis_cache.get('userList')
    if userList is None:
        users = User.query.filter(User.is_ok == 1).order_by(
            User.create_time.desc()).slice(0, 5)
        userList = []
        for user in users:
            user = User.user_json(user)
            userList.append(user)
        json_dumps = json.dumps(userList, ensure_ascii=False)
        print(json_dumps)
        redis_cache.set("userList", json_dumps)
    else:
        userList = userList.decode('utf8')
        userList = json.loads(userList)
        print(userList)
    return render_template("about.html", userList=userList)
Exemple #5
0
 def post(self):
     print("注册注册")
     code = request.form.get("code")
     random_sample_old = redis_cache.get('random_sample').decode()
     if random_sample_old.lower() == code.lower():
         username = request.form.get("username")
         password = request.form.get("password")
         email = request.form.get("email")
         name = request.form.get("name")
         phone = request.form.get("phone")
         addr = request.form.get("addr")
         id = str(uuid.uuid1()).replace('-', '')
         user = User(id=id, username=username, password=generate_password_hash(password), email=email, name=name,
                     phone=phone, addr=addr)
         db.session.add(user)
         db.session.commit()
         return redirect(url_for('user.login'))
     else:
         return render_template("registerUI.html", msg='动态码不正确')
Exemple #6
0
def index():
    productList = redis_cache.get('productList')
    productList1 = redis_cache.get('productList1')
    productList2 = redis_cache.get('productList2')
    productList3 = redis_cache.get('productList3')
    category = Category.query.all()
    # 热门产品
    if productList is None:
        products = Product.query.filter(
            Product.is_sell == 1, Product.is_pass == 2,
            Product.counts != 0).order_by(Product.click_count.desc()).slice(
                0, 10)
        productList = []
        for product in products:
            product = Product.product_json(product)
            productList.append(product)
        json_dumps = json.dumps(productList, ensure_ascii=False)
        print(json_dumps)
        redis_cache.set("productList", json_dumps)
    else:
        productList = productList.decode('utf8')
        productList = json.loads(productList)
        print(productList)
    # 新产品
    if productList1 is None:
        products = Product.query.filter(
            Product.is_sell == 1, Product.is_pass == 2,
            Product.counts != 0).order_by(Product.pdate.desc()).slice(0, 10)
        productList1 = []
        for product in products:
            product = Product.product_json(product)
            productList1.append(product)
        json_dumps = json.dumps(productList1, ensure_ascii=False)
        print(json_dumps)
        redis_cache.set("productList1", json_dumps)
    else:
        productList1 = productList1.decode('utf8')
        productList1 = json.loads(productList1)
        print(productList1)
    # 轮播图商品
    if productList2 is None:
        products = Product.query.filter(
            Product.is_hot == 2, Product.is_sell == 1, Product.is_pass == 2,
            Product.counts != 0).order_by(Product.pdate.desc()).slice(0, 3)
        productList2 = []
        for product in products:
            product = Product.product_json(product)
            productList2.append(product)
        json_dumps = json.dumps(productList2, ensure_ascii=False)
        print(json_dumps)
        redis_cache.set("productList2", json_dumps)
    else:
        productList2 = productList2.decode('utf8')
        productList2 = json.loads(productList2)
        print(productList2)
    # 最低价商品
    if productList3 is None:
        products = Product.query.filter(
            Product.is_sell == 1, Product.is_pass == 2,
            Product.counts != 0).order_by(Product.new_price.asc()).slice(0, 6)
        productList3 = []
        for product in products:
            product = Product.product_json(product)
            productList3.append(product)
        json_dumps = json.dumps(productList3, ensure_ascii=False)
        print(json_dumps)
        redis_cache.set("productList3", json_dumps)
    else:
        productList3 = productList3.decode('utf8')
        productList3 = json.loads(productList3)
        print(productList3)
    return render_template('user/index.html',
                           hot_products=productList,
                           new_products=productList1,
                           extend_products=productList2,
                           comment_products=productList3,
                           categorys=category)