§
    ¥”jƒ  ã                   óÀ   — d Z ddlZddlmZ ddlmZ ddlmZ  G d„ d¦  «        Z	 G d„ d	e	¦  «        Z
 G d
„ de
¦  «        Z G d„ de
¦  «        Z G d„ de
¦  «        ZdS )z'
Provides various throttling policies.
é    N)Úcache)ÚImproperlyConfigured)Úapi_settingsc                   ó$   — e Zd ZdZd„ Zd„ Zd„ ZdS )ÚBaseThrottlez&
    Rate throttling of requests.
    c                 ó    — t          d¦  «        ‚)zT
        Return `True` if the request should be allowed, `False` otherwise.
        z#.allow_request() must be overridden©ÚNotImplementedError©ÚselfÚrequestÚviews      úh/var/www/finuniver-perm.ru/html/portfolio/venv/lib/python3.11/site-packages/rest_framework/throttling.pyÚallow_requestzBaseThrottle.allow_request   s   € õ "Ð"GÑHÔHÐHó    c                 óŠ  — |j                              d¦  «        }|j                              d¦  «        }t          j        }|�W|dk    s|€|S |                     d¦  «        }|t          |t          |¦  «        ¦  «                  }|                     ¦   «         S |r'd                     |                     ¦   «         ¦  «        n|S )zå
        Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR
        if present and number of proxies is > 0. If not use all of
        HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR.
        ÚHTTP_X_FORWARDED_FORÚREMOTE_ADDRNr   ú,Ú )	ÚMETAÚgetr   ÚNUM_PROXIESÚsplitÚminÚlenÚstripÚjoin)r   r   ÚxffÚremote_addrÚnum_proxiesÚaddrsÚclient_addrs          r   Ú	get_identzBaseThrottle.get_ident   s´   € ð Œl×ÒÐ5Ñ6Ô6ˆØ”l×&Ò& }Ñ5Ô5ˆÝ"Ô.ˆàÐ"Ø˜aÒÐ 3 ;Ø"Ð"Ø—I’I˜c‘N”NˆEØ¥ [µ#°e±*´*Ñ!=Ô!=Ð =Ô>ˆKØ×$Ò$Ñ&Ô&Ð&à'*Ð;ˆr�wŠw�s—y’y‘{”{Ñ#Ô#Ð#°Ð;r   c                 ó   — dS )zm
        Optionally, return a recommended number of seconds to wait before
        the next request.
        N© ©r   s    r   ÚwaitzBaseThrottle.wait*   s	   € ð
 ˆtr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r(   r&   r   r   r   r      sN   € € € € € ðð ðIð Ið Ið<ð <ð <ð&ð ð ð ð r   r   c                   ój   — e Zd ZdZeZej        ZdZdZ	e
j        Zd„ Zd„ Zd„ Zd„ Zd„ Zd	„ Zd
„ Zd„ ZdS )ÚSimpleRateThrottlea¡  
    A simple cache implementation, that only requires `.get_cache_key()`
    to be overridden.

    The rate (requests / seconds) is set by a `rate` attribute on the Throttle
    class.  The attribute is a string of the form 'number_of_requests/period'.

    Period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')

    Previous request information used for throttling is stored in the cache.
    zthrottle_%(scope)s_%(ident)sNc                 ó¨   — t          | dd ¦  «        s|                      ¦   «         | _        |                      | j        ¦  «        \  | _        | _        d S )NÚrate)ÚgetattrÚget_rater0   Ú
parse_rateÚnum_requestsÚdurationr'   s    r   Ú__init__zSimpleRateThrottle.__init__D   sF   € Ý�t˜V TÑ*Ô*ð 	(ØŸš™œˆDŒIØ+/¯?ª?¸4¼9Ñ+EÔ+EÑ(ˆÔ˜4œ=˜=˜=r   c                 ó    — t          d¦  «        ‚)z³
        Should return a unique cache-key which can be used for throttling.
        Must be overridden.

        May return `None` if the request should not be throttled.
        z#.get_cache_key() must be overriddenr	   r   s      r   Úget_cache_keyz SimpleRateThrottle.get_cache_keyI   s   € õ "Ð"GÑHÔHÐHr   c                 óÔ   — t          | dd¦  «        sd| j        j        z  }t          |¦  «        ‚	 | j        | j                 S # t          $ r d| j        z  }t          |¦  «        ‚w xY w)zR
        Determine the string representation of the allowed request rate.
        ÚscopeNz9You must set either `.scope` or `.rate` for '%s' throttlez+No default throttle rate set for '%s' scope)r1   Ú	__class__r)   r   ÚTHROTTLE_RATESr:   ÚKeyError)r   Úmsgs     r   r2   zSimpleRateThrottle.get_rateR   s‚   € õ �t˜W dÑ+Ô+ð 	,ØNØ”>Ô*ñ+ˆCå& sÑ+Ô+Ð+ð	,ØÔ& t¤zÔ2Ð2øÝð 	,ð 	,ð 	,Ø?À$Ä*ÑLˆCÝ& sÑ+Ô+Ð+ð	,øøøs   ±A Á$A'c                 ó†   — |€dS |                      d¦  «        \  }}t          |¦  «        }dddddœ|d	                  }||fS )
z‰
        Given the request rate string, return a two tuple of:
        <allowed number of requests>, <period of time in seconds>
        N)NNú/é   é<   i  i€Q )ÚsÚmÚhÚdr   )r   Úint)r   r0   ÚnumÚperiodr4   r5   s         r   r3   zSimpleRateThrottle.parse_ratea   sQ   € ð
 ˆ<Ø�<Ø—j’j ‘o”o‰ˆˆVÝ˜3‘x”xˆØ ¨$°UÐ;Ð;¸FÀ1¼IÔFˆØ˜hÐ'Ð'r   c                 ó(  — | j         €dS |                      ||¦  «        | _        | j        €dS | j                             | j        g ¦  «        | _        |                      ¦   «         | _        | j        r\| j        d         | j        | j        z
  k    r>| j         	                    ¦   «          | j        r| j        d         | j        | j        z
  k    °>t          | j        ¦  «        | j        k    r|                      ¦   «         S |                      ¦   «         S )z«
        Implement the check to see if the request should be throttled.

        On success calls `throttle_success`.
        On failure calls `throttle_failure`.
        NTéÿÿÿÿ)r0   r8   Úkeyr   r   ÚhistoryÚtimerÚnowr5   Úpopr   r4   Úthrottle_failureÚthrottle_successr   s      r   r   z SimpleRateThrottle.allow_requestm   sù   € ð Œ9ÐØ�4à×%Ò% g¨tÑ4Ô4ˆŒØŒ8ÐØ�4à”z—~’~ d¤h°Ñ3Ô3ˆŒØ—:’:‘<”<ˆŒð Œlð 	˜tœ|¨BÔ/°4´8¸d¼mÑ3KÒKÐKØŒL×ÒÑÔÐð Œlð 	˜tœ|¨BÔ/°4´8¸d¼mÑ3KÒKÐKåˆtŒ|ÑÔ Ô 1Ò1Ð1Ø×(Ò(Ñ*Ô*Ð*Ø×$Ò$Ñ&Ô&Ð&r   c                 óœ   — | j                              d| j        ¦  «         | j                             | j        | j         | j        ¦  «         dS )zd
        Inserts the current request's timestamp along with the key
        into the cache.
        r   T)rM   ÚinsertrO   r   ÚsetrL   r5   r'   s    r   rR   z#SimpleRateThrottle.throttle_success†   sA   € ð
 	Œ×Ò˜A˜tœxÑ(Ô(Ð(ØŒ
�Š�t”x ¤¨t¬}Ñ=Ô=Ð=Øˆtr   c                 ó   — dS )zP
        Called when a request to the API has failed due to throttling.
        Fr&   r'   s    r   rQ   z#SimpleRateThrottle.throttle_failure�   s	   € ð ˆur   c                 óÌ   — | j         r| j        | j        | j         d         z
  z
  }n| j        }| j        t	          | j         ¦  «        z
  dz   }|dk    rdS |t          |¦  «        z  S )zG
        Returns the recommended next request time in seconds.
        rK   rA   r   N)rM   r5   rO   r4   r   Úfloat)r   Úremaining_durationÚavailable_requestss      r   r(   zSimpleRateThrottle.wait•   su   € ð Œ<ð 	/Ø!%¤°$´(¸T¼\È"Ô=MÑ2MÑ!NÐÐà!%¤Ðà!Ô.µ°T´\Ñ1BÔ1BÑBÀQÑFÐØ Ò"Ð"Ø�4à!¥EÐ*<Ñ$=Ô$=Ñ=Ð=r   )r)   r*   r+   r,   Údefault_cacher   ÚtimerN   Úcache_formatr:   r   ÚDEFAULT_THROTTLE_RATESr<   r6   r8   r2   r3   r   rR   rQ   r(   r&   r   r   r.   r.   2   s¼   € € € € € ð
ð 
ð €EØŒI€EØ1€LØ€EØ!Ô8€NðFð Fð Fð
Ið Ið Ið,ð ,ð ,ð
(ð 
(ð 
(ð'ð 'ð 'ð2ð ð ðð ð ð>ð >ð >ð >ð >r   r.   c                   ó   — e Zd ZdZdZd„ ZdS )ÚAnonRateThrottlez–
    Limits the rate of API calls that may be made by a anonymous users.

    The IP address of the request will be used as the unique cache key.
    Úanonc                 óv   — |j         r|j         j        rd S | j        | j        |                      |¦  «        dœz  S ©N©r:   Úident)ÚuserÚis_authenticatedr]   r:   r$   r   s      r   r8   zAnonRateThrottle.get_cache_key­   sK   € ØŒ<ð 	˜GœLÔ9ð 	Ø�4àÔ Ø”ZØ—^’^ GÑ,Ô,ð$
ð $
ñ 
ð 	
r   N©r)   r*   r+   r,   r:   r8   r&   r   r   r`   r`   ¥   s4   € € € € € ðð ð
 €Eð
ð 
ð 
ð 
ð 
r   r`   c                   ó   — e Zd ZdZdZd„ ZdS )ÚUserRateThrottlezç
    Limits the rate of API calls that may be made by a given user.

    The user id will be used as a unique cache key if the user is
    authenticated.  For anonymous requests, the IP address of the request will
    be used.
    rf   c                 ó�   — |j         r|j         j        r|j         j        }n|                      |¦  «        }| j        | j        |dœz  S rc   ©rf   rg   Úpkr$   r]   r:   ©r   r   r   re   s       r   r8   zUserRateThrottle.get_cache_keyÁ   sV   € ØŒ<ð 	,˜GœLÔ9ð 	,Ø”L”OˆEˆEà—N’N 7Ñ+Ô+ˆEàÔ Ø”ZØð$
ð $
ñ 
ð 	
r   Nrh   r&   r   r   rj   rj   ·   s4   € € € € € ðð ð €Eð	
ð 	
ð 	
ð 	
ð 	
r   rj   c                   ó2   ‡ — e Zd ZdZdZd„ Zˆ fd„Zd„ Zˆ xZS )ÚScopedRateThrottlea,  
    Limits the rate of API calls by different amounts for various parts of
    the API.  Any view that has the `throttle_scope` property set will be
    throttled.  The unique cache key will be generated by concatenating the
    user id of the request, and the scope of the view being accessed.
    Úthrottle_scopec                 ó   — d S )Nr&   r'   s    r   r6   zScopedRateThrottle.__init__Ö   s	   € ð 	ˆr   c                 ó  •— t          || j        d ¦  «        | _        | j        sdS |                      ¦   «         | _        |                      | j        ¦  «        \  | _        | _        t          ¦   «          	                    ||¦  «        S )NT)
r1   Ú
scope_attrr:   r2   r0   r3   r4   r5   Úsuperr   )r   r   r   r;   s      €r   r   z ScopedRateThrottle.allow_requestÛ   ss   ø€ å˜T 4¤?°DÑ9Ô9ˆŒ
ð Œzð 	Ø�4ð —M’M‘O”OˆŒ	Ø+/¯?ª?¸4¼9Ñ+EÔ+EÑ(ˆÔ˜4œ=õ ‰wŒw×$Ò$ W¨dÑ3Ô3Ð3r   c                 ó�   — |j         r|j         j        r|j         j        }n|                      |¦  «        }| j        | j        |dœz  S )zØ
        If `view.throttle_scope` is not set, don't apply this throttle.

        Otherwise generate the unique cache key by concatenating the user id
        with the `.throttle_scope` property of the view.
        rd   rl   rn   s       r   r8   z ScopedRateThrottle.get_cache_keyë   sX   € ð Œ<ð 	,˜GœLÔ9ð 	,Ø”L”OˆEˆEà—N’N 7Ñ+Ô+ˆEàÔ Ø”ZØð$
ð $
ñ 
ð 	
r   )	r)   r*   r+   r,   rt   r6   r   r8   Ú__classcell__)r;   s   @r   rp   rp   Í   sg   ø€ € € € € ðð ð "€Jðð ð ð
4ð 4ð 4ð 4ð 4ð 
ð 
ð 
ð 
ð 
ð 
ð 
r   rp   )r,   r\   Údjango.core.cacher   r[   Údjango.core.exceptionsr   Úrest_framework.settingsr   r   r.   r`   rj   rp   r&   r   r   ú<module>r{      s7  ððð ð €€€à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7à 0Ð 0Ð 0Ð 0Ð 0Ð 0ð#ð #ð #ð #ð #ñ #ô #ð #ðLp>ð p>ð p>ð p>ð p>˜ñ p>ô p>ð p>ðf
ð 
ð 
ð 
ð 
Ð)ñ 
ô 
ð 
ð$
ð 
ð 
ð 
ð 
Ð)ñ 
ô 
ð 
ð,-
ð -
ð -
ð -
ð -
Ð+ñ -
ô -
ð -
ð -
ð -
r   