Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
57b3a372
Commit
57b3a372
authored
Feb 19, 2011
by
tom christie tom@tomchristie.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move status codes into seperate module
parent
805aa03e
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
151 additions
and
125 deletions
+151
-125
djangorestframework/emitters.py
+2
-1
djangorestframework/modelresource.py
+2
-1
djangorestframework/parsers.py
+2
-1
djangorestframework/resource.py
+2
-3
djangorestframework/response.py
+8
-100
djangorestframework/status.py
+91
-0
djangorestframework/tests/response.py
+18
-15
djangorestframework/tests/status.py
+16
-0
examples/blogpost/views.py
+3
-1
examples/objectstore/views.py
+3
-2
examples/resourceexample/views.py
+4
-1
No files found.
djangorestframework/emitters.py
View file @
57b3a372
...
@@ -8,13 +8,14 @@ from django.http import HttpResponse
...
@@ -8,13 +8,14 @@ from django.http import HttpResponse
from
django.template
import
RequestContext
,
loader
from
django.template
import
RequestContext
,
loader
from
django
import
forms
from
django
import
forms
from
djangorestframework.response
import
NoContent
,
ResponseException
,
status
from
djangorestframework.response
import
NoContent
,
ResponseException
from
djangorestframework.validators
import
FormValidatorMixin
from
djangorestframework.validators
import
FormValidatorMixin
from
djangorestframework.utils
import
dict2xml
,
url_resolves
from
djangorestframework.utils
import
dict2xml
,
url_resolves
from
djangorestframework.markdownwrapper
import
apply_markdown
from
djangorestframework.markdownwrapper
import
apply_markdown
from
djangorestframework.breadcrumbs
import
get_breadcrumbs
from
djangorestframework.breadcrumbs
import
get_breadcrumbs
from
djangorestframework.content
import
OverloadedContentMixin
from
djangorestframework.content
import
OverloadedContentMixin
from
djangorestframework.description
import
get_name
,
get_description
from
djangorestframework.description
import
get_name
,
get_description
from
djangorestframework
import
status
from
urllib
import
quote_plus
from
urllib
import
quote_plus
import
string
import
string
...
...
djangorestframework/modelresource.py
View file @
57b3a372
...
@@ -2,9 +2,10 @@ from django.forms import ModelForm
...
@@ -2,9 +2,10 @@ from django.forms import ModelForm
from
django.db.models.query
import
QuerySet
from
django.db.models.query
import
QuerySet
from
django.db.models
import
Model
from
django.db.models
import
Model
from
djangorestframework.response
import
status
,
Response
,
ResponseException
from
djangorestframework.response
import
Response
,
ResponseException
from
djangorestframework.resource
import
Resource
from
djangorestframework.resource
import
Resource
from
djangorestframework.validators
import
ModelFormValidatorMixin
from
djangorestframework.validators
import
ModelFormValidatorMixin
from
djangorestframework
import
status
import
decimal
import
decimal
import
inspect
import
inspect
...
...
djangorestframework/parsers.py
View file @
57b3a372
from
djangorestframework.response
import
status
,
ResponseException
from
djangorestframework.response
import
ResponseException
from
djangorestframework
import
status
try
:
try
:
import
json
import
json
...
...
djangorestframework/resource.py
View file @
57b3a372
...
@@ -8,8 +8,8 @@ from djangorestframework.authenticators import AuthenticatorMixin
...
@@ -8,8 +8,8 @@ from djangorestframework.authenticators import AuthenticatorMixin
from
djangorestframework.validators
import
FormValidatorMixin
from
djangorestframework.validators
import
FormValidatorMixin
from
djangorestframework.content
import
OverloadedContentMixin
from
djangorestframework.content
import
OverloadedContentMixin
from
djangorestframework.methods
import
OverloadedPOSTMethodMixin
from
djangorestframework.methods
import
OverloadedPOSTMethodMixin
from
djangorestframework
import
emitters
,
parsers
,
authenticators
from
djangorestframework
.response
import
Response
,
ResponseException
from
djangorestframework
.response
import
status
,
Response
,
ResponseException
from
djangorestframework
import
emitters
,
parsers
,
authenticators
,
status
import
re
import
re
...
@@ -124,7 +124,6 @@ class Resource(EmitterMixin, ParserMixin, AuthenticatorMixin, FormValidatorMixin
...
@@ -124,7 +124,6 @@ class Resource(EmitterMixin, ParserMixin, AuthenticatorMixin, FormValidatorMixin
return
data
return
data
# Session based authentication is explicitly CSRF validated, all other authentication is CSRF exempt.
# Session based authentication is explicitly CSRF validated, all other authentication is CSRF exempt.
@csrf_exempt
@csrf_exempt
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
"""This method is the core of Resource, through which all requests are passed.
"""This method is the core of Resource, through which all requests are passed.
...
...
djangorestframework/response.py
View file @
57b3a372
from
django.core.handlers.wsgi
import
STATUS_CODE_TEXT
from
django.core.handlers.wsgi
import
STATUS_CODE_TEXT
__all__
=
[
'
status'
,
'
NoContent'
,
'Response'
,
]
__all__
=
[
'NoContent'
,
'Response'
,
]
class
Status
(
object
):
"""Descriptive HTTP status codes, for code readability.
See RFC 2616 - Sec 10: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"""
# Verbose format (I prefer this as it's more explicit)
HTTP_100_CONTINUE
=
100
HTTP_101_SWITCHING_PROTOCOLS
=
101
HTTP_200_OK
=
200
HTTP_201_CREATED
=
201
HTTP_202_ACCEPTED
=
202
HTTP_203_NON_AUTHORITATIVE_INFORMATION
=
203
HTTP_204_NO_CONTENT
=
204
HTTP_205_RESET_CONTENT
=
205
HTTP_206_PARTIAL_CONTENT
=
206
HTTP_300_MULTIPLE_CHOICES
=
300
HTTP_301_MOVED_PERMANENTLY
=
301
HTTP_302_FOUND
=
302
HTTP_303_SEE_OTHER
=
303
HTTP_304_NOT_MODIFIED
=
304
HTTP_305_USE_PROXY
=
305
HTTP_306_RESERVED
=
306
HTTP_307_TEMPORARY_REDIRECT
=
307
HTTP_400_BAD_REQUEST
=
400
HTTP_401_UNAUTHORIZED
=
401
HTTP_402_PAYMENT_REQUIRED
=
402
HTTP_403_FORBIDDEN
=
403
HTTP_404_NOT_FOUND
=
404
HTTP_405_METHOD_NOT_ALLOWED
=
405
HTTP_406_NOT_ACCEPTABLE
=
406
HTTP_407_PROXY_AUTHENTICATION_REQUIRED
=
407
HTTP_408_REQUEST_TIMEOUT
=
408
HTTP_409_CONFLICT
=
409
HTTP_410_GONE
=
410
HTTP_411_LENGTH_REQUIRED
=
411
HTTP_412_PRECONDITION_FAILED
=
412
HTTP_413_REQUEST_ENTITY_TOO_LARGE
=
413
HTTP_414_REQUEST_URI_TOO_LONG
=
414
HTTP_415_UNSUPPORTED_MEDIA_TYPE
=
415
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE
=
416
HTTP_417_EXPECTATION_FAILED
=
417
HTTP_500_INTERNAL_SERVER_ERROR
=
500
HTTP_501_NOT_IMPLEMENTED
=
501
HTTP_502_BAD_GATEWAY
=
502
HTTP_503_SERVICE_UNAVAILABLE
=
503
HTTP_504_GATEWAY_TIMEOUT
=
504
HTTP_505_HTTP_VERSION_NOT_SUPPORTED
=
505
# Short format
CONTINUE
=
100
SWITCHING_PROTOCOLS
=
101
OK
=
200
CREATED
=
201
ACCEPTED
=
202
NON_AUTHORITATIVE_INFORMATION
=
203
NO_CONTENT
=
204
RESET_CONTENT
=
205
PARTIAL_CONTENT
=
206
MULTIPLE_CHOICES
=
300
MOVED_PERMANENTLY
=
301
FOUND
=
302
SEE_OTHER
=
303
NOT_MODIFIED
=
304
USE_PROXY
=
305
RESERVED
=
306
TEMPORARY_REDIRECT
=
307
BAD_REQUEST
=
400
UNAUTHORIZED
=
401
PAYMENT_REQUIRED
=
402
FORBIDDEN
=
403
NOT_FOUND
=
404
METHOD_NOT_ALLOWED
=
405
NOT_ACCEPTABLE
=
406
PROXY_AUTHENTICATION_REQUIRED
=
407
REQUEST_TIMEOUT
=
408
CONFLICT
=
409
GONE
=
410
LENGTH_REQUIRED
=
411
PRECONDITION_FAILED
=
412
REQUEST_ENTITY_TOO_LARGE
=
413
REQUEST_URI_TOO_LONG
=
414
UNSUPPORTED_MEDIA_TYPE
=
415
REQUESTED_RANGE_NOT_SATISFIABLE
=
416
EXPECTATION_FAILED
=
417
INTERNAL_SERVER_ERROR
=
500
NOT_IMPLEMENTED
=
501
BAD_GATEWAY
=
502
SERVICE_UNAVAILABLE
=
503
GATEWAY_TIMEOUT
=
504
HTTP_VERSION_NOT_SUPPORTED
=
505
# This is simply stylistic, I think 'status.HTTP_200_OK' reads nicely.
status
=
Status
()
class
NoContent
(
object
):
class
NoContent
(
object
):
"""Used to indicate no body in http response.
"""Used to indicate no body in http response.
(We cannot just use None, as that is a valid, serializable response object.)"""
(We cannot just use None, as that is a valid, serializable response object.)
TODO: On relflection I'm going to get rid of this and just not support serailized 'None' responses.
"""
pass
pass
class
Response
(
object
):
class
Response
(
object
):
def
__init__
(
self
,
status
=
200
,
content
=
NoContent
,
headers
=
{}):
def
__init__
(
self
,
status
=
200
,
content
=
NoContent
,
headers
=
{}):
self
.
status
=
status
self
.
status
=
status
self
.
has_content_body
=
not
content
is
NoContent
self
.
has_content_body
=
not
content
is
NoContent
# TODO: remove and just use content
self
.
raw_content
=
content
# content prior to filtering
self
.
raw_content
=
content
# content prior to filtering
- TODO: remove and just use content
self
.
cleaned_content
=
content
# content after filtering
self
.
cleaned_content
=
content
# content after filtering
TODO: remove and just use content
self
.
headers
=
headers
self
.
headers
=
headers
@property
@property
...
...
djangorestframework/status.py
0 → 100644
View file @
57b3a372
"""Descriptive HTTP status codes, for code readability.
See RFC 2616 - Sec 10: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Also, django.core.handlers.wsgi.STATUS_CODE_TEXT"""
# Verbose format
HTTP_100_CONTINUE
=
100
HTTP_101_SWITCHING_PROTOCOLS
=
101
HTTP_200_OK
=
200
HTTP_201_CREATED
=
201
HTTP_202_ACCEPTED
=
202
HTTP_203_NON_AUTHORITATIVE_INFORMATION
=
203
HTTP_204_NO_CONTENT
=
204
HTTP_205_RESET_CONTENT
=
205
HTTP_206_PARTIAL_CONTENT
=
206
HTTP_300_MULTIPLE_CHOICES
=
300
HTTP_301_MOVED_PERMANENTLY
=
301
HTTP_302_FOUND
=
302
HTTP_303_SEE_OTHER
=
303
HTTP_304_NOT_MODIFIED
=
304
HTTP_305_USE_PROXY
=
305
HTTP_306_RESERVED
=
306
HTTP_307_TEMPORARY_REDIRECT
=
307
HTTP_400_BAD_REQUEST
=
400
HTTP_401_UNAUTHORIZED
=
401
HTTP_402_PAYMENT_REQUIRED
=
402
HTTP_403_FORBIDDEN
=
403
HTTP_404_NOT_FOUND
=
404
HTTP_405_METHOD_NOT_ALLOWED
=
405
HTTP_406_NOT_ACCEPTABLE
=
406
HTTP_407_PROXY_AUTHENTICATION_REQUIRED
=
407
HTTP_408_REQUEST_TIMEOUT
=
408
HTTP_409_CONFLICT
=
409
HTTP_410_GONE
=
410
HTTP_411_LENGTH_REQUIRED
=
411
HTTP_412_PRECONDITION_FAILED
=
412
HTTP_413_REQUEST_ENTITY_TOO_LARGE
=
413
HTTP_414_REQUEST_URI_TOO_LONG
=
414
HTTP_415_UNSUPPORTED_MEDIA_TYPE
=
415
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE
=
416
HTTP_417_EXPECTATION_FAILED
=
417
HTTP_500_INTERNAL_SERVER_ERROR
=
500
HTTP_501_NOT_IMPLEMENTED
=
501
HTTP_502_BAD_GATEWAY
=
502
HTTP_503_SERVICE_UNAVAILABLE
=
503
HTTP_504_GATEWAY_TIMEOUT
=
504
HTTP_505_HTTP_VERSION_NOT_SUPPORTED
=
505
# Short format
CONTINUE
=
100
SWITCHING_PROTOCOLS
=
101
OK
=
200
CREATED
=
201
ACCEPTED
=
202
NON_AUTHORITATIVE_INFORMATION
=
203
NO_CONTENT
=
204
RESET_CONTENT
=
205
PARTIAL_CONTENT
=
206
MULTIPLE_CHOICES
=
300
MOVED_PERMANENTLY
=
301
FOUND
=
302
SEE_OTHER
=
303
NOT_MODIFIED
=
304
USE_PROXY
=
305
RESERVED
=
306
TEMPORARY_REDIRECT
=
307
BAD_REQUEST
=
400
UNAUTHORIZED
=
401
PAYMENT_REQUIRED
=
402
FORBIDDEN
=
403
NOT_FOUND
=
404
METHOD_NOT_ALLOWED
=
405
NOT_ACCEPTABLE
=
406
PROXY_AUTHENTICATION_REQUIRED
=
407
REQUEST_TIMEOUT
=
408
CONFLICT
=
409
GONE
=
410
LENGTH_REQUIRED
=
411
PRECONDITION_FAILED
=
412
REQUEST_ENTITY_TOO_LARGE
=
413
REQUEST_URI_TOO_LONG
=
414
UNSUPPORTED_MEDIA_TYPE
=
415
REQUESTED_RANGE_NOT_SATISFIABLE
=
416
EXPECTATION_FAILED
=
417
INTERNAL_SERVER_ERROR
=
500
NOT_IMPLEMENTED
=
501
BAD_GATEWAY
=
502
SERVICE_UNAVAILABLE
=
503
GATEWAY_TIMEOUT
=
504
HTTP_VERSION_NOT_SUPPORTED
=
505
\ No newline at end of file
djangorestframework/tests/response.py
View file @
57b3a372
from
django.test
import
TestCase
# Right now we expect this test to fail - I'm just going to leave it commented out.
from
djangorestframework.response
import
Response
# Looking forward to actually being able to raise ExpectedFailure sometime!
#
#from django.test import TestCase
class
TestResponse
(
TestCase
):
#from djangorestframework.response import Response
#
# Interface tests
#
#class TestResponse(TestCase):
# This is mainly to remind myself that the Response interface needs to change slightly
#
def
test_response_interface
(
self
):
# # Interface tests
"""Ensure the Response interface is as expected."""
#
response
=
Response
()
# # This is mainly to remind myself that the Response interface needs to change slightly
getattr
(
response
,
'status'
)
# def test_response_interface(self):
getattr
(
response
,
'content'
)
# """Ensure the Response interface is as expected."""
getattr
(
response
,
'headers'
)
# response = Response()
# getattr(response, 'status')
# getattr(response, 'content')
# getattr(response, 'headers')
djangorestframework/tests/status.py
0 → 100644
View file @
57b3a372
"""Tests for the status module"""
from
django.test
import
TestCase
from
djangorestframework
import
status
class
TestStatus
(
TestCase
):
"""Simple sanity test to check the status module"""
def
test_status
(
self
):
"""Ensure the status module is present and correct."""
self
.
assertEquals
(
200
,
status
.
OK
)
self
.
assertEquals
(
200
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
404
,
status
.
NOT_FOUND
)
self
.
assertEquals
(
404
,
status
.
HTTP_404_NOT_FOUND
)
examples/blogpost/views.py
View file @
57b3a372
from
djangorestframework.response
import
Response
,
status
from
djangorestframework.response
import
Response
from
djangorestframework.resource
import
Resource
from
djangorestframework.resource
import
Resource
from
djangorestframework.modelresource
import
ModelResource
,
RootModelResource
from
djangorestframework.modelresource
import
ModelResource
,
RootModelResource
from
djangorestframework
import
status
from
blogpost
import
models
from
blogpost
import
models
BLOG_POST_FIELDS
=
(
'created'
,
'title'
,
'slug'
,
'content'
,
'absolute_url'
,
'comment_url'
,
'comments_url'
)
BLOG_POST_FIELDS
=
(
'created'
,
'title'
,
'slug'
,
'content'
,
'absolute_url'
,
'comment_url'
,
'comments_url'
)
...
...
examples/objectstore/views.py
View file @
57b3a372
...
@@ -2,7 +2,8 @@ from django.conf import settings
...
@@ -2,7 +2,8 @@ from django.conf import settings
from
django.core.urlresolvers
import
reverse
from
django.core.urlresolvers
import
reverse
from
djangorestframework.resource
import
Resource
from
djangorestframework.resource
import
Resource
from
djangorestframework.response
import
Response
,
status
from
djangorestframework.response
import
Response
from
djangorestframework
import
status
import
pickle
import
pickle
import
os
import
os
...
@@ -19,7 +20,7 @@ def remove_oldest_files(dir, max_files):
...
@@ -19,7 +20,7 @@ def remove_oldest_files(dir, max_files):
filepaths
=
[
os
.
path
.
join
(
dir
,
file
)
for
file
in
os
.
listdir
(
dir
)]
filepaths
=
[
os
.
path
.
join
(
dir
,
file
)
for
file
in
os
.
listdir
(
dir
)]
ctime_sorted_paths
=
[
item
[
0
]
for
item
in
sorted
([(
path
,
os
.
path
.
getctime
(
path
))
for
path
in
filepaths
],
ctime_sorted_paths
=
[
item
[
0
]
for
item
in
sorted
([(
path
,
os
.
path
.
getctime
(
path
))
for
path
in
filepaths
],
key
=
operator
.
itemgetter
(
1
),
reverse
=
True
)]
key
=
operator
.
itemgetter
(
1
),
reverse
=
True
)]
[
os
.
remove
(
path
)
for
path
in
ctime_sorted_paths
[
max_file
s
:]]
[
os
.
remove
(
path
)
for
path
in
ctime_sorted_paths
[
max_file
:]]
class
ObjectStoreRoot
(
Resource
):
class
ObjectStoreRoot
(
Resource
):
...
...
examples/resourceexample/views.py
View file @
57b3a372
from
django.core.urlresolvers
import
reverse
from
django.core.urlresolvers
import
reverse
from
djangorestframework.resource
import
Resource
from
djangorestframework.resource
import
Resource
from
djangorestframework.response
import
Response
,
status
from
djangorestframework.response
import
Response
from
djangorestframework
import
status
from
resourceexample.forms
import
MyForm
from
resourceexample.forms
import
MyForm
class
ExampleResource
(
Resource
):
class
ExampleResource
(
Resource
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment