diff options
| author | Tom Christie | 2012-08-26 23:06:52 +0100 |
|---|---|---|
| committer | Tom Christie | 2012-08-26 23:06:52 +0100 |
| commit | 73cc77553ed5411f1959a51574b156a47ad5340d (patch) | |
| tree | 0b3a0f707fefd116a274adcfa80364596e8df76d /djangorestframework/permissions.py | |
| parent | edd8f5963cb32063931a1557d3c6ac29d19b3425 (diff) | |
| download | django-rest-framework-73cc77553ed5411f1959a51574b156a47ad5340d.tar.bz2 | |
Drop ImmediateResponse
Diffstat (limited to 'djangorestframework/permissions.py')
| -rw-r--r-- | djangorestframework/permissions.py | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/djangorestframework/permissions.py b/djangorestframework/permissions.py index b56d8a32..bdda4def 100644 --- a/djangorestframework/permissions.py +++ b/djangorestframework/permissions.py @@ -6,9 +6,7 @@ Permission behavior is provided by mixing the :class:`mixins.PermissionsMixin` c """ from django.core.cache import cache -from djangorestframework import status -from djangorestframework.exceptions import PermissionDenied -from djangorestframework.response import ImmediateResponse +from djangorestframework.exceptions import PermissionDenied, Throttled import time __all__ = ( @@ -24,11 +22,6 @@ __all__ = ( SAFE_METHODS = ['GET', 'HEAD', 'OPTIONS'] -_503_SERVICE_UNAVAILABLE = ImmediateResponse( - {'detail': 'request was throttled'}, - status=status.HTTP_503_SERVICE_UNAVAILABLE) - - class BasePermission(object): """ A base class from which all permission classes should inherit. @@ -192,7 +185,7 @@ class BaseThrottle(BasePermission): """ self.history.insert(0, self.now) cache.set(self.key, self.history, self.duration) - header = 'status=SUCCESS; next=%s sec' % self.next() + header = 'status=SUCCESS; next=%.2f sec' % self.next() self.view.headers['X-Throttle'] = header def throttle_failure(self): @@ -200,9 +193,10 @@ class BaseThrottle(BasePermission): Called when a request to the API has failed due to throttling. Raises a '503 service unavailable' response. """ - header = 'status=FAILURE; next=%s sec' % self.next() + wait = self.next() + header = 'status=FAILURE; next=%.2f sec' % wait self.view.headers['X-Throttle'] = header - raise _503_SERVICE_UNAVAILABLE + raise Throttled(wait) def next(self): """ @@ -215,7 +209,7 @@ class BaseThrottle(BasePermission): available_requests = self.num_requests - len(self.history) + 1 - return '%.2f' % (remaining_duration / float(available_requests)) + return remaining_duration / float(available_requests) class PerUserThrottling(BaseThrottle): |
