U )._S@sRdZddlmZddlmZddlmZeZGdddeZGdd d eZ d S) z flask.blueprints ~~~~~~~~~~~~~~~~ Blueprints are the recommended way to implement larger or more pluggable applications in Flask 0.7 and later. :copyright: 2010 Pallets :license: BSD-3-Clause )update_wrapper)_endpoint_from_view_func)_PackageBoundObjectc@s"eZdZdZddZdddZdS)BlueprintSetupStatezTemporary holder object for registering a blueprint with the application. An instance of this class is created by the :meth:`~flask.Blueprint.make_setup_state` method and later passed to all register callback functions. cCs||_||_||_||_|jd}|dkr4|jj}||_|jd}|dkrV|jj}||_t|jj|_ |j |jdddS)N subdomain url_prefix url_defaults) app blueprintoptionsfirst_registrationgetrrdicturl_values_defaultsr update)selfr r r rrrr r C:\Users\Miouzora\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask/blueprints.py__init__s  zBlueprintSetupState.__init__NcKs|jdk r2|r,d|jd|df}n|j}|d|j|dkrPt|}|j}d|krpt|f| d}|j j |d|j j |f|fd|i|dS)zA helper method to register a rule (and optionally a view function) to the application. The endpoint is automatically prefixed with the blueprint's name. N/rdefaultsz%s.%s)rjoinrstriplstrip setdefaultrrr rpopr add_url_ruler name)rruleendpoint view_funcr rr r rrAs& z BlueprintSetupState.add_url_rule)NN)__name__ __module__ __qualname____doc__rrr r r rrs%rc@s@eZdZdZdZdZdZdZdZdZ dZ ddddddde fddZ ddZ dd Zd@d d ZdAd d ZddZdBddZddZdCddZdDddZdEddZdFddZdGddZdHddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Z d.d/Z!d0d1Z"d2d3Z#d4d5Z$d6d7Z%d8d9Z&d:d;Z'dd?Z)dS)I Blueprinta Represents a blueprint, a collection of routes and other app-related functions that can be registered on a real application later. A blueprint is an object that allows defining application functions without requiring an application object ahead of time. It uses the same decorators as :class:`~flask.Flask`, but defers the need for an application by recording them for later registration. Decorating a function with a blueprint creates a deferred function that is called with :class:`~flask.blueprints.BlueprintSetupState` when the blueprint is registered on an application. See :ref:`blueprints` for more information. .. versionchanged:: 1.1.0 Blueprints have a ``cli`` group to register nested CLI commands. The ``cli_group`` parameter controls the name of the group under the ``flask`` command. .. versionadded:: 0.7 :param name: The name of the blueprint. Will be prepended to each endpoint name. :param import_name: The name of the blueprint package, usually ``__name__``. This helps locate the ``root_path`` for the blueprint. :param static_folder: A folder with static files that should be served by the blueprint's static route. The path is relative to the blueprint's root path. Blueprint static files are disabled by default. :param static_url_path: The url to serve static files from. Defaults to ``static_folder``. If the blueprint does not have a ``url_prefix``, the app's static route will take precedence, and the blueprint's static files won't be accessible. :param template_folder: A folder with templates that should be added to the app's template search path. The path is relative to the blueprint's root path. Blueprint templates are disabled by default. Blueprint templates have a lower precedence than those in the app's templates folder. :param url_prefix: A path to prepend to all of the blueprint's URLs, to make them distinct from the rest of the app's routes. :param subdomain: A subdomain that blueprint routes will match on by default. :param url_defaults: A dict of default values that blueprint routes will receive by default. :param root_path: By default, the blueprint will automatically this based on ``import_name``. In certain situations this automatic detection can fail, so the path can be specified manually instead. FNc CsRtj|||| d||_||_||_||_||_g|_|dkrBi}||_| |_ dS)N) root_path) rrrrr static_folderstatic_url_pathdeferred_functionsr cli_group) rr import_namer(r)template_folderrrr r'r+r r rrs zBlueprint.__init__cCs4|jr$|jr$ddlm}|td|j|dS)zRegisters a function that is called when the blueprint is registered on the application. This function is called with the state as argument as returned by the :meth:`make_setup_state` method. r)warnzgThe blueprint was already registered once but is getting modified now. These changes will not show up.N)_got_registered_oncewarn_on_modificationswarningsr.Warningr*append)rfuncr.r r rrecords  zBlueprint.recordcsfdd}|t|S)zWorks like :meth:`record` but wraps the function in another function that will ensure the function is only called once. If the blueprint is registered a second time on the application, the function passed is not called. cs|jr|dSN)rstater4r rwrappersz&Blueprint.record_once..wrapper)r5r)rr4r:r r9r record_onces zBlueprint.record_oncecCst||||S)zCreates an instance of :meth:`~flask.blueprints.BlueprintSetupState` object that is later passed to the register callback functions. Subclasses can override this to return a subclass of the setup state. )r)rr r rr r rmake_setup_stateszBlueprint.make_setup_statecCsd|_||||}|jr2|j|jd|jdd|jD] }||q8|d|j}|j j s`dS|dkr||j j |j j n8|t kr|j |j _ |j |j n||j _ |j |j dS)a4Called by :meth:`Flask.register_blueprint` to register all views and callbacks registered on the blueprint with the application. Creates a :class:`.BlueprintSetupState` and calls each :meth:`record` callback with it. :param app: The application this blueprint is being registered with. :param options: Keyword arguments forwarded from :meth:`~Flask.register_blueprint`. :param first_registration: Whether this is the first time this blueprint has been registered on the application. Tz/Zstatic)r!r r+N)r/r<Zhas_static_folderrr)Zsend_static_filer*rr+clicommandsr _sentinelrZ add_command)rr r rr8deferredZcli_resolved_groupr r rregisters(    zBlueprint.registerc sfdd}|S)zLike :meth:`Flask.route` but for a blueprint. The endpoint for the :func:`url_for` function is prefixed with the name of the blueprint. cs$d|j}j||f|S)Nr )rr"r)fr r rrr r decoratorsz"Blueprint.route..decoratorr )rrr rDr rCrrouteszBlueprint.routec sPrdkstdr4tdr4djks4td|fdddS)zLike :meth:`Flask.add_url_rule` but for a blueprint. The endpoint for the :func:`url_for` function is prefixed with the name of the blueprint. .z+Blueprint endpoints should not contain dotsr"z4Blueprint view function name should not contain dotscs|jfSr6)rsr r rr!r r&z(Blueprint.add_url_rule..N)AssertionErrorhasattrr"r5)rrr r!r r rIrrszBlueprint.add_url_rulecsfdd}|S)a\Like :meth:`Flask.endpoint` but for a blueprint. This does not prefix the endpoint with the blueprint name, this has to be done explicitly by the user of this method. If the endpoint is prefixed with a `.` it will be registered to the current blueprint, otherwise it's an application independent endpoint. csfdd}|S)Ncs|jj<dSr6)r Zview_functionsr7)r rBr rregister_endpoint1sz@Blueprint.endpoint..decorator..register_endpointr;)rBrNr rrBrrD0s z%Blueprint.endpoint..decoratorr )rr rDr rPrr (szBlueprint.endpointcsfdd}|S)zRegister a custom template filter, available application wide. Like :meth:`Flask.template_filter` but for a blueprint. :param name: the optional name of the filter, otherwise the function name will be used. csj|d|SN)r)add_app_template_filterrQrrr rrDAsz0Blueprint.app_template_filter..decoratorr rrrDr rTrapp_template_filter9szBlueprint.app_template_filtercsfdd}||dS)aIRegister a custom template filter, available application wide. Like :meth:`Flask.add_template_filter` but for a blueprint. Works exactly like the :meth:`app_template_filter` decorator. :param name: the optional name of the filter, otherwise the function name will be used. cs|jjjpj<dSr6)r jinja_envfiltersr"r7rBrr rregister_templatePsz.register_templateNrOrrBrrZr rYrrSGs z!Blueprint.add_app_template_filtercsfdd}|S)aRegister a custom template test, available application wide. Like :meth:`Flask.template_test` but for a blueprint. .. versionadded:: 0.10 :param name: the optional name of the test, otherwise the function name will be used. csj|d|SrR)add_app_template_testrQrTr rrD_sz.Blueprint.app_template_test..decoratorr rUr rTrapp_template_testUs zBlueprint.app_template_testcsfdd}||dS)aaRegister a custom template test, available application wide. Like :meth:`Flask.add_template_test` but for a blueprint. Works exactly like the :meth:`app_template_test` decorator. .. versionadded:: 0.10 :param name: the optional name of the test, otherwise the function name will be used. cs|jjjpj<dSr6)r rWtestsr"r7rYr rrZpsz:Blueprint.add_app_template_test..register_templateNrOr[r rYrr\es zBlueprint.add_app_template_testcsfdd}|S)aRegister a custom template global, available application wide. Like :meth:`Flask.template_global` but for a blueprint. .. versionadded:: 0.10 :param name: the optional name of the global, otherwise the function name will be used. csj|d|SrR)add_app_template_globalrQrTr rrDsz0Blueprint.app_template_global..decoratorr rUr rTrapp_template_globalus zBlueprint.app_template_globalcsfdd}||dS)aiRegister a custom template global, available application wide. Like :meth:`Flask.add_template_global` but for a blueprint. Works exactly like the :meth:`app_template_global` decorator. .. versionadded:: 0.10 :param name: the optional name of the global, otherwise the function name will be used. cs|jjjpj<dSr6)r rWglobalsr"r7rYr rrZsz.register_templateNrOr[r rYrr_s z!Blueprint.add_app_template_globalcsfddS)zLike :meth:`Flask.before_request` but for a blueprint. This function is only executed before each request that is handled by a function of that blueprint. cs|jjjgSr6)r before_request_funcsrrr3rGrBrr rrJrKz*Blueprint.before_request..rOrrBr rcrbefore_requests zBlueprint.before_requestcs|fddS)zLike :meth:`Flask.before_request`. Such a function is executed before each request, even if outside of a blueprint. cs|jjdgSr6)r rbrr3rGrQr rrJrKz.Blueprint.before_app_request..rOrdr rQrbefore_app_requests zBlueprint.before_app_requestcs|fddS)zLike :meth:`Flask.before_first_request`. Such a function is executed before the first request to the application. cs|jjSr6)r Zbefore_first_request_funcsr3rGrQr rrJrKz4Blueprint.before_app_first_request..rOrdr rQrbefore_app_first_requestsz"Blueprint.before_app_first_requestcsfddS)zLike :meth:`Flask.after_request` but for a blueprint. This function is only executed after each request that is handled by a function of that blueprint. cs|jjjgSr6)r after_request_funcsrrr3rGrcr rrJrKz)Blueprint.after_request..rOrdr rcr after_requests zBlueprint.after_requestcs|fddS)zLike :meth:`Flask.after_request` but for a blueprint. Such a function is executed after each request, even if outside of the blueprint. cs|jjdgSr6)r rhrr3rGrQr rrJrKz-Blueprint.after_app_request..rOrdr rQrafter_app_requests zBlueprint.after_app_requestcsfddS)a=Like :meth:`Flask.teardown_request` but for a blueprint. This function is only executed when tearing down requests handled by a function of that blueprint. Teardown request functions are executed when the request context is popped, even when no actual request was performed. cs|jjjgSr6)r teardown_request_funcsrrr3rGrcr rrJrKz,Blueprint.teardown_request..rOrdr rcrteardown_requests zBlueprint.teardown_requestcs|fddS)zLike :meth:`Flask.teardown_request` but for a blueprint. Such a function is executed when tearing down each request, even if outside of the blueprint. cs|jjdgSr6)r rkrr3rGrQr rrJrKz0Blueprint.teardown_app_request..rOrdr rQrteardown_app_requests zBlueprint.teardown_app_requestcsfddS)zLike :meth:`Flask.context_processor` but for a blueprint. This function is only executed for requests handled by a blueprint. cs|jjjgSr6)r template_context_processorsrrr3rGrcr rrJs z-Blueprint.context_processor..rOrdr rcrcontext_processors zBlueprint.context_processorcs|fddS)zLike :meth:`Flask.context_processor` but for a blueprint. Such a function is executed each request, even if outside of the blueprint. cs|jjdgSr6)r rnrr3rGrQr rrJrKz1Blueprint.app_context_processor..rOrdr rQrapp_context_processors zBlueprint.app_context_processorcsfdd}|S)zLike :meth:`Flask.errorhandler` but for a blueprint. This handler is used for all requests, even if outside of the blueprint. csfddS)Ncs|jSr6)r errorhandlerrG)coderBr rrJrKz?Blueprint.app_errorhandler..decorator..rOrQrrrrQrrDsz-Blueprint.app_errorhandler..decoratorr )rrrrDr rsrapp_errorhandlerszBlueprint.app_errorhandlercsfddS)zRegisters a function as URL value preprocessor for this blueprint. It's called before the view functions are called and can modify the url values provided. cs|jjjgSr6)r url_value_preprocessorsrrr3rGrcr rrJrKz2Blueprint.url_value_preprocessor..rOrdr rcrurl_value_preprocessors z Blueprint.url_value_preprocessorcsfddS)zCallback function for URL defaults for this blueprint. It's called with the endpoint and values and should update the values passed in place. cs|jjjgSr6)r url_default_functionsrrr3rGrcr rrJrKz(Blueprint.url_defaults..rOrdr rcrr s zBlueprint.url_defaultscs|fddS)zESame as :meth:`url_value_preprocessor` but application wide. cs|jjdgSr6)r rurr3rGrQr rrJrKz6Blueprint.app_url_value_preprocessor..rOrdr rQrapp_url_value_preprocessor s z$Blueprint.app_url_value_preprocessorcs|fddS)z;Same as :meth:`url_defaults` but application wide. cs|jjdgSr6)r rwrr3rGrQr rrJrKz,Blueprint.app_url_defaults..rOrdr rQrapp_url_defaultss zBlueprint.app_url_defaultscsfdd}|S)a Registers an error handler that becomes active for this blueprint only. Please be aware that routing does not happen local to a blueprint so an error handler for 404 usually is not handled by a blueprint unless it is caused inside a view function. Another special case is the 500 internal server error which is always looked up from the application. Otherwise works as the :meth:`~flask.Flask.errorhandler` decorator of the :class:`~flask.Flask` object. csfddS)Ncs|jjSr6r Z_register_error_handlerrrGcode_or_exceptionrBrr rrJ)rKz;Blueprint.errorhandler..decorator..rOrQr|rrQrrD'sz)Blueprint.errorhandler..decoratorr )rr|rDr r}rrqs zBlueprint.errorhandlercsfdddS)a1Non-decorator version of the :meth:`errorhandler` error attach function, akin to the :meth:`~flask.Flask.register_error_handler` application-wide function of the :class:`~flask.Flask` object but for error handlers limited to this blueprint. .. versionadded:: 0.11 cs|jjSr6rzrGr{r rrJ8rKz2Blueprint.register_error_handler..NrO)rr|rBr r{rregister_error_handler/sz Blueprint.register_error_handler)F)F)NN)N)N)N)N)N)N)*r"r#r$r%r0r/Z json_encoderZ json_decoderr,r-r'r?rr5r;r<rArErr rVrSr]r\r`r_rerfrgrirjrlrmrorprtrvr rxryrqr~r r r rr&Zs\4   '                  r&N) r% functoolsrZhelpersrrobjectr?rr&r r r rs    E