| 1 | import timescip-python python python-stdlib 3.11 time/__init__: |
| 2 | |
| 3 | from django.core.cachescip-python python django-tastypie 0.15.1 `django.core.cache`/__init__: import cache |
| 4 | |
| 5 | |
| 6 | _other_allowed_charsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/_other_allowed_chars. = frozensetscip-python python python-stdlib 3.11 builtins/frozenset#(['_', '.', '-']) |
| 7 | |
| 8 | |
| 9 | class BaseThrottlescip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#(objectscip-python python python-stdlib 3.11 builtins/object#): |
| 10 | """ |
| 11 | A simplified, swappable base class for throttling. |
| 12 | |
| 13 | Does nothing save for simulating the throttling API and implementing |
| 14 | some common bits for the subclasses. |
| 15 | |
| 16 | Accepts a number of optional kwargs:: |
| 17 | |
| 18 | * ``throttle_at`` - the number of requests at which the user should |
| 19 | be throttled. Default is 150 requests. |
| 20 | * ``timeframe`` - the length of time (in seconds) in which the user |
| 21 | make up to the ``throttle_at`` requests. Default is 3600 seconds ( |
| 22 | 1 hour). |
| 23 | * ``expiration`` - the length of time to retain the times the user |
| 24 | has accessed the api in the cache. Default is 604800 (1 week). |
| 25 | """ |
| 26 | def __init__scip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(self), throttle_atscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(throttle_at)=150, timeframescip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(timeframe)=3600, expirationscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(expiration)=None): |
| 27 | selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(self).throttle_atscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#throttle_at. = throttle_atscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(throttle_at) |
| 28 | # In seconds, please. |
| 29 | selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(self).timeframescip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#timeframe. = timeframescip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(timeframe) |
| 30 | |
| 31 | if expirationscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(expiration) is None: |
| 32 | # Expire in a week. |
| 33 | expirationscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(expiration) = 604800 |
| 34 | |
| 35 | selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(self).expirationscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#expiration. = intscip-python python python-stdlib 3.11 builtins/int#(expirationscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#__init__().(expiration)) |
| 36 | |
| 37 | def convert_identifier_to_keyscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#convert_identifier_to_key().(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#convert_identifier_to_key().(self), identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#convert_identifier_to_key().(identifier)): |
| 38 | """ |
| 39 | Takes an identifier (like a username or IP address) and converts it |
| 40 | into a key usable by the cache system. |
| 41 | """ |
| 42 | bits = [ |
| 43 | char |
| 44 | for char in identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#convert_identifier_to_key().(identifier) |
| 45 | if char.isalnum() or char in _other_allowed_charsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/_other_allowed_chars. |
| 46 | ] |
| 47 | bits.appendscip-python python python-stdlib 3.11 builtins/list#append().('_accesses') |
| 48 | |
| 49 | return ''.joinscip-python python python-stdlib 3.11 builtins/str#join().(bits) |
| 50 | |
| 51 | def should_be_throttledscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#should_be_throttled().(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#should_be_throttled().(self), identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#should_be_throttled().(identifier), **kwargsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#should_be_throttled().(kwargs)): |
| 52 | """ |
| 53 | Returns whether or not the user has exceeded their throttle limit. If |
| 54 | throttled, can return either True, and int specifying the number of |
| 55 | seconds to wait, or a datetime object specifying when to retry the |
| 56 | request. |
| 57 | |
| 58 | Always returns ``False``, as this implementation does not actually |
| 59 | throttle the user. |
| 60 | """ |
| 61 | return False |
| 62 | |
| 63 | def accessedscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#accessed().(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#accessed().(self), identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#accessed().(identifier), **kwargsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#accessed().(kwargs)): |
| 64 | """ |
| 65 | Handles recording the user's access. |
| 66 | |
| 67 | Does nothing in this implementation. |
| 68 | """ |
| 69 | pass |
| 70 | |
| 71 | |
| 72 | class CacheThrottlescip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#(BaseThrottlescip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#): |
| 73 | """ |
| 74 | A throttling mechanism that uses just the cache. |
| 75 | """ |
| 76 | def should_be_throttledscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(self), identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(identifier), **kwargsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(kwargs)): |
| 77 | """ |
| 78 | Returns whether or not the user has exceeded their throttle limit. If |
| 79 | throttled, can return either True, and int specifying the number of |
| 80 | seconds to wait, or a datetime object specifying when to retry the |
| 81 | request. |
| 82 | |
| 83 | Maintains a list of timestamps when the user accessed the api within |
| 84 | the cache. |
| 85 | |
| 86 | Returns ``False`` if the user should NOT be throttled or ``True`` if |
| 87 | the user should be throttled. |
| 88 | """ |
| 89 | key = selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(self).convert_identifier_to_keyscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#convert_identifier_to_key().(identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(identifier)) |
| 90 | |
| 91 | # Weed out anything older than the timeframe. |
| 92 | now = intscip-python python python-stdlib 3.11 builtins/int#(timescip-python python python-stdlib 3.11 time/__init__:.timescip-python python python-stdlib 3.11 time/time().()) |
| 93 | timeframe = intscip-python python python-stdlib 3.11 builtins/int#(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(self).timeframescip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#timeframe.) |
| 94 | throttle_at = intscip-python python python-stdlib 3.11 builtins/int#(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(self).throttle_atscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#throttle_at.) |
| 95 | |
| 96 | minimum_time = now - timeframe |
| 97 | times_accessed = [access for access in cache.get(key, []) if access >= minimum_time] |
| 98 | cache.set(key, times_accessed, selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#should_be_throttled().(self).expirationscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#expiration.) |
| 99 | |
| 100 | if len(times_accessed) >= throttle_at: |
| 101 | # Throttle them. |
| 102 | return timeframe - (now - times_accessed[-throttle_at]) |
| 103 | |
| 104 | # Let them through. |
| 105 | return False |
| 106 | |
| 107 | def accessedscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#accessed().(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#accessed().(self), identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#accessed().(identifier), **kwargsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#accessed().(kwargs)): |
| 108 | """ |
| 109 | Handles recording the user's access. |
| 110 | |
| 111 | Stores the current timestamp in the "accesses" list within the cache. |
| 112 | """ |
| 113 | key = selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#accessed().(self).convert_identifier_to_keyscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#convert_identifier_to_key().(identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#accessed().(identifier)) |
| 114 | times_accessed = cache.get(key, []) |
| 115 | times_accessed.append(intscip-python python python-stdlib 3.11 builtins/int#(timescip-python python python-stdlib 3.11 time/__init__:.timescip-python python python-stdlib 3.11 time/time().())) |
| 116 | cache.set(key, times_accessed, selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#accessed().(self).expirationscip-python python django-tastypie 0.15.1 `tastypie.throttle`/BaseThrottle#expiration.) |
| 117 | |
| 118 | |
| 119 | class CacheDBThrottlescip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#(CacheThrottlescip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#): |
| 120 | """ |
| 121 | A throttling mechanism that uses the cache for actual throttling but |
| 122 | writes-through to the database. |
| 123 | |
| 124 | This is useful for tracking/aggregating usage through time, to possibly |
| 125 | build a statistics interface or a billing mechanism. |
| 126 | """ |
| 127 | def accessedscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(self), identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(identifier), **kwargsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(kwargs)): |
| 128 | """ |
| 129 | Handles recording the user's access. |
| 130 | |
| 131 | Does everything the ``CacheThrottle`` class does, plus logs the |
| 132 | access within the database using the ``ApiAccess`` model. |
| 133 | """ |
| 134 | # Do the import here, instead of top-level, so that the model is |
| 135 | # only required when using this throttling mechanism. |
| 136 | from tastypie.modelsscip-python python django-tastypie 0.15.1 `tastypie.models`/__init__: import ApiAccessscip-python python django-tastypie 0.15.1 `tastypie.models`/ApiAccess# |
| 137 | superscip-python python python-stdlib 3.11 builtins/super#(CacheDBThrottlescip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#, selfscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(self)).accessedscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheThrottle#accessed().(identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(identifier), **kwargsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(kwargs)) |
| 138 | # Write out the access to the DB for logging purposes. |
| 139 | ApiAccessscip-python python django-tastypie 0.15.1 `tastypie.models`/ApiAccess#.objects.create( |
| 140 | identifier=identifierscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(identifier), |
| 141 | url=kwargsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(kwargs).getscip-python python python-stdlib 3.11 builtins/dict#get().('url', ''), |
| 142 | request_method=kwargsscip-python python django-tastypie 0.15.1 `tastypie.throttle`/CacheDBThrottle#accessed().(kwargs).getscip-python python python-stdlib 3.11 builtins/dict#get().('request_method', '') |
| 143 | ) |