Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
bruth committed Dec 31, 2012
1 parent 5b7fd4a commit cc24ac2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
49 changes: 27 additions & 22 deletions restlib2/resources.py
Expand Up @@ -228,6 +228,9 @@ class Resource(object):
# Every `Resource` class can be initialized once since they are stateless # Every `Resource` class can be initialized once since they are stateless
# (and thus thread-safe). # (and thus thread-safe).
def __call__(self, request, *args, **kwargs): def __call__(self, request, *args, **kwargs):
return self.dispatch(request, *args, **kwargs)

def dispatch(self, request, *args, **kwargs):
# Process the request. This includes all the necessary checks prior to # Process the request. This includes all the necessary checks prior to
# actually interfacing with the resource itself. # actually interfacing with the resource itself.
response = self.process_request(request, *args, **kwargs) response = self.process_request(request, *args, **kwargs)
Expand Down Expand Up @@ -308,7 +311,8 @@ def is_too_many_requests(self, request, response, *args, **kwargs):
# ### Request Entity Too Large # ### Request Entity Too Large
# Check if the request entity is too large to process. # Check if the request entity is too large to process.
def is_request_entity_too_large(self, request, response, *args, **kwargs): def is_request_entity_too_large(self, request, response, *args, **kwargs):
if request.META['CONTENT_LENGTH'] > self.max_request_entity_length: if self.max_request_entity_length and \
request.META['CONTENT_LENGTH'] > self.max_request_entity_length:
return True return True


# ### Method Not Allowed # ### Method Not Allowed
Expand Down Expand Up @@ -359,10 +363,15 @@ def is_not_acceptable(self, request, response, *args, **kwargs):
# ### Precondition Required # ### Precondition Required
# Check if a conditional request is # Check if a conditional request is
def is_precondition_required(self, request, response, *args, **kwargs): def is_precondition_required(self, request, response, *args, **kwargs):
if not self.require_conditional_request:
return False

if self.use_etags and 'HTTP_IF_MATCH' not in request.META: if self.use_etags and 'HTTP_IF_MATCH' not in request.META:
return True return True

if self.use_last_modified and 'HTTP_IF_UNMODIFIED_SINCE' not in request.META: if self.use_last_modified and 'HTTP_IF_UNMODIFIED_SINCE' not in request.META:
return True return True

return False return False


def is_precondition_failed(self, request, response, *args, **kwargs): def is_precondition_failed(self, request, response, *args, **kwargs):
Expand All @@ -387,7 +396,6 @@ def is_precondition_failed(self, request, response, *args, **kwargs):


return False return False



# ### Not Found # ### Not Found
# Checks if the requested resource exists. # Checks if the requested resource exists.
def is_not_found(self, request, response, *args, **kwargs): def is_not_found(self, request, response, *args, **kwargs):
Expand All @@ -399,7 +407,6 @@ def is_gone(self, request, response, *args, **kwargs):
return False return False





# ## Request Accept-* handlers # ## Request Accept-* handlers


# Checks if the requested `Accept` mimetype is supported. Defaults # Checks if the requested `Accept` mimetype is supported. Defaults
Expand Down Expand Up @@ -614,6 +621,17 @@ def process_request(self, request, *args, **kwargs):
response.status_code = codes.too_many_requests response.status_code = codes.too_many_requests
return response return response


# ### 405 Method Not Allowed
if self.is_method_not_allowed(request, response, *args, **kwargs):
response.status_code = codes.method_not_allowed
return response

# ### 406 Not Acceptable
# Checks Accept and Accept-* headers
if self.is_not_acceptable(request, response, *args, **kwargs):
response.status_code = codes.not_acceptable
return response

# ### Process an _OPTIONS_ request # ### Process an _OPTIONS_ request
# Enough processing has been performed to allow an OPTIONS request. # Enough processing has been performed to allow an OPTIONS request.
if request.method == methods.OPTIONS and 'OPTIONS' in self.allowed_methods: if request.method == methods.OPTIONS and 'OPTIONS' in self.allowed_methods:
Expand All @@ -631,21 +649,9 @@ def process_request(self, request, *args, **kwargs):


# ### 413 Request Entity Too Large # ### 413 Request Entity Too Large
# Check if the entity is too large for processing # Check if the entity is too large for processing
if self.max_request_entity_length: if self.is_request_entity_too_large(request, response, *args, **kwargs):
if self.is_request_entity_too_large(request, response, *args, **kwargs): response.status_code = codes.request_entity_too_large
response.status_code = codes.request_entity_too_large return response
return response

# ### 405 Method Not Allowed
if self.is_method_not_allowed(request, response, *args, **kwargs):
response.status_code = codes.method_not_allowed
return response

# ### 406 Not Acceptable
# Checks Accept and Accept-* headers
if self.is_not_acceptable(request, response, *args, **kwargs):
response.status_code = codes.not_acceptable
return response


# ### 404 Not Found # ### 404 Not Found
# Check if this resource exists. Note, if this requires a database # Check if this resource exists. Note, if this requires a database
Expand All @@ -671,10 +677,9 @@ def process_request(self, request, *args, **kwargs):
# Prevents the "lost udpate" problem and requires client to confirm # Prevents the "lost udpate" problem and requires client to confirm
# the state of the resource has not changed since the last `GET` # the state of the resource has not changed since the last `GET`
# request. This applies to `PUT` and `PATCH` requests. # request. This applies to `PUT` and `PATCH` requests.
if self.require_conditional_request: if request.method == methods.PUT or request.method == methods.PATCH:
if request.method == methods.PUT or request.method == methods.PATCH: if self.is_precondition_required(request, response, *args, **kwargs):
if self.is_precondition_required(request, response, *args, **kwargs): return UncacheableResponse(status=codes.precondition_required)
return UncacheableResponse(status=codes.precondition_required)


# ### 412 Precondition Failed # ### 412 Precondition Failed
# Conditional requests applies to GET, HEAD, PUT, and PATCH. # Conditional requests applies to GET, HEAD, PUT, and PATCH.
Expand Down
2 changes: 1 addition & 1 deletion run-tests.sh
@@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh


DJANGO_SETTINGS_MODULE='restlib2.tests.settings' PYTHONPATH=. coverage run ../bin/django-admin.py test restlib2 DJANGO_SETTINGS_MODULE='restlib2.tests.settings' PYTHONPATH=. coverage run `which django-admin.py` test restlib2
rm -rf docs/coverage rm -rf docs/coverage
coverage html coverage html

0 comments on commit cc24ac2

Please sign in to comment.