
    Evg'!                         d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZ  G d d	e          Z G d
 de          Z G d dee          ZdS )a  
Cache middleware. If enabled, each Django-powered page will be cached based on
URL. The canonical way to enable cache middleware is to set
``UpdateCacheMiddleware`` as your first piece of middleware, and
``FetchFromCacheMiddleware`` as the last::

    MIDDLEWARE = [
        'django.middleware.cache.UpdateCacheMiddleware',
        ...
        'django.middleware.cache.FetchFromCacheMiddleware'
    ]

This is counterintuitive, but correct: ``UpdateCacheMiddleware`` needs to run
last during the response phase, which processes middleware bottom-up;
``FetchFromCacheMiddleware`` needs to run last during the request phase, which
processes middleware top-down.

The single-class ``CacheMiddleware`` can be used for some simple sites.
However, if any other piece of middleware needs to affect the cache key, you'll
need to use the two-part ``UpdateCacheMiddleware`` and
``FetchFromCacheMiddleware``. This'll most often happen when you're using
Django's ``LocaleMiddleware``.

More details about how the caching works:

* Only GET or HEAD-requests with status code 200 are cached.

* The number of seconds each page is stored for is set by the "max-age" section
  of the response's "Cache-Control" header, falling back to the
  CACHE_MIDDLEWARE_SECONDS setting if the section was not found.

* This middleware expects that a HEAD request is answered with the same response
  headers exactly like the corresponding GET request.

* When a hit occurs, a shallow copy of the original response object is returned
  from process_request.

* Pages will be cached based on the contents of the request headers listed in
  the response's "Vary" header.

* This middleware also sets ETag, Last-Modified, Expires and Cache-Control
  headers on the response object.

    N)settings)DEFAULT_CACHE_ALIAScaches)get_cache_keyget_max_agehas_vary_headerlearn_cache_keypatch_response_headers)MiddlewareMixin)parse_http_date_safec                   D     e Zd ZdZ fdZed             Zd Zd Z xZ	S )UpdateCacheMiddlewarea6  
    Response-phase cache middleware that updates the cache if the response is
    cacheable.

    Must be used as part of the two-part update/fetch cache middleware.
    UpdateCacheMiddleware must be the first piece of middleware in MIDDLEWARE
    so that it'll get called last during the response phase.
    c                     t                                          |           t          j        | _        d | _        t          j        | _        t          j        | _	        d S N)
super__init__r   CACHE_MIDDLEWARE_SECONDScache_timeoutpage_timeoutCACHE_MIDDLEWARE_KEY_PREFIX
key_prefixCACHE_MIDDLEWARE_ALIAScache_aliasselfget_response	__class__s     T/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/middleware/cache.pyr   zUpdateCacheMiddleware.__init__G   sI    &&&%> ">#:    c                 &    t           | j                 S r   r   r   r   s    r   cachezUpdateCacheMiddleware.cacheN       d&''r   c                 0    t          |d          o|j        S )N_cache_update_cache)hasattrr&   )r   requestresponses      r   _should_update_cachez*UpdateCacheMiddleware._should_update_cacheR   s    w 566V7;VVr   c                 T                          ||          s|S |j        s	|j        dvr|S |j        s|j        rt          |d          r|S d|                    dd          v r|S  j        !t          |           j	        ndk    r|S t          |           r|j        dk    ryt          || j         j        	          t          |d
          r/t          |j                  r|                     fd           n j                            |           |S )zSet the cache, if needed.)   i0  CookieprivatezCache-Control Nr   r,   r#   renderc                 <    j                             |           S r   )r#   set)r	cache_keyr   timeouts    r   <lambda>z8UpdateCacheMiddleware.process_response.<locals>.<lambda>~   s    djnnY7CC r   )r*   	streamingstatus_codeCOOKIEScookiesr   getr   r   r   r
   r	   r   r#   r'   callabler1   add_post_render_callbackr3   )r   r(   r)   r5   r6   s   `  @@r   process_responsez&UpdateCacheMiddleware.process_responseU   s   (((;; 	O 	!5Z!G!GO
 	 	  (33	
 O _b9999O #? "(++G,Ax111 		=x+s22'7DO4:  I x** =x/H/H =11CCCCCC    
y(G<<<r   )
__name__
__module____qualname____doc__r   propertyr#   r*   r?   __classcell__r   s   @r   r   r   =   s~         ; ; ; ; ; ( ( X(W W W- - - - - - -r   r   c                   >     e Zd ZdZ fdZed             Zd Z xZS )FetchFromCacheMiddlewarea!  
    Request-phase cache middleware that fetches a page from the cache.

    Must be used as part of the two-part update/fetch cache middleware.
    FetchFromCacheMiddleware must be the last piece of middleware in MIDDLEWARE
    so that it'll get called last during the request phase.
    c                     t                                          |           t          j        | _        t          j        | _        d S r   )r   r   r   r   r   r   r   r   s     r   r   z!FetchFromCacheMiddleware.__init__   s6    &&&">#:r   c                 &    t           | j                 S r   r!   r"   s    r   r#   zFetchFromCacheMiddleware.cache   r$   r   c                 "   |j         dvr	d|_        dS t          || j        d| j                  }|	d|_        dS | j                            |          }|B|j         dk    r7t          || j        d| j                  }| j                            |          }|	d|_        dS t          |          x}St          |d                   x}	 ;t          t          j	                              }||z
  }t          d	||z
            |d
<   d|_        |S )zn
        Check whether the page is already cached and return the cached
        version if available.
        )GETHEADFNrL   r0   TrM   Expiresr   Age)methodr&   r   r   r#   r<   r   r   inttimemax)r   r(   r5   r)   max_age_secondsexpires_timestampnow_timestampremaining_secondss           r   process_requestz(FetchFromCacheMiddleware.process_request   s7   
 >00*/G'4 "'4?ETTT	*.G'4:>>),,& 8 8%&
  I z~~i00H*.G'4  +8444OA!5hy6I!J!JJG  	,,M 1M A!!_7H%HIIHUO ',#r   )	r@   rA   rB   rC   r   rD   r#   rX   rE   rF   s   @r   rH   rH      sl         ; ; ; ; ;
 ( ( X(% % % % % % %r   rH   c                   $     e Zd ZdZd fd	Z xZS )CacheMiddlewarez
    Cache middleware that provides basic behavior for many simple sites.

    Also used as the hook point for the cache decorator, which is generated
    using the decorator-from-middleware utility.
    Nc                    t                                          |           	 |d         }|d}|| _        n# t          $ r Y nw xY w	 |d         }|t          }|| _        n# t          $ r Y nw xY w||| _        || _        d S )Nr    r   )r   r   r   KeyErrorr   r   r   r   )r   r   r   r   kwargsr   r   r   s          r   r   zCacheMiddleware.__init__   s    &&&	-J!
(DOO 	 	 	D		 /K"1*D 	 	 	D	 $!.D(s!   8 
AA	A" "
A/.A/)NN)r@   rA   rB   rC   r   rE   rF   s   @r   rZ   rZ      sG         ) ) ) ) ) ) ) ) ) )r   rZ   )rC   rR   django.confr   django.core.cacher   r   django.utils.cacher   r   r   r	   r
   django.utils.deprecationr   django.utils.httpr   r   rH   rZ   r/   r   r   <module>rd      sG  + +Z              9 9 9 9 9 9 9 9              5 4 4 4 4 4 2 2 2 2 2 2E E E E EO E E EP7 7 7 7 7 7 7 7t )  )  )  )  )+-E  )  )  )  )  )r   