Commit 20f8956c by Tom Christie

Merge monseiur drummond's pagination niceness

parent 59afd87c
...@@ -12,7 +12,7 @@ Andrew Straw <astraw> ...@@ -12,7 +12,7 @@ Andrew Straw <astraw>
Zeth <zeth> Zeth <zeth>
Fernando Zunino <fzunino> Fernando Zunino <fzunino>
Jens Alm <ulmus> Jens Alm <ulmus>
Craig Blaszczyk <jakul> Craig Blaszczyk <jakul>
Garcia Solero <garciasolero> Garcia Solero <garciasolero>
Tom Drummond <devioustree> Tom Drummond <devioustree>
Danilo Bargen <gwrtheyrn> Danilo Bargen <gwrtheyrn>
......
""" """
The :mod:`permissions` module bundles a set of permission classes that are used The :mod:`permissions` module bundles a set of permission classes that are used
for checking if a request passes a certain set of constraints. You can assign a permission for checking if a request passes a certain set of constraints. You can assign a permission
class to your view by setting your View's :attr:`permissions` class attribute. class to your view by setting your View's :attr:`permissions` class attribute.
""" """
...@@ -40,7 +40,7 @@ class BasePermission(object): ...@@ -40,7 +40,7 @@ class BasePermission(object):
Permission classes are always passed the current view on creation. Permission classes are always passed the current view on creation.
""" """
self.view = view self.view = view
def check_permission(self, auth): def check_permission(self, auth):
""" """
Should simply return, or raise an :exc:`response.ErrorResponse`. Should simply return, or raise an :exc:`response.ErrorResponse`.
...@@ -64,7 +64,7 @@ class IsAuthenticated(BasePermission): ...@@ -64,7 +64,7 @@ class IsAuthenticated(BasePermission):
def check_permission(self, user): def check_permission(self, user):
if not user.is_authenticated(): if not user.is_authenticated():
raise _403_FORBIDDEN_RESPONSE raise _403_FORBIDDEN_RESPONSE
class IsAdminUser(BasePermission): class IsAdminUser(BasePermission):
...@@ -82,7 +82,7 @@ class IsUserOrIsAnonReadOnly(BasePermission): ...@@ -82,7 +82,7 @@ class IsUserOrIsAnonReadOnly(BasePermission):
The request is authenticated as a user, or is a read-only request. The request is authenticated as a user, or is a read-only request.
""" """
def check_permission(self, user): def check_permission(self, user):
if (not user.is_authenticated() and if (not user.is_authenticated() and
self.view.method != 'GET' and self.view.method != 'GET' and
self.view.method != 'HEAD'): self.view.method != 'HEAD'):
...@@ -100,7 +100,7 @@ class BaseThrottle(BasePermission): ...@@ -100,7 +100,7 @@ class BaseThrottle(BasePermission):
Period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day') Period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')
Previous request information used for throttling is stored in the cache. Previous request information used for throttling is stored in the cache.
""" """
attr_name = 'throttle' attr_name = 'throttle'
default = '0/sec' default = '0/sec'
...@@ -109,7 +109,7 @@ class BaseThrottle(BasePermission): ...@@ -109,7 +109,7 @@ class BaseThrottle(BasePermission):
def get_cache_key(self): def get_cache_key(self):
""" """
Should return a unique cache-key which can be used for throttling. Should return a unique cache-key which can be used for throttling.
Muse be overridden. Muse be overridden.
""" """
pass pass
...@@ -123,7 +123,7 @@ class BaseThrottle(BasePermission): ...@@ -123,7 +123,7 @@ class BaseThrottle(BasePermission):
self.duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]] self.duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
self.auth = auth self.auth = auth
self.check_throttle() self.check_throttle()
def check_throttle(self): def check_throttle(self):
""" """
Implement the check to see if the request should be throttled. Implement the check to see if the request should be throttled.
...@@ -134,7 +134,7 @@ class BaseThrottle(BasePermission): ...@@ -134,7 +134,7 @@ class BaseThrottle(BasePermission):
self.key = self.get_cache_key() self.key = self.get_cache_key()
self.history = cache.get(self.key, []) self.history = cache.get(self.key, [])
self.now = self.timer() self.now = self.timer()
# Drop any requests from the history which have now passed the # Drop any requests from the history which have now passed the
# throttle duration # throttle duration
while self.history and self.history[-1] <= self.now - self.duration: while self.history and self.history[-1] <= self.now - self.duration:
...@@ -153,7 +153,7 @@ class BaseThrottle(BasePermission): ...@@ -153,7 +153,7 @@ class BaseThrottle(BasePermission):
cache.set(self.key, self.history, self.duration) cache.set(self.key, self.history, self.duration)
header = 'status=SUCCESS; next=%s sec' % self.next() header = 'status=SUCCESS; next=%s sec' % self.next()
self.view.add_header('X-Throttle', header) self.view.add_header('X-Throttle', header)
def throttle_failure(self): def throttle_failure(self):
""" """
Called when a request to the API has failed due to throttling. Called when a request to the API has failed due to throttling.
...@@ -162,7 +162,7 @@ class BaseThrottle(BasePermission): ...@@ -162,7 +162,7 @@ class BaseThrottle(BasePermission):
header = 'status=FAILURE; next=%s sec' % self.next() header = 'status=FAILURE; next=%s sec' % self.next()
self.view.add_header('X-Throttle', header) self.view.add_header('X-Throttle', header)
raise _503_SERVICE_UNAVAILABLE raise _503_SERVICE_UNAVAILABLE
def next(self): def next(self):
""" """
Returns the recommended next request time in seconds. Returns the recommended next request time in seconds.
...@@ -205,7 +205,7 @@ class PerViewThrottling(BaseThrottle): ...@@ -205,7 +205,7 @@ class PerViewThrottling(BaseThrottle):
def get_cache_key(self): def get_cache_key(self):
return 'throttle_view_%s' % self.view.__class__.__name__ return 'throttle_view_%s' % self.view.__class__.__name__
class PerResourceThrottling(BaseThrottle): class PerResourceThrottling(BaseThrottle):
""" """
Limits the rate of API calls that may be used against all views on Limits the rate of API calls that may be used against all views on
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment