from flask import Blueprint

def {{ current_model.create_blueprint_from_app|base_name }}(app):
    """Create {{ current_model.package_name }} blueprint."""
    if app.config.get('{{ current_model.config_resource_register_blueprint_key }}', True):
        blueprint = app.extensions["{{ current_model.flask_extension_name }}"].resource.as_blueprint()
    else:
        blueprint = Blueprint(
            '{{ current_model.flask_extension_name }}',
            __name__,
            url_prefix='/empty/{{ current_model.flask_extension_name }}')
    blueprint.record_once(init_{{ current_model.create_blueprint_from_app|base_name }})

    #calls record_once for all other functions starting with "init_addons_"
    #https://stackoverflow.com/questions/58785162/how-can-i-call-function-with-string-value-that-equals-to-function-name
    funcs = globals()
    funcs = [v for k, v in funcs.items() if k.startswith("init_addons_{{ current_model.extension_suffix }}") and callable(v)]
    for func in funcs:
        blueprint.record_once(func)

    return blueprint

def init_{{ current_model.create_blueprint_from_app|base_name }}(state):
    """Init app."""
    app = state.app
    ext = app.extensions["{{ current_model.flask_extension_name }}"]

    # register service
    sregistry = app.extensions["invenio-records-resources"].registry
    sregistry.register(ext.service, service_id="{{ current_model.service_id }}")

    # Register indexer
    if hasattr(ext.service, "indexer"):
        iregistry = app.extensions["invenio-indexer"].registry
        iregistry.register(ext.service.indexer, indexer_id="{{ current_model.flask_extension_name }}")



{{ current_model.invenio_views_extra_code }}