ó
    |chƒ  ã                   ó    • S r SSKrSSKJr  SSKJr  SSKJr   " S S5      r	 " S S	\	5      r
 " S
 S\
5      r " S S\
5      r " S S\
5      rg)z'
Provides various throttling policies.
é    N)Úcache)ÚImproperlyConfigured)Úapi_settingsc                   ó*   • \ rS rSrSrS rS rS rSrg)ÚBaseThrottleé   z
Rate throttling of requests.
c                 ó   • [        S5      e)zD
Return `True` if the request should be allowed, `False` otherwise.
z#.allow_request() must be overridden©ÚNotImplementedError©ÚselfÚrequestÚviews      Úb/var/www/djangovue.mamus.xyz/django/venv/lib/python3.13/site-packages/rest_framework/throttling.pyÚallow_requestÚBaseThrottle.allow_request   s   € ô "Ð"GÓHÐHó    c                 ón  • UR                   R                  S5      nUR                   R                  S5      n[        R                  nUbE  US:X  d  Uc  U$ UR	                  S5      nU[        U[        U5      5      *    nUR                  5       $ U(       a  SR                  UR	                  5       5      $ U$ )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_ADDRr   Ú,Ú )	ÚMETAÚgetr   ÚNUM_PROXIESÚsplitÚminÚlenÚstripÚjoin)r   r   ÚxffÚremote_addrÚnum_proxiesÚaddrsÚclient_addrs          r   Ú	get_identÚBaseThrottle.get_ident   s    € ð �l‰l×ÑÐ5Ó6ˆØ—l‘l×&Ñ& }Ó5ˆÜ"×.Ñ.ˆàÑ"Ø˜aÓ 3¡;Ø"Ð"Ø—I‘I˜c“NˆEØ¤ [´#°e³*Ó!=Ð =Ñ>ˆKØ×$Ñ$Ó&Ð&æ'*ˆr�w‰w�s—y‘y“{Ó#Ð;°Ð;r   c                 ó   • g)zU
Optionally, return a recommended number of seconds to wait before
the next request.
N© ©r   s    r   ÚwaitÚBaseThrottle.wait*   s   € ð
 r   r)   N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r&   r+   Ú__static_attributes__r)   r   r   r   r      s   † ñòIò<õ&r   r   c                   ó„   • \ rS rSrSr\r\R                  rSr	Sr
\R                  rS rS rS rS rS	 rS
 rS rS rSrg)ÚSimpleRateThrottleé2   a…  
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                 ó¢   • [        U SS 5      (       d  U R                  5       U l        U R                  U R                  5      u  U l        U l        g )NÚrate)ÚgetattrÚget_rater7   Ú
parse_rateÚnum_requestsÚdurationr*   s    r   Ú__init__ÚSimpleRateThrottle.__init__D   s:   € Ü�t˜V T×*Ñ*ØŸ™›ˆDŒIØ+/¯?©?¸4¿9¹9Ó+EÑ(ˆÔ˜4�=r   c                 ó   • [        S5      e)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_keyÚ SimpleRateThrottle.get_cache_keyI   s   € ô "Ð"GÓHÐHr   c                 óò   • [        U SS5      (       d$  SU R                  R                  -  n[        U5      e U R                  U R
                     $ ! [         a    SU R
                  -  n[        U5      ef = f)zB
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)r8   Ú	__class__r-   r   ÚTHROTTLE_RATESrC   ÚKeyError)r   Úmsgs     r   r9   ÚSimpleRateThrottle.get_rateR   sw   € ô �t˜W d×+Ñ+ØNØ—>‘>×*Ñ*ñ+ˆCä& sÓ+Ð+ð	,Ø×&Ñ& t§z¡zÑ2Ð2øÜó 	,Ø?À$Ç*Á*ÑLˆCÜ& sÓ+Ð+ð	,ús   ¸A Á%A6c                 óf   • Uc  gUR                  S5      u  p#[        U5      nSSSSS.US      nXE4$ )	zq
Given the request rate string, return a two tuple of:
<allowed number of requests>, <period of time in seconds>
)NNÚ/é   é<   i  i€Q )ÚsÚmÚhÚdr   )r   Úint)r   r7   ÚnumÚperiodr;   r<   s         r   r:   ÚSimpleRateThrottle.parse_ratea   sD   € ð
 ‰<ØØ—j‘j “o‰ˆÜ˜3“xˆØ ¨$°UÑ;¸FÀ1¹IÑFˆØÐ'Ð'r   c                 ó�  • U R                   c  gU R                  X5      U l        U R                  c  gU R                  R	                  U R                  / 5      U l        U R                  5       U l        U R
                  (       a�  U R
                  S   U R                  U R                  -
  ::  aW  U R
                  R                  5         U R
                  (       a,  U R
                  S   U R                  U R                  -
  ::  a  MW  [        U R
                  5      U R                  :¼  a  U R                  5       $ U R                  5       $ )z‹
Implement the check to see if the request should be throttled.

On success calls `throttle_success`.
On failure calls `throttle_failure`.
Téÿÿÿÿ)r7   r@   Úkeyr   r   ÚhistoryÚtimerÚnowr<   Úpopr   r;   Úthrottle_failureÚthrottle_successr   s      r   r   Ú SimpleRateThrottle.allow_requestm   sè   € ð �9‰9ÑØà×%Ñ% gÓ4ˆŒØ�8‰8ÑØà—z‘z—~‘~ d§h¡h°Ó3ˆŒØ—:‘:“<ˆŒð �l�l˜tŸ|™|¨BÑ/°4·8±8¸d¿m¹mÑ3KÓKØ�L‰L×ÑÔð �l�l˜tŸ|™|¨BÑ/°4·8±8¸d¿m¹mÑ3KÕKäˆt�|‰|Ó × 1Ñ 1Ó1Ø×(Ñ(Ó*Ð*Ø×$Ñ$Ó&Ð&r   c                 óÆ   • U R                   R                  SU R                  5        U R                  R	                  U R
                  U R                   U R                  5        g)zL
Inserts the current request's timestamp along with the key
into the cache.
r   T)rX   ÚinsertrZ   r   ÚsetrW   r<   r*   s    r   r]   Ú#SimpleRateThrottle.throttle_success†   s?   € ð
 	�‰×Ñ˜A˜tŸx™xÔ(Ø�
‰
�‰�t—x‘x §¡¨t¯}©}Ô=Ør   c                 ó   • g)z@
Called when a request to the API has failed due to throttling.
Fr)   r*   s    r   r\   Ú#SimpleRateThrottle.throttle_failure�   s   € ð r   c                 ó  • U R                   (       a*  U R                  U R                  U R                   S   -
  -
  nOU R                  nU R                  [	        U R                   5      -
  S-   nUS::  a  gU[        U5      -  $ )z7
Returns the recommended next request time in seconds.
rV   rK   r   N)rX   r<   rZ   r;   r   Úfloat)r   Úremaining_durationÚavailable_requestss      r   r+   ÚSimpleRateThrottle.wait•   sq   € ð �<�<Ø!%§¡°$·(±(¸T¿\¹\È"Ñ=MÑ2MÑ!NÑà!%§¡Ðà!×.Ñ.´°T·\±\Ó1BÑBÀQÑFÐØ Ó"Øà!¤EÐ*<Ó$=Ñ=Ð=r   )r<   rX   rW   rZ   r;   r7   )r-   r.   r/   r0   r1   Údefault_cacher   ÚtimerY   Úcache_formatrC   r   ÚDEFAULT_THROTTLE_RATESrE   r=   r@   r9   r:   r   r]   r\   r+   r2   r)   r   r   r4   r4   2   sV   † ñ
ð €EØ�I‰I€EØ1€LØ€EØ!×8Ñ8€NòFò
Iò,ò
(ò'ò2òõ>r   r4   c                   ó"   • \ rS rSrSrSrS rSrg)ÚAnonRateThrottleé¥   zŠ
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                 ó²   • UR                   (       a  UR                   R                  (       a  g U R                  U R                  U R	                  U5      S.-  $ ©N©rC   Úident)ÚuserÚis_authenticatedrl   rC   r&   r   s      r   r@   ÚAnonRateThrottle.get_cache_key­   sC   € Ø�<�<˜GŸL™L×9×9Øà× Ñ Ø—Z‘ZØ—^‘^ GÓ,ñ$
ñ 
ð 	
r   r)   N©r-   r.   r/   r0   r1   rC   r@   r2   r)   r   r   ro   ro   ¥   s   † ñð
 €Eõ
r   ro   c                   ó"   • \ rS rSrSrSrS rSrg)ÚUserRateThrottleé·   zÓ
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.
rv   c                 óâ   • UR                   (       a2  UR                   R                  (       a  UR                   R                  nOU R                  U5      nU R                  U R
                  US.-  $ rs   ©rv   rw   Úpkr&   rl   rC   ©r   r   r   ru   s       r   r@   ÚUserRateThrottle.get_cache_keyÁ   sR   € Ø�<�<˜GŸL™L×9×9Ø—L‘L—O‘O‰Eà—N‘N 7Ó+ˆEà× Ñ Ø—Z‘ZØñ$
ñ 
ð 	
r   r)   Nry   r)   r   r   r{   r{   ·   s   † ñð €Eõ	
r   r{   c                   ó<   ^ • \ rS rSrSrSrS rU 4S jrS rSr	U =r
$ )ÚScopedRateThrottleéÍ   a  
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                 ó   • g )Nr)   r*   s    r   r=   ÚScopedRateThrottle.__init__Ö   s   € ð 	r   c                 óö   >• [        X R                  S 5      U l        U R                  (       d  gU R                  5       U l        U R                  U R                  5      u  U l        U l        [        TU ]%  X5      $ )NT)
r8   Ú
scope_attrrC   r9   r7   r:   r;   r<   Úsuperr   )r   r   r   rD   s      €r   r   Ú ScopedRateThrottle.allow_requestÛ   s^   ø€ ä˜T§?¡?°DÓ9ˆŒ
ð �z�zØð —M‘M“OˆŒ	Ø+/¯?©?¸4¿9¹9Ó+EÑ(ˆÔ˜4œ=ô ‰wÑ$ WÓ3Ð3r   c                 óâ   • UR                   (       a2  UR                   R                  (       a  UR                   R                  nOU R                  U5      nU R                  U R
                  U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.
rt   r~   r€   s       r   r@   Ú ScopedRateThrottle.get_cache_keyë   sT   € ð �<�<˜GŸL™L×9×9Ø—L‘L—O‘O‰Eà—N‘N 7Ó+ˆEà× Ñ Ø—Z‘ZØñ$
ñ 
ð 	
r   )r<   r;   r7   rC   )r-   r.   r/   r0   r1   r‰   r=   r   r@   r2   Ú__classcell__)rD   s   @r   rƒ   rƒ   Í   s#   ø† ñð "€Jòõ
4÷ 
ð 
r   rƒ   )r1   rk   Údjango.core.cacher   rj   Údjango.core.exceptionsr   Úrest_framework.settingsr   r   r4   ro   r{   rƒ   r)   r   r   Ú<module>r’      sY   ðñó å 4Ý 7å 0÷#ñ #ôLp>˜ô p>ôf
Ð)ô 
ô$
Ð)ô 
ô,-
Ð+õ -
r   