
    h                         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      K/var/www/html/env/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     ll56ll&&}5".."a3;""IIcNE[#e*!= =>K$$&&'*rwwsyy{#;;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)SimpleRateThrottle2   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:    tVT**DI+/??499+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     tWd++N>>**+C&s++	,&&tzz22 	,?$**LC&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  iQ )smhdr   )r   int)r   r7   numperiodr;   r<   s         r   r:   SimpleRateThrottle.parse_ratea   sD    
 <jjo3x$U;F1I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     99%%g488zz~~dhh3::< llt||B/488dmm3KKLL llt||B/488dmm3K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?    
 	Atxx(

txx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\\"=M2M!N!%!..T\\1BBQ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IIE1LE!88N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    <<GLL99  ZZ^^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    <<GLL99LLOOENN7+E  ZZ$
 
 	
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
 zz MMO	+/??499+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     <<GLL99LLOOENN7+E  ZZ$
 
 	
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   