Exemplo n.º 1
0
def build_resource_api_info(api_yaml, all_models, custom_configs):
    apis = {underscore(k): k for k in custom_configs}
    if len(apis) != len(custom_configs):
        raise Exception("There are same indexes for apis. Note: the api "
                        "index is case insensitive")

    result = {}
    for k, k1 in apis.items():
        cc = custom_configs[k1]

        op_id = str(cc.get("operation_id", ""))
        if not op_id:
            raise Exception("Must set operation_id for api(%s)" % k1)

        api = api_yaml.get(underscore(op_id))
        if not api:
            raise Exception("Unknown opertion id:%s" % op_id)

        r = _build_api_info(k, api, all_models, cc)

        if len(r["crud"]) != 1:
            raise Exception("The crud of api(%s) must be one of c, r, u, d" %
                            k)

        r["name"] = k
        r["api_index"] = k
        r["op_id"] = op_id
        r["api"] = api
        r["has_response_body"] = (api.get("response", {}).get("datatype")
                                  in all_models)
        r["verb"] = api["method"].upper()
        r["async"] = cc.get("async")

        if cc.get("exclude_for_schema"):
            r["exclude_for_schema"] = True

        for i in [
                "path_parameter", "header_params", "service_type",
                "success_codes"
        ]:
            if i in cc:
                r[i] = cc[i]

        _remove_project(r)

        result[k] = r

    # avoid generating properties of both read and list
    read_api = fetch_api(result, "read")
    list_api = fetch_api(result, "list")
    if read_api and list_api:
        list_api["exclude_for_schema"] = True

    for i in ["create", "delete"]:
        if fetch_api(result, i) is None:
            raise Exception("Must configue %s api" % i)

    return result
Exemplo n.º 2
0
    def init(cls, flask_or_import_name, project=None, directory=None,
             config=None, exceptions=None, compress_html=True):
        """
        Allow to register all subclasses of Webmaster at once

        If a class doesn't have a route base, it will create a dasherize version
        of the class name.

        So we call it once initiating
        :param flask_or_import_name: Flask instance or import name -> __name__
        :param project: name of the project. If the directory and config is empty, it will guess them from here
        :param directory: The directory containing your project's Views, Templates and Static
        :param config: string of config object. ie: "app.config.Dev"
        :param exceptions: The exceptions path to load
        :param compress_html: bool - If true it will use the plugin "jinja2htmlcompress"
                to remove white spaces off the html resul
        """

        if isinstance(flask_or_import_name, Flask):
            app = flask_or_import_name
        else:
            app = Flask(flask_or_import_name)

        app.wsgi_app = ProxyFix(app.wsgi_app)

        app.url_map.converters['regex'] = RegexConverter

        if not directory:
            directory = "application/%s" % project if project else "."

        if not config:
            config = "application.config.%s" % get_env()

        app.config.from_object(config)

        # Extensions to remove extra white spaces in html
        if compress_html:
            app.jinja_env.add_extension('webmaster.htmlcompress_ext.HTMLCompress')

        if directory:
            app.template_folder = directory + "/templates"
            app.static_folder = directory + "/static"

        # Setup map base exceptions to abort()
        abort.map_from_module(exceptions)

        cls._app = app

        cls._setup_logger()

        cls._add_asset_bundle(app.static_folder)

        # Flask Assets
        cls.assets = Environment(cls._app)

        # Register templates
        if cls._template_paths:
            loader = [cls._app.jinja_loader] + list(cls._template_paths)
            cls._app.jinja_loader = jinja2.ChoiceLoader(loader)

        # Register static
        if cls._static_paths:
            loader = [cls._app.static_folder] + list(cls._static_paths)
            cls.assets.load_path = loader

        # init_app
        [_app(cls._app) for _app in cls._init_apps]

        # Register all views
        for subcls in cls.__subclasses__():
            base_route = subcls.base_route
            if not base_route:
                base_route = utils.dasherize(utils.underscore(subcls.__name__))
                if subcls.__name__.lower() == "index":
                    base_route = "/"
            subcls.register(cls._app, base_route=base_route)

        # Load all bundles
        [cls.assets.from_yaml(a) for a in cls._asset_bundles]

        @cls._app.after_request
        def _after_request_cleanup(response):
            cls._global["__META__"] = cls._default_page_meta
            return response

        return cls._app
Exemplo n.º 3
0
 def wrapper(cls):
     route.routes.append(URLSpec(pattern, cls, name=name or "%s_%s" % (
         cls.__module__.split(".")[-1],
         utils.underscore(cls.__name__[:-7])
     )))
     return cls