def info_login(): """ :function: info_login 模拟登录信息门户 """ LoginUrl = info_login_url TestUrl = info_login_test_url hashstr = request.headers.get('Authorization') if hashstr is None: raise ForbiddenError() base64_hashstr = hashstr[6:] id_password = base64.b64decode(base64_hashstr) sid, password = id_password.split(':') # set rds lru cache for speed up resolve nginx header # rds:6384 (restccnulru) password_hash = base64.b64encode(password) s = requests.Session() # s: 爬虫session对象 s.post(LoginUrl, { 'userName': sid, 'userPass': password }, headers=headers, proxies=proxy) r = s.get(TestUrl) if 'window.alert' in r.content: raise ForbiddenError() else: rds.hset('restccnulru', sid, password_hash) rds.save() return s, sid
def new_app(): """ :function: new_app :args: none :rv: json message 上传一个新的版本 """ if request.method == 'POST': name = request.get_json().get("name") version = request.get_json().get("version") download = request.get_json().get("download") update = request.get_json().get("update") intro = request.get_json().get("intro") size = request.get_json().get('size') app_data = { "name": name, "version": version, "download": download, "update": update, "intro": intro, "size": size, } if not rds.get('apps'): rds.set('apps', "[{'name':'ccnubox'}]") apps = ast.literal_eval(rds.get('apps')) apps.append(app_data) rds.set('apps', str(apps)) rds.save() return jsonify({'msg': 'add new version data'}), 201
def get_patch(): """获取华师匣子所有补丁包相关信息""" if not rds.get('patches'): rds.set('patches', "[{'name':'ccnubox_patch'}]") rds.save() patches = rds.get('patches') return eval(patches)
def new_patch(): """ :function: new_patch :args: none :rv: json message 上传一个新的补丁包 """ if request.method == 'POST': version = request.get_json().get("version") download = request.get_json().get("download") update = request.get_json().get("update") intro = request.get_json().get("intro") size = request.get_json().get('size') patch_data = { "version": version, "download": download, "update": update, "intro": intro, "size": size, } if not rds.get('patches'): rds.set('patches', "[{'name':'ccnubox_patch'}]") patches = ast.literal_eval(rds.get('patches')) patches.append(patch_data) rds.set('patches', str(patches)) rds.save() return jsonify({'msg': 'add new patch version'}), 201
def get_app(): """获取华师匣子所有版本相关信息""" if not rds.get('apps'): rds.set('apps', "[{'name':'ccnubox'}]") rds.save() apps = rds.get('apps') return eval(apps)
def push_register(): """ :function: push_register :args: none :rv: success message redis(6384): string key: "ids" vlaue: "['id1', 'id2']" 注册ios设备的unique_id """ if request.method == 'POST': unique_id = request.get_json().get('unique_id') sid = request.get_json().get('sid') # 学号 # 将unique_id写入数据库 if not rds.get('ids'): rds.set('ids', "[]") rds.save() ids = ast.literal_eval(rds.get('ids')) if unique_id and (unique_id not in ids): ids.append(unique_id) if sid: rds.hset('idsids', unique_id, sid) # db commit rds.set('ids', ids) rds.save() return jsonify({ 'message': 'add new unique_id' }), 201
def push_register(): """ :function: push_register :args: none :rv: success message redis(6384): string key: "ids" vlaue: "['id1', 'id2']" 注册ios设备的unique_id """ if request.method == 'POST': unique_id = request.get_json().get('unique_id') sid = request.get_json().get('sid') # 学号 # 将unique_id写入数据库 if not rds.get('ids'): rds.set('ids', "[]") rds.save() ids = ast.literal_eval(rds.get('ids')) if unique_id and (unique_id not in ids): ids.append(unique_id) if sid: rds.hset('idsids', unique_id, sid) # db commit rds.set('ids', ids) rds.save() return jsonify({'message': 'add new unique_id'}), 201
def add_product(): """ :function: add_product 添加一个木犀的产品 """ if request.method == 'PUT': name = request.get_json().get('name') icon = request.get_json().get('icon') url = request.get_json().get('url') intro = request.get_json().get('intro') products_dict = ast.literal_eval( rds.get('products') or '{"_products":[], "update":""}') products_list = products_dict.get('_products') products_list.append({ 'name': name, 'icon': icon, 'url': url, 'intro': intro }) products_dict['_products'] = products_list products_dict['update'] = time.time() rds.set('products', products_dict) rds.save() return jsonify({}), 200
def delete_patch_version(version): """删除华师匣子特定版本补丁的信息""" patches = eval(rds.get('patches')) # eval is evil for n, patch in enumerate(patches): if patch.get('version') == version: del patches[n] rds.set('patches', str(patches)) rds.save() return jsonify({'msg': 'delete patch version %s' % version}), 200
def delete_version(version): """删除华师匣子特定版本version的信息""" apps = eval(rds.get('apps')) for n, app in enumerate(apps): if app.get('version') == version: del apps[n] rds.set('apps', str(apps)) rds.save() return jsonify({'msg': 'delete version %s' % version}), 200
def update_ios_config(): """ 更新ios json配置 """ if request.method == 'PUT': config = request.get_json().get('config') if rds.get('ios_config'): rds.set('ios_config', config) rds.save() return jsonify({}), 200 else: return jsonify({}), 404
def create_ios_config(): """ 上传ios json配置 """ if request.method == 'POST': config = request.get_json().get('config') if not rds.get('ios_config'): rds.set('ios_config', config) rds.save() return jsonify({}), 201 else: return jsonify({'msg': 'ios configuration already uploaded'})
def delete_patch_version(version): """ :function: delete_patch_version :args: - version: 待删除的补丁包版本 :rv: json message 删除华师匣子特定版本补丁的信息 """ patches = ast.literal_eval(rds.get('patches')) for n, patch in enumerate(patches): if patch.get('version') == version: del patches[n] rds.set('patches', str(patches)) rds.save() return jsonify({'msg': 'delete patch version %s' % version}), 200
def delete_version(version): """ :function: delete_version :args: version - 待删除的版本号 :rv: json message 删除华师匣子特定版本version的信息 """ apps = ast.literal_eval(rds.get('apps')) for n, app in enumerate(apps): if app.get('version') == version: del apps[n] rds.set('apps', str(apps)) rds.save() return jsonify({'msg': 'delete version %s' % version}), 200j
def delete_product(): """删除某个应用""" if request.method == 'DELETE': if request.args.get('name'): name = request.args.get('name') products_dict = eval( rds.get('products') or '{"_products":[], "update":""}') products_list = products_dict.get('_products') if products_list: for product in products_list: if product.get('name') == name: products_list.remove(product) products_dict['_products'] = products_list products_dict['update'] = time.time() rds.set('products', products_dict) rds.save() return jsonify({}), 200 return jsonify({}), 404
def get_app(): """ :function: get_app :args: none :rv: 所有版本列表 redis1(6384): string key: "apps" value: "[{ "name": "", "update": "", "version": "", "download": "", "intro": "" }]" 获取华师匣子所有版本相关信息 """ if not rds.get('apps'): rds.set('apps', "[{'name':'ccnubox'}]") rds.save() apps = rds.get('apps') return ast.literal_eval(apps)
def get_patch(): """ :function: get_patch :args: none :rv: 所有补丁包的信息 redis1(6384)~patches - key: "patches" - value "[{ "version": version, "update": update, "download": download, "size": size, "intro": intro }]" string 获取华师匣子所有补丁包相关信息 """ if not rds.get('patches'): rds.set('patches', "[{'name':'ccnubox_patch'}]") rds.save() patches = rds.get('patches') return ast.literal_eval(patches)
def delete_product(): """ :function: delete_product 删除某个应用 """ if request.method == 'DELETE': if request.args.get('name'): name = request.args.get('name') products_dict = ast.literal_eval(rds.get('products') or '{"_products":[], "update":""}') products_list = products_dict.get('_products') if products_list: for product in products_list: if product.get('name') == name: products_list.remove(product) products_dict['_products'] = products_list products_dict['update'] = time.time() rds.set('products', products_dict) rds.save() return jsonify({}), 200 return jsonify({}), 404
def add_product(): """ :function: add_product 添加一个木犀的产品 """ if request.method == 'PUT': name = request.get_json().get('name') icon = request.get_json().get('icon') url = request.get_json().get('url') intro = request.get_json().get('intro') products_dict = ast.literal_eval(rds.get('products') or '{"_products":[], "update":""}') products_list = products_dict.get('_products') products_list.append({ 'name': name, 'icon': icon, 'url': url, 'intro': intro }) products_dict['_products'] = products_list products_dict['update'] = time.time() rds.set('products', products_dict) rds.save() return jsonify({}), 200