def preparing(db: Session, obj_list: str, visible_bucket: str): """ 尝试准备数据,并根据需求添加filter,允许准备失败返回默认值 应对 视频id 找不到对应记录而引发的错误 视频准备失败不应该引发poster报错 1. add the filter 2. try dealing """ data = [] # 1. filter if obj_list is None: return data if obj_list == '': return data # 2. try dealing the data try: data = [ _video_id_to_dict(db, int(i), visible_bucket) for i in obj_list.split(',') ] except Exception as e: data = [{} for i in obj_list.split(',')] logworker.exception("preparing video") logworker.error([obj_list, visible_bucket]) return data
def poster_user_get(): # 1. check data - offset limit try: req = PageRequestSchema().load(request.args) except marshmallow.ValidationError as err: logworker.exception(request.args) return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages)) # 2. check authority # None 查看自己的 不用验证 # 3. querying try: data = poster_usecase.obtain( g.db, to_user_id=request.current_user_id, visible=RoleVisible.Buyer.value, offset=req.offset, limit=req.limit) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) # 4. prepare return data data = PosterSchema(many=True).dump(data) return jsonify(DataResponse(data))
def poster_visible(): # 1. check data - poster id try: req = PosterVisibleSchema().load(request.json) except marshmallow.ValidationError as err: logworker.error(request.json) return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages)) # 2. check authorty # None 由visible控制是否可以操作 # 3. update querying try: poster_usecase.visible( g.db, req.poster_id, req.is_content, SET_VISIBLE[req.set_visible]) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) return jsonify(OKResponse(True))
def poster_reply(): """summary: 同时获得指定poster内容和它的所有回复 分享页H5""" # 1. check data try: req = PosterGetSchema().load(request.args) except marshmallow.ValidationError as err: logworker.exception(request.args) return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages)) # 2. check authority 目前是否可查看 try: auth_usecase.has_authority( g.db, 'obtain_poster_public', req.poster_id) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) # 3. query try: data = poster_usecase.obtain( g.db, poster_id=req.poster_id, visible=VisibleLevel.Public.value, share=True, wechat=True) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) data = PosterAndReplySchema().dump(data) return jsonify(DataResponse(data))
def poster_delete(): # 1. check data - poster id try: req = PosterDeleteSchema().load(request.json) except marshmallow.ValidationError as err: logworker.error(request.json) return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages)) # 2. check authorty try: auth_usecase.has_authority( g.db, 'delete_poster', req.poster_id) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) # querying try: poster_usecase.delete(g.db, req.poster_id) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) return jsonify(OKResponse(True))
def poster_post(): # 1. check data try: req = PosterPostSchema().load(request.json) except marshmallow.ValidationError as err: logworker.error(request.json) return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages)) # 2. check authority try: auth_usecase.has_authority( g.db, 'submit_poster', req.order_no) except exception.MoretimeException as exc: logworker.error(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) # 3. create querying try: poster = poster_usecase.submit(g.db, request.current_user_id, req) except exception.MoretimeException as exc: logworker.error(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) # 4. create querying #【这里尝试使用异步的方式调用】【去除】 此时可以返回了 try: order_usecase.submit( g.db, req.order_no, poster.id, request.current_user_id, poster.to_user_id) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) #result = PosterCreateSchema().dump(poster.id) return jsonify(DataResponse({"posterID": poster.id}))
def preparing(db, obj_list): """ 尝试准备数据,并根据需求添加filter,允许准备失败返回默认值 应对 图片id 找不到对应记录而引发的错误 图片准备失败不应该引发poster报错 1. add the filter 2. try dealing """ data = [] # 1. filter if obj_list is None: return data if obj_list == '': return data # 2. try dealing the data try: data = [_picture_id_to_dict(db, int(i)) for i in obj_list.split(',')] except Exception as e: data = [{} for i in obj_list.split(',')] logworker.exception("preparing picture") logworker.error([obj_list]) return data
def poster_share(): """ """ # 1. check data try: req = PosterGetSchema().load(request.args) except marshmallow.ValidationError as err: logworker.exception(request.args) return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages)) # 2. check authority 使用[更改可见性]权限 try: auth_usecase.has_authority( g.db, 'change_visible', req.poster_id) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) # 3. change visible try: poster_usecase.visible( g.db, req.poster_id, VisiblePart.Media.value, PublicVisible.Yes.value) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) # 4. query share information try: data = poster_usecase.obtain( g.db, poster_id=req.poster_id, share=True, visible=VisibleLevel.Private.value) except exception.MoretimeException as exc: logworker.exception(req) return jsonify(ErrorResponse(exc.error, data=str(exc))) data = PosterShareSchema().dump(data) return jsonify(DataResponse(data))