def regional_add(r, name, value, kindtype, status): """ 增加一个 regional """ if r is None or name is None or value is None: return responseto('Param please!', code=401) kindtype = parse_int(kindtype, 0) try: toml.loads(value) except toml.TomlDecodeError as e: msg = 'value is not a TOML string: %s' % str(e) logger.error(msg) return responseto(msg, code=401) now = int(time.time()) robj = Regional(r=r, name=name, value=value, status=status, kindtype=kindtype, createtime=now, updatetime=now) resp = commit_and_response_error(robj, refresh=True) if resp is not None: return resp return responseto(regional=robj, code=200)
def regional_edit(r, name, value, kindtype, status): """ 修改一个 regional """ status = parse_int(status, 1) kindtype = parse_int(kindtype) if r is None: return responseto('Param please!', code=401) robj = Regional.query.get(r) if robj is None: return responseto('找不到 regional %s!' % r, code=404) if name is not None: robj.name = name if value is not None: try: toml.loads(value) robj.value = value except toml.TomlDecodeError as e: msg = 'value is not a TOML string: %s' % str(e) logger.error(msg) return responseto(msg, code=401) if kindtype is not None: robj.kindtype = kindtype if status is not None: robj.status = status robj.updatetime = int(time.time()) resp = commit_and_response_error(robj, refresh=True) if resp is not None: return resp return responseto(regional=robj, code=200)
def delfile(self, file_path): message = None try: up = self._build_upyun() up.delete(file_path) return True, message except Exception as e: message = 'upyun delfile exception:%s' % str(e) logger.error(message) return None, message
def decorated_fun(*args, **kwargs): regional = request.args.get('r') r, robj = check_regional(regional, ignore_zero) if r is None: logger.error('@regional_checker_gdb CAN NOT find regional %s.', regional) abort(403) if add_r: kwargs['r'] = r if add_robj: kwargs['robj'] = robj return f(*args, **kwargs)
def get_post_data(): """ 优先作为 json 获取数据 如果不是 json 则获取 form 数据 """ if request.is_json: try: logger.info('get_post_data request.data: %s', request.data) return request.get_json() except Exception as e: logger.error('get_post_data request.get_json error:%s', str(e)) return None return request.form.to_dict()
def decorated_fun(*args, **kwargs): if gconfig.regional is None: logger.error('@regional_checker_gconfig NO RegionalConfig') abort(403) r = request.args.get('r') r, robj = gconfig.regional.check_regional(r, ignore_zero) if r is None: logger.regional('@regional_checker_gconfig CAN NOT find regional {}.'.format(r)) abort(403) if add_r: kwargs['r'] = r return f(*args, **kwargs)
def decorated_fun(*args, **kwargs): rdict = get_request_values(defaultvalue=defaultvalue, request_key=request_key) # kwargs2 = {} try: for k in request_params: if k in parse_int_params: kwargs[k] = parse_int(rdict.get(k)) else: kwargs[k] = rdict.get(k) except Exception as e: logger.error('checker.request_values request_params(%s) defaultvalue:(%s) request_key(%s) error: %s', request_params, defaultvalue, request_key, e) abort(401) return f(*args, **kwargs)
def upload2cdn(self, file_path, content): """ 上传文件到 CDN :param file_path: :param content: 可以是 file object :return: """ message = None try: up = self._build_upyun() res = up.put(file_path, content, checksum=True) logger.info('upyun upload file_path:%s res:%s' % (file_path, res)) return True, self.cdn_cfg.get('siteurl') + file_path except Exception as e: message = 'upyun upload exception:%s' % str(e) logger.error(message) return None, message
def regional_del(r): """ 删除一个 regional """ if r is None: return responseto('Param please!', code=401) # 必须先删除所有的与这个 Regional 相关的关系 if ValueObject.query.filter_by(r=r).first() is not None: return responseto('Please delete valueobjet of %s first!' % r, code=403) try: Regional.query.filter_by(r=r).delete() gdb.session.commit() except SQLAlchemyError as e: msg = 'regional_del error: ' + str(e) logger.error(msg) return responseto(msg, code=500) return responseto(code=200)
def getlist(self, dir_path, limit=None, begin=None): """ 获取 CDN 文件列表 :param dir_path: 文件夹地址 :param limit: 分页限制 :param order: 排序,默认 asc :return: {file: [], iter: str, siteurl: str, path: str} """ message = None try: up = self._build_upyun() res = up.get_list_with_iter(dir_path, limit, begin=begin) res['siteurl'] = self.cdn_cfg.get('siteurl') res['path'] = dir_path return True, res except Exception as e: message = 'upyun getlist exception:%s' % str(e) logger.error(message) return None, message
def decorated_fun(*args, **kwargs): if use_global: regional = 0 else: regional = request.args.get('r', 0) r, robj = check_regional(regional, True) if r is None: logger.error('@ip_checker_gdb CAN NOT find regional %s.', r) abort(403) ip = request.remote_addr ips = robj.merge().get('ips') # logger.info('@ip_checker_gdb ip: %s, ips: %s', ip, ips) if isinstance(ips, list) and not ip in ips: logger.error('@ip_checker_gdb IP %s is not in %s.', ip, ips) abort(403) return f(*args, **kwargs)
def commit_and_response_error(inst, refresh=False, delete=False, return_dict=False): """ 将提交一个 instance 并返回错误响应,封装成一个操作 """ try: if delete: gdb.session.delete(inst) else: gdb.session.add(inst) gdb.session.commit() if refresh: gdb.session.refresh(inst) return None except Exception as e: msg = str(e) logger.error(msg) gdb.session.rollback() resp_dict = {'error': True, 'message': msg, 'code': 500} if return_dict: return resp_dict return jsonify(resp_dict)
def decorated_fun(*args, **kwargs): if gconfig.regional is None: logger.error('@ip_checker_gconfig NO RegionalConfig') abort(403) r = request.args.get('r') r, robj = gconfig.regional.check_regional(r, False) if r is None: logger.error('@ip_checker_gconfig CAN NOT find regional %s.', r) abort(403) ip = request.remote_addr ips = robj.get('ips') # logger.info('@ip_checker_gconfig ip: %s, ips: %s', ip, ips) if isinstance(ips, list) and not ip in ips: logger.error('@ip_checker_gconfig IP %s is not in %s.', ip, ips) abort(403) return f(*args, **kwargs)
def valueobject_edit(r, withcache, vid=None, name=None, value=None, votype=None, status=None, index=None, note=None, valuetype=None, return_dict=False): """ 更新一个 VO """ if vid is not None: vid = parse_int(vid) if vid is None: return responseto(message='vid 必须是整数!', code=401, error=True, return_dict=return_dict) if status is not None: status = parse_int(status) if status is None: return responseto(message='status 必须是整数!', code=401, error=True, return_dict=return_dict) if index is not None: index = parse_int(index) try: value = valueobject_check_value(value, valuetype) except ValueError as e: logger.error('valueobject_edit %s', str(e)) return responseto(message='value 无法正常解析!请检查。', code=401, error=True, return_dict=return_dict) voitem = None if vid is not None: # 提供 vid 代表是修改 vo voitem = ValueObject.query.get(vid) elif name is not None: # 没有提供 vid 但提供了 name 也代表是修改 vo voitem = ValueObject.query.filter_by(name=name).first() if voitem is None: return responseto(message='找不到 vo!', code=404, error=True, return_dict=return_dict) voitem.updatetime = int(time.time()) # 只有明确提供了 status 才设置它 # 不处理 votype/r ,因为已经创建了的 VO 不允许修改这些关键分类的值 if name is not None: voitem.name = name if value is not None: voitem.value = value if status is not None: voitem.status = status if note is not None: voitem.note = note if index is not None: voitem.index = index if votype is not None: voitem.votype = votype resp = commit_and_response_error(voitem, refresh=True, return_dict=True) if resp is not None: return resp if withcache > 0: valueobj = ValueObject.load_value(value) gcache.setg(name, valueobj, r) return responseto(vo=voitem, error=False, code=200, return_dict=return_dict)
def valueobject_add(r, withcache, name, value, votype, status=None, index=None, note=None, valuetype=None, return_dict=False): """ 增加一个 VO """ if name is None or value is None or votype is None: return responseto(message='必须提供 name/value/votype!', code=401, error=True, return_dict=return_dict) if status is not None: status = parse_int(status) if status is None: return responseto(message='status 必须是整数!', code=401, error=True, return_dict=return_dict) if index is not None: index = parse_int(index) try: value = valueobject_check_value(value, valuetype) except ValueError as e: logger.error('valueobject_add %s', str(e)) return responseto(message='value 无法正常解析!请检查。', code=401, error=True, return_dict=return_dict) votype = parse_int(votype) if votype is None: return responseto(message='请提供 votype!', code=401, error=True, return_dict=return_dict) now = int(time.time()) voitem = ValueObject(name=name, value=value, status=status if status is not None else 1, index=index if index is not None else 0, r=r, votype=votype, createtime=now, updatetime=now, note=note) resp = commit_and_response_error(voitem, refresh=True, return_dict=True) if resp is not None: return resp if withcache > 0: valueobj = ValueObject.load_value(value) gcache.setg(name, valueobj, r) return responseto(vo=voitem, error=False, code=200, return_dict=return_dict)