def api_available_module_public(module_name, rest): # security check module_name, rest = map(secure_filename, (module_name, rest)) if not allowed_file(rest): abort(403) if not catalog.is_available(module_name): abort(404) # get zip file from catalog with zipfile.ZipFile(catalog.get_zipfile(module_name)) as zf: try: module_conf_filename = os.path.join(module_name, path.CONFIG_FILE) with zf.open(module_conf_filename) as module_conf_zf: module_conf = json.load(module_conf_zf) public_dir = catalog.get_from_config(module_conf, 'public_directory') requested_file = os.path.join(module_name, public_dir, rest) with zf.open(requested_file) as requested_zf: try: res = None _, fname = tempfile.mkstemp() with open(fname, 'w') as fp: fp.write(requested_zf.read()) res = send_file(fname) finally: os.remove(fname) if res: return res else: abort(404) except (KeyError, IOError): abort(404)
def api_available_module_get_rate(module_name): """ TODO: api key """ # check that the module is indeed available if not catalog.is_available(module_name): app.logger.warning('Module "%s" not available', module_name) abort(404) # save the new rating try: return jsonify({ 'value': Rating.average(module_name) }) except (IndexError, ValueError, KeyError) as e: app.logger.exception(e) abort(404)
def api_available_module_set_rate(): """ TODO: api key """ # validate module name try: module_name = path.realname(request.form['name']) # hack except KeyError: abort(400) # check that the module is indeed available if not catalog.is_available(module_name): app.logger.warning('Module "%s" not available', module_name) abort(404) # save the new rating try: value = int(request.form['value']) Rating.create(module=module_name, value=value) return jsonify({ 'success': True }) except (ValueError, KeyError) as e: app.logger.exception(e) abort(404)