Skip to content

Commit 9775e45

Browse files
authored
fix: remove outdated httpresponse wrapper (#129)
* removed outdated httpresponse wrapper * flake8 fix
1 parent 3771323 commit 9775e45

File tree

2 files changed

+17
-53
lines changed

2 files changed

+17
-53
lines changed

django_declarative_apis/resources/utils.py

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import warnings
99
from pydoc import locate
1010

11-
import django
1211
from decorator import decorator
1312
from django import get_version as django_version
1413
from django.http import HttpResponse
@@ -27,20 +26,20 @@ class rc_factory:
2726
"""
2827

2928
CODES = dict(
30-
ALL_OK=("OK", 200),
31-
CREATED=("Created", 201),
32-
ACCEPTED=("Accepted", 202),
33-
DELETED=("", 204), # 204 says "Don't send a body!"
34-
BAD_REQUEST=("Bad Request", 400),
35-
UNAUTHORIZED=("Unauthorized", 401),
36-
FORBIDDEN=("Forbidden", 403),
37-
NOT_FOUND=("Not Found", 404),
38-
NOT_ACCEPTABLE=("Not Acceptable", 406),
39-
DUPLICATE_ENTRY=("Conflict/Duplicate", 409),
40-
NOT_HERE=("Gone", 410),
41-
INTERNAL_ERROR=("Internal Error", 500),
42-
NOT_IMPLEMENTED=("Not Implemented", 501),
43-
THROTTLED=("Throttled", 503),
29+
ALL_OK=(b"OK", 200),
30+
CREATED=(b"Created", 201),
31+
ACCEPTED=(b"Accepted", 202),
32+
DELETED=(b"", 204), # 204 says "Don't send a body!"
33+
BAD_REQUEST=(b"Bad Request", 400),
34+
UNAUTHORIZED=(b"Unauthorized", 401),
35+
FORBIDDEN=(b"Forbidden", 403),
36+
NOT_FOUND=(b"Not Found", 404),
37+
NOT_ACCEPTABLE=(b"Not Acceptable", 406),
38+
DUPLICATE_ENTRY=(b"Conflict/Duplicate", 409),
39+
NOT_HERE=(b"Gone", 410),
40+
INTERNAL_ERROR=(b"Internal Error", 500),
41+
NOT_IMPLEMENTED=(b"Not Implemented", 501),
42+
THROTTLED=(b"Throttled", 503),
4443
)
4544

4645
def __getattr__(self, attr):
@@ -64,43 +63,7 @@ def __getattr__(self, attr):
6463
DeprecationWarning,
6564
)
6665

67-
class HttpResponseWrapper(HttpResponse):
68-
"""
69-
Wrap HttpResponse and make sure that the internal _is_string
70-
flag is updated when the _set_content method (via the content
71-
property) is called
72-
"""
73-
74-
def _set_content(self, content):
75-
"""
76-
Set the _container and _is_string /
77-
_base_content_is_iter properties based on the type of
78-
the value parameter. This logic is in the construtor
79-
for HttpResponse, but doesn't get repeated when
80-
setting HttpResponse.content although this bug report
81-
(feature request) suggests that it should:
82-
http://code.djangoproject.com/ticket/9403
83-
"""
84-
is_string = False
85-
if not isinstance(content, str) and hasattr(content, "__iter__"):
86-
self._container = content
87-
else:
88-
self._container = [content]
89-
is_string = True
90-
if django.VERSION >= (1, 4):
91-
self._base_content_is_iter = not is_string
92-
else: # pragma: nocover
93-
self._is_string = is_string
94-
95-
try:
96-
content = property(HttpResponse._get_content, _set_content)
97-
except Exception:
98-
99-
@HttpResponse.content.setter
100-
def content(self, content):
101-
self._set_content(content)
102-
103-
return HttpResponseWrapper(r, content_type="text/plain", status=c)
66+
return HttpResponse(r, content_type="text/plain", status=c)
10467

10568

10669
rc = rc_factory()

tests/resources/test_resource.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class Handler:
5050
) as mock_translate:
5151
mock_translate.side_effect = resource.MimerDataException
5252

53-
res(req)
53+
resource_instance = res(req)
54+
self.assertEqual(resource_instance.content, b"Bad Request")
5455

5556
def test_call_put(self):
5657
class Handler:

0 commit comments

Comments
 (0)