def add_goods(args, result): name = args[0] price = float(args[1]) label = args[2] goods = Goods(name=name, price=price, label=label) goods.save() result['status'] = 'success'
def index(): resp_data = { 'code': 200, 'msg': '操作成功~~', 'page': { 'from': 1, 'pagelist': [1], 'cate': 'index' }, 'data': {} } addNav(resp_data) addBanner(resp_data) goods = Goods() goodsList = goods.getAllGoodsLimitPerPage() canPage(resp_data, goods.getAllGoods, current_app.config['PER_PAGE'], index) #能分多少页 if len(request.args) > 0: #处理分页 goodsList = paging(resp_data, goods.getAllGoodsPaging, index) if goodsList == []: resp_data['code'] = -1 resp_data['msg'] = '取不到数据库的信息~' goodscollection = GoodsCollection(goodsList) resp_data['data'] = goodscollection.data return render_template('index.html', data=resp_data)
def company(company): resp_data = { 'code': 200, 'msg': '操作成功~~', 'page': { 'from': 1, 'pagelist': [1], 'cate': 'company', 'cate_para': company }, 'data': {} } addNav(resp_data) addBanner(resp_data) goods = Goods() goodsList = goods.getGoodListByCompanyLimitPerPage(company) canPage(resp_data, goods.getGoodListByCompany, current_app.config['PER_PAGE'], company) if len(request.args) > 0: goodsList = paging(resp_data, goods.getGoodListByCompanyPaging, company) if goodsList == []: resp_data['code'] = -1 resp_data['msg'] = '取不到数据库的信息~' goodscollection = GoodsCollection(goodsList) resp_data['data'] = goodscollection.data return render_template('index.html', data=resp_data)
def detail(gid): resp_data = {'code': 200, 'msg': '查询成功~~', 'data': {}} addNav(resp_data) goods = Goods() goodDetail = goods.getGoodListById(gid) if goodDetail == []: resp_data['code'] = -1 resp_data['msg'] = '取不到数据库的信息~' goodsviewmodel = GoodsViewModel(goodDetail) resp_data['data'] = goodsviewmodel.data return render_template('detail.html', data=resp_data)
def cart(gid): resp_data = {'code': 200, 'msg': '查询成功~~', 'data': {}} addNav(resp_data) uid = current_user.id cart = Cart() goods = Goods() if gid != '0': # gid=0表示从nav跳转过来,不是0表示从详情页跳转过来,git>0表示添加一台,git<0表示减少一台 abs_gid = abs(int(gid)) num = cart.getNum(uid, abs_gid) goods_info = goods.getCartWantDict(abs_gid) if int(gid) > 0: if num > 0: # 如果存在数量加1,不存在变成1 total_price = cart.getTotalPrice(uid=uid, gid=gid) # 得到现在的总价 with db.auto_commit(): cart.query.filter_by(uid=uid, gid=gid).update({ 'num': num + 1, 'total_price': total_price + goods_info['price'] }) elif num == 0: with db.auto_commit(): cart.gid = gid cart.uid = uid cart.num = 1 cart.goods_name = goods_info['goods_name'] cart.goods_img = goods_info['goods_img'] cart.price = goods_info['price'] cart.cpu = goods_info['cpu'] cart.ram = goods_info['ram'] cart.memory = goods_info['memory'] cart.video_card = goods_info['video_card'] cart.size = goods_info['size'] cart.cate_company = goods_info['cate_company'] cart.total_price = goods_info['price'] db.session.add(cart) elif int(gid) < 0: total_price = cart.getTotalPrice(uid=uid, gid=abs_gid) with db.auto_commit(): cart.query.filter_by(uid=uid, gid=abs_gid).update({ 'num': num - 1, 'total_price': total_price - goods_info['price'] }) userAllCartList = cart.getUserAllCartList(uid) cartCollection = CartCollection(userAllCartList) resp_data['data'] = cartCollection.data return render_template('cart.html', data=resp_data)
def del_goods(goods_id, result): goods = Goods.objects(_id=goods_id) if goods: goods.delete() result['status'] = 'success' else: result['status'] = 'fail' result['msg'] = 'goods not exist'
def search_goods_by_label(required_label): tmp_list = list() all_goods = Goods.objects() for goods in all_goods: # whether list has contains relationship if set(required_label) <= set(goods.label): tmp_list.append(goods) return tmp_list
def get_all_goods(): goods_list = list() all_goods = Goods.objects() for goods in all_goods: single_goods = dict() single_goods['_id'] = str(goods['_id']) single_goods['name'] = goods['name'] single_goods['price'] = goods['price'] single_goods['label'] = goods['label'] goods_list.append(single_goods) return goods_list
def check(self): """ 检查 """ # 商品主图 _goods_img = request.files.get('goods_img', None) # 商品相册 _gallery_list = request.files.getlist('gallery') # 筛选属性 _attr_id_list = request.form.getlist('attr_id') _attr_id_list = list(set(_attr_id_list)) # SKU _sku_list = request.form.getlist('sku') _sku_name_list = request.form.getlist('sku_name') _sku_price_list = request.form.getlist('sku_price') _sku_quantity_list = request.form.getlist('sku_quantity') # 是否创建商品 self.is_new = True if self.goods_id == 0 else False # 检查 - 必填项 required_arr = ['goods_name', 'is_sale', 'is_hot', 'is_pre', 'is_new', 'goods_price', 'is_free_shipping', 'is_return'] for key in required_arr: value = self.form.get(key, '').strip() if not value: self.errmsg[key] = u'必填项' # 检查 - 新建商品是否上传商品主图 if self.is_new and not _goods_img: self.errmsg['goods_img'] = u'必填项' gc_id = toint(self.form.get('gc_id', '0')) if gc_id <= 0: self.errmsg['gc_id'] = u'必填项' # 检查 - 商品是否存在 if not self.is_new: self.goods = Goods.get(self.goods_id) if not self.goods: self.errmsg['submit'] = u'商品不存在' # 检查 - 商品主图是否合法 if _goods_img: oss = AliyunOSS('goods', current_app.config['SAVE_TARGET_PATH']) try: oss.save(_goods_img) self.goods_img = oss.put_to_oss() except UploadNotAllowed, e: self.errmsg['goods_img'] = u'商品图片只允许是图片文件' except Exception, e: self.errmsg['goods_img'] = u'商品图片上传失败'
def search(): search_name = request.args['q'] resp_data = { 'code': 200, 'msg': '操作成功~~', 'page': { 'from': 1, 'pagelist': [1], 'cate': 'search' }, 'data': {} } addNav(resp_data) addBanner(resp_data) goods = Goods() goodsList = goods.getGoodListByNameLimitPerPage(search_name) if goodsList == []: resp_data['code'] = -1 resp_data['msg'] = '取不到数据库的信息~' goodscollection = GoodsCollection(goodsList) resp_data['data'] = goodscollection.data return render_template('index.html', data=resp_data)
def order(order_from): resp_data = {'code': 200, 'msg': '查询成功~~', 'data': {}} addNav(resp_data) uid = current_user.id cart = Cart() order = Order() goods = Goods() if order_from == "list": pass elif order_from == "from_cart": with db.auto_commit(): order.uid = uid order.order_no = geneOrderNo() order.order_status = 600 order.order_address = current_user.address if current_user.address else "不详" order.order_detail = cart.getUserCartTotalDetails(uid) order.goods_imgs = cart.getUserCartTotalImage(uid) order.total_price = cart.getUserCartTotalPrice(uid) db.session.add(order) cart.deleteUserAllCartList(uid) else: gid = order_from with db.auto_commit(): order.uid = uid order.order_no = geneOrderNo() order.order_status = 600 order.order_address = current_user.address if current_user.address else "不详" order.order_detail = goods.getName(gid) order.goods_imgs = goods.getImage(gid) order.total_price = goods.getPrice(gid) db.session.add(order) userAllOrderList = order.getUserAllOrderList(uid) orderCollection = OrderCollection(userAllOrderList) resp_data['data'] = orderCollection.data return render_template('order.html', data=resp_data)
def edit_goods(args, result): goods_id = args[0] edit_type = args[1] edit_content = args[2] goods = Goods.objects(_id=goods_id).first() if goods: if edit_type == 'price': goods[edit_type] = float(edit_content) else: goods[edit_type] = edit_content goods.save() result['status'] = 'success' else: result['status'] = 'fail' result['msg'] = 'goods not exist'
def sort_modify(): """修改排序""" goods_id = toint(request.args.get('goods_id', '0')) new_sort = toint(request.args.get('new_sort', '0')) new_sort = new_sort if new_sort > 0 else -1 if new_sort < 0: return u'只能输入大于0的数字' if new_sort > 10000000: return u'数字不能过大' if goods_id <= 0: return u'参数出错' goods_info = Goods.get(goods_id) if goods_info: goods_info.sort_order = new_sort db.session.commit() return u'ok'
def put_cart(args, result): goods_id = args[0] phone_num = args[1] goods_num = args[2] user = User.objects(phone_num=phone_num).first() goods = Goods.objects(_id=goods_id).first() if user and goods: for cart in user.cart: if cart['goods_id'] == goods_id: cart['good_num'] = goods_num cart['goods_price'] = float(goods.price) * int(goods_num) break else: goods_info = dict() goods_info['goods_id'] = goods_id goods_info['goods_name'] = goods.name goods_info['goods_price'] = float(goods.price) * int(goods_num) goods_info['good_num'] = goods_num user.cart.append(goods_info) user.save() result['status'] = 'success' else: result['status'] = 'fail' result['msg'] = 'user or goods not exist'
def save(self): """ 保存 """ gc_id = toint(self.form.get('gc_id', 0)) goods_name = self.form.get('goods_name', '').strip() goods_desc = self.form.get('goods_desc', '').strip() goods_detail = self.form.get('goods_detail', '').strip() is_sale = toint(self.form.get('is_sale', 0)) kind = toint(self.form.get('kind', 0)) goods_price = Decimal(self.form.get('goods_price', '0.00').strip()) market_price = Decimal(self.form.get('market_price', '0.00').strip()) quantity = toint(self.form.get('quantity', 0)) sort_order = toint(self.form.get('sort_order', 0)) unit = self.form.get('unit', '').strip() uf_id = toint(self.form.get('uf_id', 0)) uid = session.get('uid', 0) is_hot = toint(self.form.get('is_hot', '0')) is_pre = toint(self.form.get('is_pre', '0')) is_new = toint(self.form.get('is_new', '0')) sale_count = toint(self.form.get('sale_count', 0)) is_free_shipping = toint(self.form.get('is_free_shipping', 0)) is_return = toint(self.form.get('is_return', 0)) goods_attr = self.form.get('goods_attr', '').strip() if self.is_new: self.goods = Goods.create(add_time=self.current_time, commit=True) # 商品主图 if self.goods_img: self.goods.update(goods_img=self.goods_img) # 相册 for img in self.goods_gallery_list: GoodsGallery.create(goods_id=self.goods.goods_id, img=img, add_time=self.current_time) # 筛选属性 for attr in self.goods_attr_list: GoodsAttr.create(goods_id=self.goods.goods_id, attr_id=attr.attr_id, attr_name=attr.attr_name, ag_id=attr.ag_id, ag_name=attr.ag_name) # SKU for sku in self.goods_sku_list: GoodsSku.create(goods_id=self.goods.goods_id, sku=sku['sku'], sku_name=sku['sku_name'], sku_price=sku['sku_price'], sku_quantity=sku['sku_quantity']) self.goods.update(gc_id=gc_id, goods_name=goods_name, is_hot=is_hot, is_pre=is_pre, is_new=is_new, goods_attr=goods_attr, goods_desc=goods_desc, goods_detail=goods_detail, sale_count=sale_count, is_sale=is_sale, market_price=market_price, goods_price=goods_price, kind=kind, quantity=quantity, sort_order=sort_order, uid=uid, unit=unit, uf_id=uf_id, is_return=is_return, is_free_shipping=is_free_shipping, commit=True) return True
def upload(): resp_data = {'code': 200, 'data': {}} addNav(resp_data) goods = Goods() if request.method == 'POST': randomImgName = geneGoodsImgName() try: img = request.files['goods_img'] end = img.filename.split('.')[1] path = os.path.dirname(os.path.dirname( os.path.abspath(__file__))) + "/static/img/goods" if allowed_file(img.filename): with db.auto_commit(): goods.goods_img = '../static/img/goods/' + randomImgName + '.' + end goods.goods_name = request.values['goods_name'] goods.stock = int(request.values['stock']) goods.price = decimal.Decimal(request.values['price']) goods.goods_detail = request.values['goods_detail'] goods.cpu = request.values['cpu'] goods.ram = request.values['ram'] goods.memory = request.values['memory'] goods.video_card = request.values['video_card'] goods.size = request.values['size'] goods.cate_company = request.values['cate_company'] goods.cate_outlook = request.values['cate_outlook'] db.session.add(goods) resp_data['msg'] = "保存成功!" img.save(path + '/' + randomImgName + '.' + end) else: resp_data['img_error'] = '上传图片格式不正确,支持png,jpg,gif,jpeg' except Exception as e: resp_data['msg'] = "保存失败,请检查是否有字段或图片空缺并检查字段格式!" if resp_data[ 'msg'] == "保存失败,请检查是否有字段或图片空缺并检查字段格式!": #如果填错了,将上次填的信息返回去,不用再一次输入 last_fill = { 'goods_name': request.values['goods_name'], 'stock': request.values['stock'], 'price': request.values['price'], 'goods_detail': request.values['goods_detail'], 'cpu': request.values['cpu'], 'ram': request.values['ram'], 'memory': request.values['memory'], 'video_card': request.values['video_card'], 'size': request.values['size'], 'cate_company': request.values['cate_company'], 'cate_outlook': request.values['cate_outlook'], } resp_data['last_fill'] = last_fill return render_template('upload.html', data=resp_data) return render_template('upload.html', data=resp_data)
def addNav(resp_data): if current_user.is_active: # addUserInfo userviewmodel = UserViewModel(current_user) resp_data['user'] = userviewmodel.data companys = Goods.getDistinctCompany() # addCompanyList resp_data['companys'] = companys
def addBanner(resp_data): bannerList = Goods().getAllGoodsLimit3() if bannerList == []: return None bannerCollection = GoodsCollection(bannerList) resp_data['banner'] = bannerCollection.data
def add_news_goods(): """新增资讯商品""" """ goods_id = toint(request.args.get('goods_id', '0')) news_id = toint(request.args.get('news_id', '0')) redirect_url = request.args.get('redirect_url', None) if goods_id <= 0 or news_id <=0: return u'参数出错' goods = Goods.query.get_or_404(goods_id) news = News.query.get_or_404(news_id) if not goods: return u'新增资讯商品不存在' if not news: return u'找不到资讯' ng_info = NewsGoods.query.filter(NewsGoods.news_id == news_id).\ filter(NewsGoods.goods_id == goods_id).first() if ng_info: return u'资讯商品已经存在,无需增加。' ng = NewsGoods() ng.goods_id = goods_id ng.news_id = news_id ng.add_time = int(time.time()) db.session.add(ng) db.session.commit() if redirect_url: return redirect(url_for('news.news_edit',news_id=news_id, redirect_url=redirect_url)) return u'ok' """ resjson.action_code = 10 goods_id = toint(request.form.get('goods_id', 0)) news_id = toint(request.form.get('news_id', 0)) extend = request.form.get('extend', '') if goods_id <= 0 or news_id <= 0: return resjson.print_json(10, u'参数错误') goods = Goods.get(goods_id) if not goods: return resjson.print_json(11, u'找不到商品') news = News.get(news_id) if not news: return resjson.print_json(12, u'找不到资讯') news_goods = NewsGoods.query.filter(NewsGoods.news_id == news_id).filter( NewsGoods.goods_id == goods_id).first() if news_goods: return resjson.print_json(13, u'资讯的商品已经存在') NewsGoods.create(news_id=news_id, goods_id=goods_id, extend=extend, add_time=current_timestamp(), commit=True) return resjson.print_json(0, u'ok')
# 如果修改的达到开奖人次数量比之前的大就增加一元云购号码池表数据 if diff_finish_quantity > 0: for diff in range(diff_finish_quantity): lottery_number += 1 LotteryNumberPool.create(lottery_id=lottery_info.lottery_id, lottery_number=lottery_number) # 如果修改的达到开奖人次数量比之前的小就删除一元云购号码池表数据 # if diff_finish_quantity < 0: # for diff in range(abs(diff_finish_quantity)): # max_lottery_number_list=db.session.query(func.max(LotteryNumberPool.lottery_number)).\ # filter(LotteryNumberPool.lottery_id == lottery_info.lottery_id).all() # max_lottery_number = LotteryNumberPool.query.filter(LotteryNumberPool.lottery_number.in_([toint(lnp) for lnp in max_lottery_number_list])).first() # if max_lottery_number: # db.session.delete(max_lottery_number) goods = Goods.get(goods_id) lottery_info.update( section_number=section_number, goods_id=goods_id, lottery_img=lottery_img if lottery_img else lottery_info.lottery_img, lottery_price=lottery_price, max_quantity=max_quantity, finish_quantity=finish_quantity, lottery_name=lottery_name if lottery_name else goods.goods_name, commit=True) return redirect(url_for('lottery.index')) @lottery.route('/number') @lottery.route('/number/<int:page>')