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" }
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)
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" }
def hot_product(): redis_cache.delete('productList') products = Product.query.filter(Product.is_sell == 1, Product.is_pass == 2).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)
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)
def sendMail(): email = request.args.get("email") isEmail = User.query.filter(User.email == email, User.is_ok == 1).first() if not isEmail: random_sample = random.sample('1234567890abcdefghijklmnopqrstuvwxyz', 4) print(random_sample) random_sample = ''.join(random_sample) print(random_sample) redis_cache.set("random_sample", str(random_sample)) msg = Message(subject="南阳理工学院二手交易平台动态码", recipients=[email]) msg.html = "<b><img src='http://pgfgqbd3k.bkt.clouddn.com/loginUI.png' " \ "style='width:260px'></b><br><b>南阳理工学院二手交易平台注册动态码:" \ "<font color='red'>" + random_sample + "</font><b>" send_mail.send(msg) return jsonify({"msg": "", 'status': "200"}) else: return jsonify({"msg": "检测到该邮箱已经被注册,一个邮箱只有一个南工二手交易平台账号", 'status': "500"})
def sendMail(): email = request.args.get("email") isEmail = User.query.filter(User.email == email, User.is_ok == 1).first() if not isEmail: random_sample = random.sample('1234567890abcdefghijklmnopqrstuvwxyz', 4) random_sample = ''.join(random_sample) redis_cache.set(email, str(random_sample)) msg = Message(subject="中原工学院在线交易平台动态码", recipients=[email]) msg.html = "<b><img src='https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3282424091,3209377413&fm=26&gp=0.jpg' " \ "style='width:260px'></b><br><b>中原工工学院在线交易平台注册动态码:" \ "<font color='red'>" + random_sample + "</font><b>" send_mail.send(msg) return jsonify({"msg": "", 'status': "200"}) else: return jsonify({ "msg": "检测到该邮箱已经被注册,一个邮箱只有一个中工在线交易平台账号", 'status': "500" })
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)