def add_log(aftersales_id, content, al_type=0, current_time=0, commit=True): """添加日志 @param al_type 类型: 0_默认, 1_申请, 2_审核, 3_收货(商家), 4_退款, 5_寄货(商家) 6_寄货(用户), """ current_time = current_time if current_time else current_timestamp() aftersales = Aftersales.query.get(aftersales_id) if aftersales: data = { 'aftersales_id': aftersales_id, 'content': content, 'al_type': al_type, 'add_time': current_time } model_create(AftersalesLogs, data) data = {'latest_log': content, 'update_time': current_time} model_update(aftersales, data, commit=commit) return True
def create_account(uid, current_time, is_commit=False): """创建帐户""" data = {'uid': uid, 'last_type': 1, 'last_time': 0} model_create(UserLastTime, data, commit=is_commit) data = { 'uid': uid, 'funds': 0, 'add_time': current_time, 'update_time': current_time } model_create(Funds, data, commit=is_commit) return True
def save_comment(): """评价订单商品""" resjson.action_code = 17 if not check_login(): return resjson.print_json(resjson.NOT_LOGIN) uid = get_uid() nickname = get_nickname() avatar = get_avatar() wtf_form = CommentOrderGoodsForm() current_time = current_timestamp() if not wtf_form.validate_on_submit(): for key, value in wtf_form.errors.items(): msg = value[0] return resjson.print_json(11, msg) og_id = wtf_form.og_id.data order_goods = OrderGoods.query.get(og_id) if not order_goods: return resjson.print_json(12, _(u'订单商品不存在')) order = Order.query.filter(Order.order_id == order_goods.order_id).filter( Order.uid == uid).first() if not order: return resjson.print_json(13, _(u'订单商品不存在')) data = {'uid': uid, 'nickname': nickname, 'avatar': avatar, 'ttype': 1, 'tid': order_goods.goods_id, 'rating': wtf_form.rating.data, 'content': wtf_form.content.data, 'img_data': wtf_form.img_data.data, 'add_time': current_time} comment = model_create(Comment, data, commit=True) item = Goods.query.get(order_goods.goods_id) if item: comment_count = Comment.query.\ filter(Comment.ttype == 1).\ filter(Comment.tid == order_goods.goods_id).count() good_count = Comment.query.\ filter(Comment.ttype == 1).\ filter(Comment.tid == order_goods.goods_id).\ filter(Comment.rating == 3).count() comment_good_rate = round( (Decimal(good_count)/Decimal(comment_count)) * 100) model_update(item, {'comment_count': comment_count, 'comment_good_rate': comment_good_rate}) model_update(order_goods, {'comment_id': comment.comment_id}, commit=True) # 站内消息 content = _(u'您已评价“%s”。' % order_goods.goods_name) mcs = MessageCreateService( 1, uid, -1, content, ttype=2, tid=og_id, current_time=current_time) if not mcs.check(): log_error('[ErrorViewApiOrderSaveComment][MessageCreateError] og_id:%s msg:%s' % ( og_id, mcs.msg)) else: mcs.do() return resjson.print_json(0, u'ok')
def create_tran(self): """创建交易""" pay_amount = Decimal('0.00') for pay_order in self.pay_order_list: pay_amount += Decimal(pay_order.pay_amount) tran_index = model_create(OrderTranIndex, {}, commit=True) data = { 'tran_id': tran_index.tran_id, 'uid': self.uid, 'pay_amount': pay_amount, 'pay_status': 1, 'order_id_list': self.order_id_json, 'add_time': self.current_time, 'update_time': self.current_time} self.tran = model_create(OrderTran, data, commit=True) return True
def address_save(): """保存地址""" resjson.action_code = 12 if not check_login(): return resjson.print_json(resjson.NOT_LOGIN) uid = get_uid() wtf_form = AddressForm() current_time = current_timestamp() if not wtf_form.validate_on_submit(): for key, value in wtf_form.errors.items(): msg = value[0] return resjson.print_json(11, msg) is_default = toint(request.form.get('is_default', '-1')) if is_default not in [-1, 0, 1]: return resjson.print_json(resjson.PARAM_ERROR) ua_id = wtf_form.ua_id.data if ua_id: user_address = UserAddress.query.filter( UserAddress.ua_id == ua_id).filter(UserAddress.uid == uid).first() if not user_address: return resjson.print_json(12, _(u'收货地址不存在')) else: data = {'uid': uid, 'is_default': 1, 'add_time': current_time} user_address = model_create(UserAddress, data) if is_default == -1: is_default = user_address.is_default data = { 'name': wtf_form.name.data, 'mobile': wtf_form.mobile.data, 'province': wtf_form.province.data, 'city': wtf_form.city.data, 'district': wtf_form.district.data, 'address': wtf_form.address.data, 'is_default': is_default, 'update_time': current_time } if is_default == 1: default = UserAddress.query.filter(UserAddress.uid == uid).filter( UserAddress.is_default == 1).first() if default and default.ua_id != ua_id: default.is_default = 0 user_address = model_update(user_address, data) db.session.commit() return resjson.print_json(0, u'ok', {'ua_id': user_address.ua_id})
def update(self): """ 更新 """ # 更新 model_update(self.funds_obj, {'funds':self.funds, 'update_time':self.current_time}) # 创建流水 data = {'uid':self.uid, 'funds_prev':self.funds_prev, 'funds_change':self.funds_change, 'funds':self.funds, 'event':self.event, 'ttype':self.ttype, 'tid':self.tid, 'remark_user':self.remark_user, 'remark_sys':self.remark_sys, 'add_time':self.current_time} self.funds_detail = model_create(FundsDetail, data) return True
def galleries_save(): """保存商品相册""" g.page_title = _(u'保存商品') goods_id = toint(request.form.get('goods_id', '0')) images = request.files.getlist('image') current_time = current_timestamp() for image in images: try: fus = FileUploadService() img = fus.save_storage(image, 'item') data = {'goods_id': goods_id, 'img': img, 'add_time': current_time} model_create(GoodsGalleries, data) except Exception as e: err_msg = _(u'上传失败,请检查云存储配置') return redirect( url_for('admin.item.galleries', goods_id=goods_id, err_msg=err_msg)) db.session.commit() return redirect(url_for('admin.item.galleries', goods_id=goods_id))
def create(self): """创建订单""" # 创建订单索引 order_index = model_create(OrderIndex, {}, commit=True) order_id = order_index.order_id # 创建订单编号 order_sn = OrderStaticMethodsService.create_order_sn(self.current_time) # 订单商品总金额 goods_amount = self.recharge_amount # 更新订单金额 order_amount = goods_amount # 更新订单应付金额 pay_amount = order_amount # 创建订单 data = { 'order_id': order_id, 'order_sn': order_sn, 'uid': self.uid, 'order_type': 2, 'order_status': 1, 'goods_amount': goods_amount, 'order_amount': order_amount, 'pay_amount': pay_amount, 'pay_type': 1, 'pay_status': 1, 'add_time': self.current_time, 'update_time': self.current_time} self.order = model_create(Order, data) db.session.commit() return True
def save(): """保存商品""" g.page_title = _(u'保存商品') form = ItemForm(CombinedMultiDict((request.files, request.form))) current_time = current_timestamp() if not form.validate_on_submit(): return render_template('admin/item/detail.html.j2', form=form, item=form.data) goods_img = '' if form.goods_img.data: fus = FileUploadService() try: goods_img = fus.save_storage(form.goods_img.data, 'item') except Exception as e: form.goods_img.errors = (_(u'上传失败,请检查云存储配置')) return render_template('admin/item/detail.html.j2', form=form, item=form.data) goods_id = toint(form.goods_id.data) if goods_id: item = Goods.query.get_or_404(goods_id) else: item = model_create(Goods, {'detail': '', 'add_time': current_time}) goods_img = goods_img if goods_img else item.goods_img data = { 'cat_id': form.cat_id.data, 'goods_name': form.goods_name.data, 'goods_img': goods_img, 'goods_desc': form.goods_desc.data, 'goods_price': form.goods_price.data, 'market_price': form.market_price.data, 'is_sale': form.is_sale.data, 'stock_quantity': form.stock_quantity.data, 'is_hot': form.is_hot.data, 'is_recommend': form.is_recommend.data, 'update_time': current_time } model_update(item, data, commit=True) return redirect(url_for('admin.item.index'))
def coupon_save(): """优惠券派发保存""" admin_uid = session.get('admin_uid', None) if not admin_uid: return_url = request.args.get('return_url', '/admin/dashboard') return redirect(url_for('admin.auth.login', return_url=return_url)) form = CouponForm(request.form) form.avatar.data = request.args.get('avatar') if not form.validate_on_submit(): return render_template('admin/user/coupon.html.j2', form=form) coupon_batch = CouponBatch.query.filter( CouponBatch.cb_id == form.cb_id.data).first() if not coupon_batch: return render_template('admin/user/coupon.html.j2', form=form) #刷新优惠券数据 give_num = coupon_batch.give_num + 1 data = {'give_num': give_num} model_update(coupon_batch, data) coupon = model_create(Coupon, {'add_time': current_timestamp()}) data = { 'uid': form.uid.data, 'cb_id': coupon_batch.cb_id, 'coupon_name': coupon_batch.coupon_name, 'begin_time': coupon_batch.begin_time, 'end_time': coupon_batch.end_time, 'is_valid': coupon_batch.is_valid, 'limit_amount': coupon_batch.limit_amount, 'coupon_amount': coupon_batch.coupon_amount, 'limit_goods': coupon_batch.limit_goods, 'limit_goods_name': coupon_batch.limit_goods_name, 'coupon_from': coupon_batch.coupon_from, 'update_timne': current_timestamp() } model_update(coupon, data, True) return redirect(url_for('admin.user.detail', uid=form.uid.data))
def __request_token(self): """获取token""" if not self.__check(): raise Exception(_(u'请先配置公众号')) params = { 'grant_type': 'client_credential', 'appid': self.appid.encode('utf8'), 'secret': self.secret.encode('utf8') } url = 'https://api.weixin.qq.com/cgi-bin/token' url = u'%s?%s' % (url, urlencode(params)) response = requests.get(url) if response.status_code != 200: log_error('[WeixinAccesstoken][RequestError] request error.') raise Exception(_(u'网络错误')) data = response.json() errcode = data.get('errcode', 0) errmsg = data.get('errmsg', '') if errcode > 0: log_error('[WeixinAccesstoken][RequestError] errcode:%s errmsg:%s' %\ (errcode, errmsg)) raise Exception(errmsg) token = data.get('access_token', '') expires_in = data.get('expires_in', 0) if not self.st: self.st = model_create(SysToken, { 'token_type': 'weixin_mp_token', 'add_time': self.current_time }) expires_in = self.current_time + expires_in - 60 model_update(self.st, { 'access_token': token, 'expires_in': expires_in }, commit=True) return token
def category_save(): """保存分类""" g.page_title = _(u'保存分类') form = CategoryForm(CombinedMultiDict((request.files, request.form))) if not form.validate_on_submit(): return render_template('admin/post/category_detail.html.j2', form=form) cat_id = toint(form.cat_id.data) if cat_id: category = PostCategories.query.get_or_404(cat_id) else: category = model_create(PostCategories, {'add_time':current_timestamp()}) data = {'cat_name':form.cat_name.data, 'is_show':form.is_show.data, 'sorting':form.sorting.data} model_update(category, data, commit=True) return redirect(url_for('admin.post.categories'))
def save(): """保存广告""" g.page_title = _(u'保存广告') form = AdvForm(CombinedMultiDict((request.files, request.form))) current_time = current_timestamp() if not form.validate_on_submit(): return render_template('admin/adv/detail.html.j2', form=form) img = '' if form.img.data: fus = FileUploadService() try: img = fus.save_storage(form.img.data, 'adv') except Exception as e: form.img.errors = (_(u'上传失败,请检查云存储配置')) return render_template('admin/adv/detail.html.j2', form=form) adv_id = toint(form.adv_id.data) if adv_id: adv = Adv.query.get_or_404(adv_id) else: adv = model_create(Adv, {'add_time': current_time}) img = img if img else adv.img data = { 'ac_id': form.ac_id.data, 'img': img, 'desc': form.desc.data, 'ttype': form.ttype.data, 'tid': form.tid.data, 'url': form.url.data, 'sorting': form.sorting.data, 'is_show': form.is_show.data, 'platform_type': form.platform_type.data } model_update(adv, data, commit=True) return redirect(url_for('admin.adv.index'))
def do(self): """通知""" data = { 'message_type': self.message_type, 'content': self.content, 'img': self.img, 'data': self.data, 'fuid': self.fuser.get('uid', 0), 'fname': self.fuser.get('nickname', ''), 'favatar': self.fuser.get('avatar', ''), 'tuid': self.tuser.uid, 'tname': self.tuser.nickname, 'tavatar': self.tuser.avatar, 'tid': self.tid, 'ttype': self.ttype, 'add_time': self.current_time, 'add_date': self.today_timestamp } self.message = model_create(Message, data, commit=True) return True
def save(): """保存文章""" g.page_title = _(u'保存文章') form = PostForm(CombinedMultiDict((request.files, request.form))) current_time = current_timestamp() if not form.validate_on_submit(): return render_template('admin/post/detail.html.j2', form=form, item=form.data) post_id = toint(form.post_id.data) if post_id: item = Post.query.get_or_404(post_id) else: item = model_create(Post, {'post_detail':'', 'add_time':current_time}) cat_id = form.cat_id.data data = db.session.query(PostCategories.cat_name).filter(PostCategories.cat_id==cat_id).first() data = {'cat_id':form.cat_id.data, 'cat_name':data[0], 'post_name':form.post_name.data, 'is_publish':form.is_publish.data, 'sorting':form.sorting.data, 'update_time':current_time} model_update(item, data, commit=True) return redirect(url_for('admin.post.h5', post_id=item.post_id))
def category_save(): """保存分类""" g.page_title = _(u'保存分类') form = CategoryForm(CombinedMultiDict((request.files, request.form))) if not form.validate_on_submit(): return render_template('admin/item/category_detail.html.j2', form=form) cat_img = '' if form.cat_img.data: fus = FileUploadService() try: cat_img = fus.save_storage(form.cat_img.data, 'category') except Exception as e: form.cat_img.errors = (_(u'上传失败,请检查云存储配置')) return render_template('admin/item/category_detail.html.j2', form=form) cat_id = toint(form.cat_id.data) if cat_id: category = GoodsCategories.query.get_or_404(cat_id) else: category = model_create(GoodsCategories, {'add_time': current_timestamp()}) cat_img = cat_img if cat_img else category.cat_img data = { 'cat_name': form.cat_name.data, 'cat_img': cat_img, 'is_show': form.is_show.data, 'is_recommend': form.is_recommend.data } model_update(category, data, commit=True) return redirect(url_for('admin.item.categories'))
def save(): """保存优惠券""" g.page_title = _(u'保存优惠券') form = CouponBatchForm(CombinedMultiDict((request.files, request.form))) current_time = current_timestamp() if not form.validate_on_submit(): return render_template('admin/coupon/detail.html.j2', form=form) cb_id = toint(form.cb_id.data) if cb_id: batch = CouponBatch.query.get_or_404(cb_id) else: batch = model_create(CouponBatch, {'add_time': current_time}) begin_time = str2timestamp(form.begin_time.data, 'YYYY-MM-DD') if form.begin_time.data else 0 end_time = str2timestamp(form.end_time.data, 'YYYY-MM-DD') if form.end_time.data else 0 data = { 'cb_name': form.cb_name.data, 'begin_time': begin_time, 'end_time': end_time, 'coupon_name': form.coupon_name.data, 'is_valid': form.is_valid.data, 'publish_num': form.publish_num.data, 'limit_amount': form.limit_amount.data, 'coupon_amount': form.coupon_amount.data, 'date_num': form.date_num.data, 'coupon_from': form.coupon_from.data, 'update_time': current_time } model_update(batch, data, commit=True) return redirect(url_for('admin.coupon.index'))
def update(self): """更新订单""" try: self.check() except OrderException as e: raise e discount_desc = None # 删除已用订单地址 model_delete(self.order_address) # 创建订单地址 data = { 'order_id': self.order_id, 'name': self.shipping_address.name, 'mobile': self.shipping_address.mobile, 'province': self.shipping_address.province, 'city': self.shipping_address.city, 'district': self.shipping_address.district, 'address': self.shipping_address.address, 'zip': self.shipping_address.zip, 'add_time': self.current_time, 'update_time': self.current_time} model_create(OrderAddress, data) # 还原已用优惠券 if self._coupon and self._coupon != self.coupon: data = { 'is_valid': 1, 'order_id': 0, 'use_time': 0} model_update(self._coupon, data) discount_desc = '' # 使用优惠券 if self.coupon: data = { 'is_valid': 0, 'order_id': self.order_id, 'use_time': self.current_time} model_update(self.coupon, data) discount_desc = _(u'使用优惠券%s: %s' % (self.coupon.coupon_id, self.coupon.coupon_name)) # 更新订单金额 self.order_amount = self.items_amount + self.shipping_amount # 更新应付金额 self.pay_amount = self.order_amount - self.discount_amount # 更新订单 data = { 'goods_amount': self.items_amount, 'order_amount': self.order_amount, 'discount_amount': self.discount_amount, 'pay_amount': self.pay_amount, 'shipping_id': self.shipping_id, 'shipping_name': self.shipping.shipping_name, 'shipping_code': self.shipping.shipping_code, 'shipping_amount': self.shipping_amount, 'update_time': self.current_time} if discount_desc is not None: data['discount_desc'] = discount_desc model_update(self.order, data) db.session.commit() return True
def add(): """加入购物车""" resjson.action_code = 11 uid = get_uid() session_id = session.sid args = request.args order_id = toint(args.get('order_id', '0')) goods_id = toint(args.get('goods_id', '0')) quantity = toint(args.get('quantity', '1')) current_time = current_timestamp() items_data = [] if order_id > 0: order = Order.query.filter(Order.order_id == order_id).filter( Order.uid == uid).first() if not order: return resjson.print_json(resjson.PARAM_ERROR) order_goods = OrderGoods.query.filter( OrderGoods.order_id == order_id).all() for _order_goods in order_goods: items_data.append({ 'goods_id': _order_goods.goods_id, 'quantity': _order_goods.goods_quantity }) else: # 检查 if goods_id <= 0 or quantity < 1: return resjson.print_json(resjson.PARAM_ERROR) items_data.append({'goods_id': goods_id, 'quantity': quantity}) for item_data in items_data: goods_id = item_data.get('goods_id') quantity = item_data.get('quantity') # 检查 item = Goods.query.get(goods_id) if not item: return resjson.print_json(10, _(u'找不到商品')) # 获取购物车商品 q = Cart.query.filter(Cart.goods_id == goods_id).filter( Cart.checkout_type == 1) if uid: q = q.filter(Cart.uid == uid) else: q = q.filter(Cart.session_id == session_id) cart = q.first() # 计算商品购买数量 quantity += cart.quantity if cart else 0 if order_id <= 0 and quantity > item.stock_quantity: return resjson.print_json(11, u'库存不足') # 是否创建购物车商品 if not cart: data = { 'uid': uid, 'session_id': session_id, 'goods_id': goods_id, 'quantity': 0, 'is_checked': 1, 'checkout_type': 1, 'add_time': current_time } cart = model_create(Cart, data) # 更新购物车商品 data = {'quantity': quantity, 'update_time': current_time} cart = model_update(cart, data) db.session.commit() cs = CartService(uid, session_id) cs.check() session['cart_total'] = cs.cart_total return resjson.print_json(0, u'ok', { 'cart_total': cs.cart_total, 'cart_num': quantity })
def create(self): """创建""" aftersales_sn = AfterSalesStaticMethodsService.create_aftersales_sn( self.current_time) for data in self.goods_data: if self.og_id > 0: data.pop('maximum') goods_data = json.dumps(self.goods_data) data = { 'aftersales_sn': aftersales_sn, 'uid': self.uid, 'order_id': self.order.order_id, 'aftersales_type': self.aftersales_type, 'deliver_status': self.deliver_status, 'content': self.content, 'img_data': self.img_data, 'status': 1, 'check_status': 1, 'refunds_amount': self.refunds_amount, 'refunds_method': self.order.pay_method, 'latest_log': u'', 'goods_data': goods_data, 'quantity': self.quantity, 'add_time': self.current_time, 'update_time': self.current_time } aftersales = model_create(Aftersales, data, commit=True) for data in self.goods_data: data['aftersales_id'] = aftersales.aftersales_id model_create(AftersalesGoods, data) # 售后商品数量 - 单个 if self.order_goods: quantity = self.order_goods.aftersales_goods_quantity + self.quantity data = { 'aftersales_goods_quantity': quantity, 'update_time': self.current_time } model_update(self.order_goods, data) # 售后商品数量 - 整单 if len(self.order_goods_list) > 0: for order_goods in self.order_goods_list: data = { 'aftersales_goods_quantity': order_goods.goods_quantity, 'update_time': self.current_time } model_update(order_goods, data) AfterSalesStaticMethodsService.add_log(aftersales.aftersales_id, _(u'申请售后服务,等待商家审核。'), 1, self.current_time, False) # 售后地址 if self.address_data: self.address_data['aftersales_id'] = aftersales.aftersales_id model_create(AftersalesAddress, self.address_data) db.session.commit() return aftersales.aftersales_id
def create(self): """创建""" self.user = model_create(User, self.user_data)
def check(self): """检查""" # 检查 self.order = Order.query.get(self.order_id) if not self.order: self.msg = _(u'订单不存在') return False # 检查 if self.order.pay_status != 2: self.msg = _(u'未支付订单') return False # 检查 if self.order.pay_method == 'funds': self.third_type = 1 if self.order.pay_method in ['weixin_app', 'weixin_jsapi']: self.third_type = 2 if self.third_type == 0: self.msg = _(u'支付方式错误') return False # 检查 refunds_amount_sum = db.session.query(func.sum(Refunds.refunds_amount).label('sum')).\ filter(Refunds.tran_id == self.order.tran_id).\ filter(Refunds.refunds_status == 1).first() _sum = refunds_amount_sum.sum if refunds_amount_sum.sum else 0 total = _sum + self.refunds_amount tran = OrderTran.query.get(self.order.tran_id) if total > tran.pay_amount: self.msg = _(u'退款金额超过交易已付金额') return False # 检查 if self.third_type == 1: remark_user = u'退款' remark_sys = u'退款,订单编号:%s,退款金额:%s' % (self.order.order_sn, self.refunds_amount) self.fs = FundsService(self.order.uid, self.refunds_amount, 3, 2, self.order_id, remark_user, remark_sys, self.current_time) if not self.fs.check(): self.msg = self.fs.msg return False # 是否创建退款记录 if not self.refunds: data = { 'tran_id': self.order.tran_id, 'order_id': self.order_id, 'refunds_amount': self.refunds_amount, 'refunds_method': self.order.pay_method, 'refunds_sn': '', 'refunds_time': 0, 'refunds_status': 0, 'remark_user': _(u'申请退款'), 'remark_sys': _(u'申请退款'), 'add_time': self.current_time } self.refunds = model_create(Refunds, data, commit=True) # 检查 if self.third_type == 2: after_year_time = before_after_timestamp(self.order.paid_time, {'years': 1}) if self.current_time > after_year_time: self.msg = _(u'交易时间超过一年的订单无法提交退款') return False self.jwrs = JsapiWeixinRefundsService(self.refunds) if not self.jwrs.check(): self.msg = self.jwrs.msg return False return True