def __init__(self): self.BaseAdmin = BASEADMIN() self.BaseController = BaseController() self.cplay = CPlay() self.cuser = CUser() # self.scale_dict = {3: 1000000, 4: 500000, 5: 200000, 6: 100000, 7: 50000, # 8: 50000, 9: 20000, 10: 10000, 11: 5000, 12: 2000, 13: 1000, # 14: 500, 15: 200, 16: 100, 17: 50, 18: 50, 19: 20, 20: 10} self.scale_dict = { 3: 10, 4: 10, 5: 10, 6: 10, 7: 5, 8: 4, 9: 3, 10: 3, 11: 2, 12: 2, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1 }
def set_mp_banner(self): """小程序轮播图""" data = parameter_required(('mpbpicture',)) mpbid = data.get('mpbid') mpb_dict = {'MPBpicture': data.get('mpbpicture'), 'MPBsort': data.get('mpbsort'), 'MPBshow': data.get('mpbshow'), 'MPBposition': data.get('mpbposition'), 'contentlink': data.get('contentlink')} with db.auto_commit(): if not mpbid: mpb_dict['MPBid'] = str(uuid.uuid1()) mpb_dict['ADid'] = getattr(request, 'user').id mpb_instance = MiniProgramBanner.create(mpb_dict) BASEADMIN().create_action(AdminActionS.insert.value, 'MiniProgramBanner', mpb_instance.MPBid) msg = '添加成功' else: mpb_instance = MiniProgramBanner.query.filter_by_(MPBid=mpbid).first_('未找到该轮播图信息') if data.get('delete'): mpb_instance.update({'isdelete': True}) BASEADMIN().create_action(AdminActionS.delete.value, 'MiniProgramBanner', mpb_instance.MPBid) msg = '删除成功' else: mpb_instance.update(mpb_dict, null='not') BASEADMIN().create_action(AdminActionS.update.value, 'MiniProgramBanner', mpb_instance.MPBid) msg = '编辑成功' db.session.add(mpb_instance) return Success(message=msg, data={'mpbid': mpb_instance.MPBid})
def update(self): """修改标签""" form = ItemUpdateForm().valid_data() psid = form.psid.data itid = form.itid.data itname = form.itname # 这里不要在后面加data isdelete = form.isdelete.data if itid in ['planet_featured', 'index_hot', 'news_bind_product', 'news_bind_coupon', 'index_brand', 'index_brand_product', 'index_recommend_product_for_you', 'upgrade_product', 'mynews' ] and isdelete is True: raise StatusError('系统默认标签不能被删除') Items.query.filter_by_(ITid=itid).first_("未找到该标签") if not isdelete and Items.query.filter(Items.ITid != itid, Items.ITname == itname, Items.ITtype == form.ittype.data, Items.isdelete == False).first(): raise DumpliError("您输入的标签名已存在") with db.auto_commit(): itsort = self._check_itsort(form.itsort.data, form.ittype.data) item_dict = {'ITname': itname, 'ITsort': itsort, 'ITdesc': form.itdesc.data, 'ITtype': form.ittype.data, 'ITrecommend': form.itrecommend.data, 'isdelete': isdelete } # item_dict = {k: v for k, v in item_dict.items() if v is not None} Items.query.filter_by_(ITid=itid).update(item_dict) BASEADMIN().create_action(AdminActionS.update.value, 'Items', itid) # 标签场景标签表 if psid: old_psids = list() scene_items = SceneItem.query.filter_by_(ITid=itid).all() [old_psids.append(scene_item.PSid) for scene_item in scene_items] # 获取已存在的关联psid for psi in psid: ProductScene.query.filter_by_({'PSid': psi}).first_('不存在的场景') if psi not in old_psids: scene_item_dict = { 'PSid': psi, 'ITid': itid, 'SIid': str(uuid.uuid1()) } scene_item_instance = SceneItem.create(scene_item_dict) db.session.add(scene_item_instance) BASEADMIN().create_action(AdminActionS.insert.value, 'SceneItem', psi) else: old_psids.remove(psi) [SceneItem.query.filter_by(PSid=droped_psid, ITid=itid).delete_() for droped_psid in old_psids] else: SceneItem.query.filter_by(ITid=itid).delete_() # psid = [] 为空时,删除所有该标签场景的关联 BASEADMIN().create_action(AdminActionS.delete.value, 'SceneItem', itid) return Success('修改成功', {'itid': itid})
def add_questanswer(self): """插入或更新问题及答案""" if not is_admin(): raise AuthorityError('权限不足') data = parameter_required(('qoid', 'quest', 'answer')) admin = Admin.query.filter( Admin.ADid == request.user.id, Admin.ADstatus == AdminStatus.normal.value).first_('权限被收回') quest_filter = Quest.query.filter_(Quest.QOid == data.get('qoid'), Quest.QUquest == data.get('quest'), Quest.isdelete == False).first() # answer_model = Answer.query.filter_(Answer.QAcontent == data.get('answer'), Answer.isdelete == False).first() if data.get('quid'): quest_model = Quest.query.filter_(Quest.QUid == data.get('quid'), Quest.isdelete == False).first() # 根据id 更新问题,如果问题重复则抛出异常 self.__update_quest(data, quest_model, quest_filter) self.__update_answer(data.get('answer'), quest_model) return Success('修改问题成功') # 如果传入的问题已经存在但是没有传入id ,还是执行update 操作 if quest_filter: self.__update_quest(data, quest_filter) self.__update_answer(data.get('answer'), quest_filter) return Success('修改问题成功') # 不在数据库内的问题插入 quest_instance = Quest.create({ 'QOid': data.get('qoid'), 'QUid': str(uuid.uuid1()), 'QUquest': data.get('quest'), 'QUcreateId': admin.ADid }) answer_instance = Answer.create({ 'QAid': str(uuid.uuid1()), 'QUid': quest_instance.QUid, 'QAcontent': data.get('answer'), 'QAcreateId': admin.ADid }) db.session.add( quest_instance, BASEADMIN().create_action(AdminActionS.insert.value, 'Quest', str(uuid.uuid1()))) db.session.add(answer_instance) BASEADMIN().create_action(AdminActionS.insert.value, 'Answer', str(uuid.uuid1())) return Success('创建问题成功')
def create_code(self): data = parameter_required(('coid', 'conum')) coid = data.get('coid') conum = data.get('conum') with db.auto_commit(): coupon = Coupon.query.filter_by(COid=coid, isdelete=False, COcode=1).first_('该优惠券不能生成兑换码') # 校验剩余数量 if coupon.COlimitNum: if int(coupon.COremainNum) < int(conum): raise StatusError('兑换数量超过剩余数量') coupon.update( {'COremainNum': int(coupon.COremainNum) - int(conum)}) # isinstance_list = [coupon] db.session.add(coupon) for _ in range(int(data.get('conum'))): ccid = str(uuid.uuid1()) cccode = self.coupen_code() coupon_code = CouponCode.create({ 'CCid': ccid, 'COid': coid, 'CCcode': cccode }) db.session.add(coupon_code) BASEADMIN().create_action(AdminActionS.insert.value, 'CouponCode', coid) db.session.flush() return Success('生成激活码成功', data={'coid': coid, 'conum': conum})
def update(self): data = parameter_required(('skuid', )) skuid = data.get('skuid') price = data.get('skuprice') stock = data.get('skustock') if price: if price < 0: raise ParamsError('价格小于0') price = round(price, 2) if stock and stock < 0: raise ParamsError('库存小于0') skuattritedetail = data.get('skuattritedetail') with self.sproduct.auto_commit() as s: sku = s.query(ProductSku).filter_by_({ 'SKUid': skuid }).first_('sku不存在') product = self.sproduct.get_product_by_prid(sku.PRid) prattribute = json.loads(product.PRattribute) if len(skuattritedetail) != len(prattribute) or not isinstance( skuattritedetail, list): raise ParamsError('skuattritedetail 参数不准确') skuattritedetail = json.dumps(skuattritedetail) sku_dict = { 'SKUpic': data.get('skupic'), 'SKUprice': price, 'SKUstock': stock, 'SKUattriteDetail': skuattritedetail, } [setattr(sku, k, v) for k, v in sku_dict.items() if v is not None] s.add(sku) if is_admin(): BASEADMIN().create_action(AdminActionS.update.value, 'ProductSku', skuid) return Success('更新成功')
def create(self): """创建场景""" form = SceneCreateForm().valid_data() psendtime = form.psendtime.data with self.sproducts.auto_commit() as s: scene_dict = { 'PSid': str(uuid.uuid1()), 'PSpic': form.pspic.data, 'PSname': form.psname.data, 'PSsort': form.pssort.data, 'PStimelimited': form.pstimelimited.data, 'PSstarttime': form.psstarttime.data, 'PSendtime': psendtime } product_scene_instance = ProductScene.create(scene_dict) s.add(product_scene_instance) # 每个场景下默认增加一个“大行星精选”标签 default_scene_item = SceneItem.create({ 'SIid': str(uuid.uuid1()), 'PSid': scene_dict.get('PSid'), 'ITid': 'planet_featured' }) s.add(default_scene_item) BASEADMIN().create_action(AdminActionS.insert.value, 'ProductScene', str(uuid.uuid1())) if form.pstimelimited.data: from planet.extensions.tasks import cancel_scene_association current_app.logger.info('限时场景结束时间 : {} '.format(psendtime)) scene_task_id = cancel_scene_association.apply_async(args=(scene_dict['PSid'],), eta=psendtime - timedelta(hours=8), ) current_app.logger.info("场景id{} 任务返回的task_id: {}".format(scene_dict['PSid'], scene_task_id)) conn.set(scene_dict['PSid'], scene_task_id) return Success('创建成功', data={'psid': product_scene_instance.PSid})
def delete_question(self): """后台管理员删除问题""" if not is_admin(): raise AuthorityError('权限不足') admin = Admin.query.filter_by_(ADid=request.user.id).first_('权限已被回收') data = parameter_required(('qulist', )) qulist = data.get('qulist') for quid in qulist: qu = Quest.query.filter_by_(QUid=quid).first() if not qu: continue qu.isdelete = True qalist = Answer.query.filter_by_(QUid=qu.QUid).all() for qa in qalist: qa.isdelete = True qan = QuestAnswerNote.create({ 'QANid': str(uuid.uuid1()), 'QANcontent': '删除问题', 'QANcreateid': admin.ADid, 'QANtargetId': quid, 'QANtype': QuestAnswerNoteType.qu.value, }) db.session.add(qan) BASEADMIN().create_action(AdminActionS.delete.value, 'QuestAnswerNote', str(uuid.uuid1())) return Success('删除完成')
def shelves(self): """批量下架试用商品""" if is_supplizer(): usid = request.user.id sup = self._check_supplizer(usid) current_app.logger.info('Supplizer {} shelves commodity'.format( sup.SUname)) elif is_admin(): usid = request.user.id admin = self._check_admin(usid) current_app.logger.info('Admin {} shelves commodity'.format( admin.ADname)) sup = None else: raise AuthorityError() data = parameter_required(("tcids", )) tcid_list = data.get('tcids') for tcid in tcid_list: commodity = TrialCommodity.query.filter_by_( TCid=tcid).first_('未找到商品信息, tcid参数异常') if sup: assert commodity.CreaterId == usid, '供应商只能下架自己上传的商品' if commodity.TCstatus != TrialCommodityStatus.upper.value: raise StatusError('只能下架正在上架状态的商品') with db.auto_commit(): commodity.TCstatus = TrialCommodityStatus.reject.value if is_admin(): BASEADMIN().create_action(AdminActionS.update.value, 'TrialCommodity', tcid) return Success('下架成功', {'tcid': tcid_list})
def delete(self): data = parameter_required(('pcid', )) pcid = data.get('pcid') with self.sproduct.auto_commit() as s: product_category_instance = s.query(ProductCategory).filter_by_({ 'PCid': pcid }).first_('该分类不存在') product_category_instance.isdelete = True s.add(product_category_instance) BASEADMIN().create_action(AdminActionS.delete.value, 'ProductCategory', pcid) s.query(Products).filter_( Products.PCid == product_category_instance.PCid).update( { 'PRstatus': ProductStatus.off_shelves.value, 'PCid': None }, synchronize_session=False) # else: # parent_catetgory_instance = s.query(ProductCategory).filter_by_({'PCid': parent_catetgory_id}).first_() # if not parent_catetgory_instance: # 父级目录已删除 # s.query(Products).filter_(Products.PCid.in_(sub_ids)).update({ # 'PCid': 'null' # }, synchronize_session=False) # else: # pass # s.query(Products).filter_by_({ # 'PCid': pcid # }).update({ # 'PCid': parent_catetgory_id # }, synchronize_session=False) return Success('删除成功')
def upload_delivery_file(self): """ 订单批量发货,文件上传入口 :return: """ if is_admin(): Admin.query.filter_by(ADid=request.user.id, ADstatus=AdminStatus.normal.value, isdelete=False).first_('管理员账号错误') elif is_supplizer(): Supplizer.query.filter_by(SUid=request.user.id, SUstatus=UserStatus.usual.value, isdelete=False).first_('供应商账号状态错误') else: raise TokenError('未登录') file = request.files.get('file') if not file: raise ParamsError('未上传文件') current_app.logger.info('Start Add Delivery Template {}'.format( datetime.now())) folder = 'xls' # 接收数据保存到服务器 file_path = self._save_excel(file, folder) # 进行订单发货 nrows = self._order_delivery(file_path) if is_admin(): with db.auto_commit(): BASEADMIN().create_action(AdminActionS.insert.value, 'file', file_path) current_app.logger.info('End Add Delivery Template {}'.format( datetime.now())) return Success('批量发货成功, 共发货 {} 个订单'.format(nrows - 1))
def delete(self): """删除申请""" # if is_supplizer(): # usid = request.user.id # sup = Supplizer.query.filter_by_(SUid=usid).first_('供应商信息错误') # suid = sup.SUid # current_app.logger.info('Supplizer {} delete integral apply'.format(sup.SUname)) if is_admin(): usid = request.user.id admin = Admin.query.filter_by_(ADid=usid).first_('管理员信息错误') current_app.logger.info('Admin {} delete integral apply'.format( admin.ADname)) sup = None suid = None else: raise AuthorityError() data = parameter_required(('ipid', )) ipid = data.get('ipid') with db.auto_commit(): apply_info = IntegralProduct.query.filter_by( IPid=ipid, isdelete=False).first_('没有该商品记录') if sup and apply_info.SUid != suid: raise ParamsError('只能删除自己提交的申请') if apply_info.IPstatus not in [ ApplyStatus.cancle.value, ApplyStatus.reject.value, ApplyStatus.shelves.value ]: raise StatusError('只能删除已下架或已撤销状态下的申请') apply_info.isdelete = True IntegralProductSku.query.filter( IntegralProductSku.IPid == apply_info.IPid).delete_() if is_admin(): BASEADMIN().create_action(AdminActionS.delete.value, 'IntegralProduct', apply_info.IPid) return Success('删除成功', {'ipid': ipid})
def delete_apply(self): """删除申请""" if is_supplizer(): usid = request.user.id sup = Supplizer.query.filter_by_(SUid=usid).first_('供应商信息错误') current_app.logger.info( 'Supplizer {} delete magicbox apply'.format(sup.SUname)) elif is_admin(): usid = request.user.id admin = Admin.query.filter_by_(ADid=usid).first_('管理员信息错误') current_app.logger.info('Admin {} magicbox apply'.format( admin.ADname)) sup = None else: raise AuthorityError() data = parameter_required(('mbaid', )) mbaid = data.get('mbaid') with db.auto_commit(): apply_info = MagicBoxApply.query.filter_by_( MBAid=mbaid).first_('无此申请记录') if sup and apply_info.SUid != sup.SUid: raise ParamsError('只能删除自己提交的申请') if apply_info.MBAstatus not in [ ApplyStatus.cancle.value, ApplyStatus.reject.value, ApplyStatus.shelves.value ]: raise StatusError('只能删除已下架、已拒绝、已撤销状态下的申请') apply_info.isdelete = True MagicBoxApplySku.query.filter_by_(MBAid=apply_info.MBAid).delete_() if is_admin(): BASEADMIN().create_action(AdminActionS.delete.value, 'MagicBoxApply', mbaid) return Success('删除成功', {'mbaid': mbaid})
def offshelves(self): current_app.logger.info('下架供应商') data = parameter_required(('suid', )) suid = data.get('suid') with db.auto_commit(): supplizer = Supplizer.query.filter( Supplizer.isdelete == False, Supplizer.SUid == suid).first_('供应商不存在') supplizer.SUstatus = UserStatus.forbidden.value db.session.add(supplizer) BASEADMIN().create_action(AdminActionS.update.value, 'Supplizer', suid) # 供应商的品牌也下架 brand_count = ProductBrand.query.filter( ProductBrand.isdelete == False, ProductBrand.PBstatus == ProductBrandStatus.upper.value, ProductBrand.SUid == suid).update( {'PBstatus': ProductBrandStatus.off_shelves.value}) current_app.logger.info('共下架了 {}个品牌'.format(brand_count)) # 供应商的商品下架 products_count = Products.query.filter( Products.isdelete == False, Products.PRstatus != ProductStatus.off_shelves.value, Products.CreaterId == suid).update({ 'PRstatus': ProductStatus.off_shelves.value, }) current_app.logger.info('共下架了 {}个商品'.format(products_count)) return Success('供应商下架成功')
def cancel_commodity_apply(self): """撤销自己的申请""" if is_supplizer(): usid = request.user.id sup = self._check_supplizer(usid) current_app.logger.info('Supplizer {} cancel commodity'.format( sup.SUname)) elif is_admin(): usid = request.user.id admin = self._check_admin(usid) current_app.logger.info('Admin {} cancel commodity'.format( admin.ADname)) else: raise AuthorityError() data = parameter_required(('tcid', )) tcid = data.get('tcid') with db.auto_commit(): commodity = TrialCommodity.query.filter_by_( TCid=tcid).first_('无此商品信息') if commodity.TCstatus != TrialCommodityStatus.auditing.value: raise StatusError('只有在审核状态的申请可以撤销') if commodity.CreaterId != request.user.id: raise AuthorityError('仅可撤销自己提交的申请') commodity.TCstatus = TrialCommodityStatus.cancel.value # 同时将正在进行的审批流改为取消 approval_info = Approval.query.filter_by_( AVcontent=tcid, AVstartid=request.user.id, AVstatus=ApplyStatus.wait_check.value).first() approval_info.AVstatus = ApplyStatus.cancle.value if is_admin(): BASEADMIN().create_action(AdminActionS.update.value, 'TrialCommodity', tcid) return Success('取消成功', {'tcid': tcid})
def delete(self): """删除""" data = parameter_required(('suid', )) suid = data.get('suid') with db.auto_commit(): supplizer = Supplizer.query.filter( Supplizer.isdelete == False, Supplizer.SUid == suid).first_('供应商不存在') if self._check_lasting_order(suid=suid): raise StatusError('供应商部分订单正在进行') supplizer.isdelete = True db.session.add(supplizer) BASEADMIN().create_action(AdminActionS.delete.value, 'Supplizer', suid) # 品牌删除 productbrands = ProductBrand.query.filter( ProductBrand.isdelete == False, ProductBrand.SUid == suid).all() current_app.logger.info('删除供应商{}'.format(supplizer.SUname)) for pb in productbrands: pb.isdelete = True db.session.add(pb) # 商品删除 delete_product = Products.query.filter( Products.isdelete == False, Products.PBid == pb.PBid).update( {'PRstatus': ProductStatus.off_shelves.value}) return Success('删除成功')
def shelf_award(self): """撤销申请""" if not is_supplizer() and not is_admin(): raise AuthorityError() form = ShelfFreshManfirstOrder().valid_data() fmfaid = form.fmfaid.data with db.auto_commit(): suid = request.user.id if is_supplizer() else None apply_query = FreshManFirstApply.query.filter( FreshManFirstApply.isdelete == False, FreshManFirstApply.FMFAid == fmfaid, FreshManFirstApply.FMFAstatus == ApplyStatus.wait_check.value ) if suid: apply_query = apply_query.filter( FreshManFirstApply.SUid == request.user.id, ) apply = apply_query.first_('申请已处理') # 库存处理 self._re_stock(apply) apply.FMFAstatus = ApplyStatus.cancle.value db.session.add(apply) # 相应的审批流 Approval.query.filter( Approval.isdelete == False, Approval.AVcontent == fmfaid ).update({ 'AVstatus': ApplyStatus.cancle.value }) if is_admin(): BASEADMIN().create_action(AdminActionS.update.value, 'FreshManFirstApply', fmfaid) return Success('撤销成功')
def del_commodity(self): """删除试用商品""" if is_supplizer(): usid = request.user.id sup = self._check_supplizer(usid) current_app.logger.info('Supplizer {} delete commodity'.format( sup.SUname)) elif is_admin(): usid = request.user.id admin = self._check_admin(usid) current_app.logger.info('Admin {} delete commodity'.format( admin.ADname)) sup = None else: raise AuthorityError() data = parameter_required(("tcid", )) tcid = data.get('tcid') commodity = TrialCommodity.query.filter_by_( TCid=tcid).first_('未找到商品信息, tcid参数异常') if sup: assert commodity.CreaterId == usid, '供应商只能删除自己上传的商品' if commodity.TCstatus not in [ TrialCommodityStatus.reject.value, TrialCommodityStatus.cancel.value ]: raise StatusError('只能删除已下架或已撤销的商品') with db.auto_commit(): TrialCommodity.query.filter_by(TCid=tcid).delete_() TrialCommodityImage.query.filter_by(TCid=tcid).delete_() TrialCommoditySku.query.filter_by(TCid=tcid).delete_() TrialCommoditySkuValue.query.filter_by(TCid=tcid).delete_() if is_admin(): BASEADMIN().create_action(AdminActionS.delete.value, 'TrialCommodity', tcid) return Success('删除成功', {'tcid': tcid})
def delete(self): data = parameter_required(('skuid', )) skuid = data.get('skuid') with self.sproduct.auto_commit() as s: count = s.query(ProductSku).filter_by_({"SKUid": skuid}).delete_() BASEADMIN().create_action(AdminActionS.delete.value, 'ProductSku', skuid) if not count: raise NotFound('不存在的sku') return Success('删除成功')
def update_code(self): data = parameter_required(('coid', 'cocode')) coupon = Coupon.query.filter_by(COid=data.get('coid'), isdelete=False).first_('优惠券已删除') with db.auto_commit(): coupon.update({'COcode': bool(data.get('cocode', False))}) BASEADMIN().create_action(AdminActionS.update.value, 'Coupon', data.get('coid')) return Success('修改成功', data={'coid': coupon.COid})
def shelf_award(self): """撤销申请""" if not (is_supplizer() or is_admin()): raise AuthorityError() data = parameter_required(('tlpid', )) tlpid = data.get('tlpid') if is_supplizer(): tlp_from = ApplyFrom.supplizer.value suid = request.user.id else: tlp_from = ApplyFrom.platform.value suid = None with db.auto_commit(): apply_info = TimeLimitedProduct.query.filter_by( TLPid=tlpid, isdelete=False).first_('无此申请记录') if apply_info.TLAstatus != ApplyStatus.wait_check.value: raise StatusError('只有在审核状态的申请可以撤销') if apply_info.SUid != suid: raise AuthorityError('仅可撤销自己提交的申请') apply_info.TLAstatus = ApplyStatus.cancle.value # 获取原商品属性 # gnap_old = GuessNumAwardProduct.query.filter(GuessNumAwardProduct.GNAAid == apply_info.GNAAid, # GuessNumAwardProduct.isdelete == False).first() product = Products.query.filter_by(PRid=apply_info.PRid, PRfrom=tlp_from, isdelete=False).first_('商品信息出错') # 获取原sku属性 tls_list = TimeLimitedSku.query.filter( TimeLimitedSku.isdelete == False, TimeLimitedSku.TLPid == apply_info.TLPid).all() # 遍历原sku 将库存退出去 for sku in tls_list: sku_instance = ProductSku.query.filter_by( isdelete=False, PRid=product.PRid, SKUid=sku.SKUid).first_('商品sku信息不存在') self._update_stock(int(sku.TLSstock), product, sku_instance) # 同时将正在进行的审批流改为取消 todo approval_info = Approval.query.filter_by( AVcontent=tlpid, AVstartid=request.user.id, isdelete=False, AVstatus=ApplyStatus.wait_check.value).first() approval_info.AVstatus = ApplyStatus.cancle.value if is_admin(): BASEADMIN().create_action(AdminActionS.update.value, 'TimeLimitedProduct', tlpid) return Success('取消成功', {'tlpid': tlpid})
def delete(self): if not is_admin(): raise AuthorityError() data = SignIndelete().valid_data() siaid = data.siaid.data check_sia = SignInAward.query.filter_by(SIAid=siaid, isdelete=False).delete_() BASEADMIN().create_action(AdminActionS.delete.value, 'SignInAward', siaid) if not check_sia: raise ParamsError('已删除') return Success('删除设置成功')
def shelves(self): """下架申请""" if is_supplizer(): usid = request.user.id sup = Supplizer.query.filter_by_(SUid=usid).first_('供应商信息错误') current_app.logger.info('Supplizer {} shelf magicbox apply'.format( sup.SUname)) elif is_admin(): usid = request.user.id admin = Admin.query.filter_by_(ADid=usid).first_('管理员信息错误') current_app.logger.info('Admin {} shelf magicbox apply'.format( admin.ADname)) sup = None else: raise AuthorityError() data = parameter_required(('mbaid', )) mbaid = data.get('mbaid') pending_box = MagicBoxJoin.query.filter( MagicBoxJoin.isdelete == False, MagicBoxJoin.MBJstatus == MagicBoxJoinStatus.pending.value, MagicBoxJoin.MBSendtime >= datetime.now().date(), MagicBoxJoin.MBAid == mbaid).first() if pending_box: current_app.logger.info('仍存在未完成礼盒 MBJid: {}'.format( pending_box.MBJid)) raise StatusError('该商品仍有正在分享中的礼盒未完成,暂不能下架') with db.auto_commit(): apply_info = MagicBoxApply.query.filter_by_( MBAid=mbaid).first_('无此申请记录') if sup and apply_info.SUid != usid: raise StatusError('只能下架自己的申请') if apply_info.MBAstatus != ApplyStatus.agree.value: raise StatusError('只能下架已通过的申请') apply_info.MBAstatus = ApplyStatus.shelves.value if is_admin(): BASEADMIN().create_action(AdminActionS.update.value, 'MagicBoxApply', mbaid) # 返回库存 product = Products.query.filter_by(PRid=apply_info.PRid, isdelete=False).first_('商品信息出错') mbs_old = MagicBoxApplySku.query.filter( MagicBoxApplySku.MBAid == apply_info.MBAid, MagicBoxApplySku.isdelete == False, ).all() for sku in mbs_old: sku_instance = ProductSku.query.filter_by( isdelete=False, PRid=product.PRid, SKUid=sku.SKUid).first_('商品sku信息不存在') super(CMagicBox, self)._update_stock(int(sku.MBSstock), product, sku_instance) return Success('下架成功', {'mbaid': mbaid})
def create(self): form = ItemCreateForm().valid_data() psid = form.psid.data itname = form.itname.data itsort = form.itsort.data itdesc = form.itdesc.data ittype = form.ittype.data itrecommend = form.itrecommend.data itid = str(uuid.uuid1()) with self.sproduct.auto_commit() as s: if s.query(Items).filter_by(ITname=itname, ITtype=ittype, isdelete=False).first(): raise DumpliError("您输入的标签名已存在") s_list = [] # 添加标签 item_dict = { 'ITid': itid, 'ITname': itname, 'ITsort': itsort, 'ITdesc': itdesc, 'ITtype': ittype, 'ITrecommend': itrecommend } items_instance = Items.create(item_dict) s_list.append(items_instance) BASEADMIN().create_action(AdminActionS.insert.value, 'Items', itid) # 标签场景标签表 if psid: for psi in psid: s.query(ProductScene).filter_by_({'PSid': psi}).first_('不存在的场景') scene_item_dict = { 'PSid': psi, 'ITid': itid, 'SIid': str(uuid.uuid1()) } scene_item_instance = SceneItem.create(scene_item_dict) s_list.append(scene_item_instance) BASEADMIN().create_action(AdminActionS.insert.value, 'SceneItem', psi) s.add_all(s_list) return Success('添加成功', {'itid': itid})
def set_entry(self): current_app.logger.info("Admin {} set entry banner".format(request.user.username)) data = parameter_required(('contentlink', 'enpic', 'enshow')) enid = data.get('enid') or str(uuid.uuid1()) with db.auto_commit(): en = Entry.query.filter( Entry.isdelete == False, Entry.ENid == enid).first() if data.get('delete'): if not en: raise ParamsError('banner 已删除') en.update({'isdelete': True}) db.session.add(en) BASEADMIN().create_action(AdminActionS.delete.value, 'Entry', enid) return Success('删除成功', {'enid': enid}) endict = { 'contentlink': data.get('contentlink'), 'ENpic': data.get('enpic'), 'ENshow': bool(data.get('enshow')), 'ENtype': data.get('entype') } if not en: endict.setdefault('ENid', enid) endict.setdefault('ACid', request.user.id) en = Entry.create(endict) BASEADMIN().create_action(AdminActionS.insert.value, 'Entry', enid) msg = '添加成功' else: en.update(endict) BASEADMIN().create_action(AdminActionS.update.value, 'Entry', enid) msg = '修改成功' db.session.add(en) if en.ENshow: Entry.query.filter( Entry.ENid != enid, Entry.isdelete == False, Entry.ENtype == en.ENtype).update({'ENshow': False}) return Success(msg, {'enid': enid})
def update(self): form = SceneUpdateForm().valid_data() psid, pspic, psname, pssort = form.psid.data, form.pspic.data, form.psname.data, form.pssort.data pstimelimited, psstarttime, psendtime = form.pstimelimited.data, form.psstarttime.data, form.psendtime.data isdelete = form.isdelete.data with db.auto_commit(): pssort = self._check_sort(pssort) product_scene = ProductScene.query.filter(ProductScene.isdelete == False, ProductScene.PSid == psid ).first_('不存在的场景') if isdelete: SceneItem.query.filter_by(PSid=psid).delete_() product_scene.isdelete = True admin_action = AdminActions.create({ 'ADid': request.user.id, 'AAaction': 2, 'AAmodel': ProductScene, 'AAdetail': request.detail, 'AAkey': psid }) db.session.add(admin_action) conn.delete(psid) else: product_scene.update({ "PSpic": pspic, "PSname": psname, "PSsort": pssort, "PStimelimited": pstimelimited, "PSstarttime": psstarttime, "PSendtime": psendtime, }, null='not') db.session.add(product_scene) BASEADMIN().create_action(AdminActionS.update.value, 'ProductScene', psid) if form.pstimelimited.data: from planet.extensions.tasks import cancel_scene_association, celery current_app.logger.info('更新限时场景结束时间为 : {} '.format(psendtime)) # celery.control.revoke(task_id=psid, terminate=True, signal='SIGKILL') exist_task = conn.get(psid) if exist_task: exist_task = str(exist_task, encoding='utf-8') current_app.logger.info('场景已有任务id: {}'.format(exist_task)) celery.AsyncResult(exist_task).revoke() scene_task_id = cancel_scene_association.apply_async(args=(psid,), eta=psendtime - timedelta(hours=8), ) conn.set(psid, scene_task_id) return Success('更新成功', {'psid': psid})
def set_banner(self): current_app.logger.info("Admin {} set index banner".format(request.user.username)) form = IndexSetBannerForm().valid_data() ibid = str(uuid.uuid1()) with db.auto_commit(): banner = IndexBanner.create({ 'IBid': ibid, 'contentlink': form.contentlink.data, 'IBpic': form.ibpic.data, 'IBsort': form.ibsort.data, 'IBshow': form.ibshow.data }) db.session.add(banner) BASEADMIN().create_action(AdminActionS.insert.value, 'IndexBanner', ibid) return Success('添加成功', {'ibid': ibid})
def set_hypermarket_banner(self): current_app.logger.info("Admin {} set index banner".format(request.user.username)) # data = parameter_required(('contentlink', 'hibpic', 'hibshow')) data = parameter_required() ibid = data.get('hibid') or str(uuid.uuid1()) with db.auto_commit(): hib = HypermarketIndexBanner.query.filter( HypermarketIndexBanner.isdelete == False, HypermarketIndexBanner.HIBid == ibid).first() if data.get('delete'): if not hib: raise ParamsError('banner 已删除') hib.update({'isdelete': True}) db.session.add(hib) return Success('删除成功', {'hibid': ibid}) hibsort = self._check_sort(data.get('hibsort')) hib_dict = { 'contentlink': data.get('contentlink'), 'HIBpic': data.get('hibpic'), 'HIBsort': hibsort, 'HIBshow': bool(data.get('hibshow')) } if not hib: hib_dict.setdefault('HIBid', ibid) hib = HypermarketIndexBanner.create(hib_dict) BASEADMIN().create_action(AdminActionS.insert.value, 'HypermarketIndexBanner', ibid) msg = '添加成功' else: hib.update(hib_dict) BASEADMIN().create_action(AdminActionS.update.value, 'HypermarketIndexBanner', ibid) msg = '修改成功' db.session.add(hib) return Success(msg, {'hibid': ibid})
def del_award(self): """删除申请""" if not is_supplizer() and not is_admin(): raise AuthorityError() form = ShelfFreshManfirstOrder().valid_data() fmfaid = form.fmfaid.data with db.auto_commit(): apply_info = FreshManFirstApply.query.filter_by_(FMFAid=fmfaid).first_('申请未找到或已删除') if is_supplizer(): assert apply_info.SUid == request.user.id, '供应商只能删除自己提交的申请' if apply_info.FMFAstatus not in [ApplyStatus.cancle.value, ApplyStatus.reject.value]: raise StatusError('只能删除已拒绝或已撤销状态下的申请') apply_info.isdelete = True if is_admin(): BASEADMIN().create_action(AdminActionS.delete.value, 'FreshManFirstApply', fmfaid) return Success('删除成功', {'fmfaid': fmfaid})
def update(self): """设置活动的基本信息""" form = ActivityUpdateForm().valid_data() with db.auto_commit(): act = form.activity act.update({ 'ACbackGround': form.acbackground.data, 'ACtopPic': form.actoppic.data, 'ACbutton': form.acbutton.data, 'ACshow': form.acshow.data, 'ACdesc': form.acdesc.data, 'ACname': form.acname.data, 'ACsort': form.acsort.data, }) db.session.add(act) BASEADMIN().create_action(AdminActionS.update.value, 'Activity', str(act.ACid)) return Success('修改成功')