U
    Ç½„bä  ã                	   @   sÖ   d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d
dlmZ d
dlmZmZmZmZmZmZmZ eeefZeefeefeeefdœZdd„ Zddd„ZdS )é    N)ÚWRAPPER_ASSIGNMENTSÚwraps©Úadd_never_cache_headers)Ú
cache_page)Úvary_on_headers)Ú
exceptions)ÚResponse)Úapi_settings)ÚAPIViewé   )Úswagger_settings)ÚOpenAPIRendererÚReDocOldRendererÚReDocRendererÚSwaggerJSONRendererÚSwaggerUIRendererÚSwaggerYAMLRendererÚ_SpecRenderer)ÚswaggerZredocz	redoc-oldc                    s   t ˆ td�‡ fdd„ƒ}|S )zX
    Decorator that adds headers to a response so that it will
    never be cached.
    )Úassignedc                    s&   ˆ | f|ž|Ž}dd„ }|  |¡ |S )Nc                 S   s   t | ƒ | S ©Nr   )Úresponse© r   ú2/tmp/pip-unpacked-wheel-o6yr43pd/drf_yasg/views.pyÚcallback(   s    zBdeferred_never_cache.<locals>._wrapped_view_func.<locals>.callback)Zadd_post_render_callback)ÚrequestÚargsÚkwargsr   r   ©Ú	view_funcr   r   Ú_wrapped_view_func    s    
z0deferred_never_cache.<locals>._wrapped_view_func)r   r   )r    r!   r   r   r   Údeferred_never_cache   s    
r"   Fc	           
   
      s‚   |‰|pt j‰|‰ ˆ dkr tj‰ |‰ˆdkr2tj‰ˆp:t j‰ˆ	pBg ‰	t‡	fdd„tD ƒƒ‰G ‡ ‡‡‡‡‡‡‡‡f	dd„dtƒ}	|	S )aÃ  Create a SchemaView class with default renderers and generators.

    :param .Info info: information about the API; if omitted, defaults to :ref:`DEFAULT_INFO <default-swagger-settings>`
    :param str url: same as :class:`.OpenAPISchemaGenerator`
    :param patterns: same as :class:`.OpenAPISchemaGenerator`
    :param urlconf: same as :class:`.OpenAPISchemaGenerator`
    :param bool public: if False, includes only the endpoints that are accesible by the user viewing the schema
    :param list validators: a list of validator names to apply; the only allowed value is ``ssv``, for now
    :param type generator_class: schema generator class to use; should be a subclass of :class:`.OpenAPISchemaGenerator`
    :param tuple authentication_classes: authentication classes for the schema view itself
    :param tuple permission_classes: permission classes for the schema view itself
    :return: SchemaView class
    :rtype: type[drf_yasg.views.SchemaView]
    Nc                 3   s   | ]}|  ˆ ¡V  qd S r   )Zwith_validators)Ú.0Úrenderer)Ú
validatorsr   r   Ú	<genexpr>L   s     z"get_schema_view.<locals>.<genexpr>c                       sz   e Zd ZdZdZ”Z”Z” Z”Z”Z	d‡‡‡‡fdd„	Z
edd„ ƒZedd	d
„ƒZed‡fdd„	ƒZed‡fdd„	ƒZdS )z#get_schema_view.<locals>.SchemaViewTNÚ c                    sf   |j p|pd}t|jtƒr.|  ˆ |ˆˆˆ¡}n| jˆ |ˆg d�}| || j¡}|d kr^t ¡ ‚t	|ƒS )Nr'   )Úpatterns)
ÚversionÚ
isinstanceZaccepted_rendererr   Úgenerator_classZ
get_schemaÚpublicr   ZPermissionDeniedr	   )Úselfr   r)   ÚformatÚ	generatorÚschema)Úinfor(   ÚurlÚurlconfr   r   ÚgetW   s    z'get_schema_view.<locals>.SchemaView.getc                 S   s*   t ddƒ|ƒ}t|f|Ž|ƒ}t|ƒ}|S )z�Override this method to customize how caching is applied to the view.

            Arguments described in :meth:`.as_cached_view`.
            ÚCookieÚAuthorization)r   r   r"   )ÚclsÚviewÚcache_timeoutÚcache_kwargsr   r   r   Úapply_cachec   s    z/get_schema_view.<locals>.SchemaView.apply_cacher   c                 [   s>   |pi }| j f |Ž}|dkr,|  |||¡}n|r:t d¡ |S )aŸ  
            Calls .as_view() and wraps the result in a cache_page decorator.
            See https://docs.djangoproject.com/en/dev/topics/cache/

            :param int cache_timeout: same as cache_page; set to 0 for no cache
            :param dict cache_kwargs: dictionary of kwargs to be passed to cache_page
            :param initkwargs: kwargs for .as_view()
            :return: a view instance
            r   z:cache_kwargs ignored because cache_timeout is 0 (disabled))Zas_viewr;   ÚwarningsÚwarn)r7   r9   r:   Z
initkwargsr8   r   r   r   Úas_cached_viewn   s    
z2get_schema_view.<locals>.SchemaView.as_cached_viewc                    s   | j ||ˆ d�S )a‡  
            Instantiate this view with just JSON and YAML renderers, optionally wrapped with cache_page.
            See https://docs.djangoproject.com/en/dev/topics/cache/.

            :param int cache_timeout: same as cache_page; set to 0 for no cache
            :param dict cache_kwargs: dictionary of kwargs to be passed to cache_page
            :return: a view instance
            ©Úrenderer_classes)r>   )r7   r9   r:   ©Ú_spec_renderersr   r   Ú
without_ui�   s    
z.get_schema_view.<locals>.SchemaView.without_uir   c                    s6   |t kstdd t ¡ ƒ‚t | ˆ  }| j|||d�S )aÔ  
            Instantiate this view with a Web UI renderer, optionally wrapped with cache_page.
            See https://docs.djangoproject.com/en/dev/topics/cache/.

            :param str renderer: UI renderer; allowed values are ``swagger``, ``redoc``
            :param int cache_timeout: same as cache_page; set to 0 for no cache
            :param dict cache_kwargs: dictionary of kwargs to be passed to cache_page
            :return: a view instance
            z supported default renderers are z, r?   )ÚUI_RENDERERSÚAssertionErrorÚjoinr>   )r7   r$   r9   r:   r@   rA   r   r   Úwith_ui�   s    z+get_schema_view.<locals>.SchemaView.with_ui)r'   N)r   N)r   N)r   r   N)Ú__name__Ú
__module__Ú__qualname__Z_ignore_model_permissionsr0   r,   r+   Úauthentication_classesÚpermission_classesr@   r4   Úclassmethodr;   r>   rC   rG   r   )	Ú_auth_classesÚ_generator_classÚ_perm_classesÚ_publicrB   r1   r(   r2   r3   r   r   Ú
SchemaViewN   s    

rR   )	r   ZDEFAULT_GENERATOR_CLASSr
   ZDEFAULT_AUTHENTICATION_CLASSESZDEFAULT_PERMISSION_CLASSESZDEFAULT_INFOÚtupleÚSPEC_RENDERERSr   )
r1   r2   r(   r3   r,   r%   r+   rK   rL   rR   r   )
rN   rO   rP   rQ   rB   r1   r(   r2   r3   r%   r   Úget_schema_view2   s    

$OrU   )	NNNNFNNNN) r<   Ú	functoolsr   r   Zdjango.utils.cacher   Zdjango.views.decorators.cacher   Zdjango.views.decorators.varyr   Zrest_frameworkr   Zrest_framework.responser	   Zrest_framework.settingsr
   Zrest_framework.viewsr   Zapp_settingsr   Z	renderersr   r   r   r   r   r   r   rT   rD   r"   rU   r   r   r   r   Ú<module>   s*   $
ý      ÿ