Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Used the decorator syntax for properties in django.http

  • Loading branch information...
commit e2b4eddc11b391bc0047032776e9ca7c2f9328b3 1 parent dfe63a5
@claudep claudep authored
Showing with 29 additions and 29 deletions.
  1. +22 −22 django/http/__init__.py
  2. +7 −7 django/template/response.py
View
44 django/http/__init__.py
@@ -243,7 +243,12 @@ def is_secure(self):
def is_ajax(self):
return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
- def _set_encoding(self, val):
+ @property
+ def encoding(self):
+ return self._encoding
+
+ @encoding.setter
+ def encoding(self, val):
"""
Sets the encoding used for GET/POST accesses. If the GET or POST
dictionary has already been created, it is removed and recreated on the
@@ -255,27 +260,22 @@ def _set_encoding(self, val):
if hasattr(self, '_post'):
del self._post
- def _get_encoding(self):
- return self._encoding
-
- encoding = property(_get_encoding, _set_encoding)
-
def _initialize_handlers(self):
self._upload_handlers = [uploadhandler.load_handler(handler, self)
for handler in settings.FILE_UPLOAD_HANDLERS]
- def _set_upload_handlers(self, upload_handlers):
- if hasattr(self, '_files'):
- raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
- self._upload_handlers = upload_handlers
-
- def _get_upload_handlers(self):
+ @property
+ def upload_handlers(self):
if not self._upload_handlers:
# If there are no upload handlers defined, initialize them from settings.
self._initialize_handlers()
return self._upload_handlers
- upload_handlers = property(_get_upload_handlers, _set_upload_handlers)
+ @upload_handlers.setter
+ def upload_handlers(self, upload_handlers):
+ if hasattr(self, '_files'):
+ raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
+ self._upload_handlers = upload_handlers
def parse_file_upload(self, META, post_data):
"""Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
@@ -397,16 +397,16 @@ def __init__(self, query_string, mutable=False, encoding=None):
force_text(value, encoding, errors='replace'))
self._mutable = mutable
- def _get_encoding(self):
+ @property
+ def encoding(self):
if self._encoding is None:
self._encoding = settings.DEFAULT_CHARSET
return self._encoding
- def _set_encoding(self, value):
+ @encoding.setter
+ def encoding(self, value):
self._encoding = value
- encoding = property(_get_encoding, _set_encoding)
-
def _assert_mutable(self):
if not self._mutable:
raise AttributeError("This QueryDict instance is immutable")
@@ -539,7 +539,7 @@ def __init__(self, content='', content_type=None, status=None,
if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
self._charset)
- # content is a bytestring. See _get_content / _set_content.
+ # content is a bytestring. See the content property methods.
self.content = content
self.cookies = SimpleCookie()
if status:
@@ -669,7 +669,8 @@ def delete_cookie(self, key, path='/', domain=None):
self.set_cookie(key, max_age=0, path=path, domain=domain,
expires='Thu, 01-Jan-1970 00:00:00 GMT')
- def _get_content(self):
+ @property
+ def content(self):
if self.has_header('Content-Encoding'):
def make_bytes(value):
if isinstance(value, int):
@@ -681,7 +682,8 @@ def make_bytes(value):
return b''.join(make_bytes(e) for e in self._container)
return b''.join(smart_bytes(e, self._charset) for e in self._container)
- def _set_content(self, value):
+ @content.setter
+ def content(self, value):
if hasattr(value, '__iter__') and not isinstance(value, (bytes, six.string_types)):
self._container = value
self._base_content_is_iter = True
@@ -689,8 +691,6 @@ def _set_content(self, value):
self._container = [value]
self._base_content_is_iter = False
- content = property(_get_content, _set_content)
-
def __iter__(self):
self._iterator = iter(self._container)
return self
View
14 django/template/response.py
@@ -102,7 +102,7 @@ def render(self):
"""
retval = self
if not self._is_rendered:
- self._set_content(self.rendered_content)
+ self.content = self.rendered_content
for post_callback in self._post_render_callbacks:
newretval = post_callback(retval)
if newretval is not None:
@@ -119,20 +119,20 @@ def __iter__(self):
'rendered before it can be iterated over.')
return super(SimpleTemplateResponse, self).__iter__()
- def _get_content(self):
+ @property
+ def content(self):
if not self._is_rendered:
raise ContentNotRenderedError('The response content must be '
'rendered before it can be accessed.')
- return super(SimpleTemplateResponse, self)._get_content()
+ return super(SimpleTemplateResponse, self).content
- def _set_content(self, value):
+ @content.setter
+ def content(self, value):
"""Sets the content for the response
"""
- super(SimpleTemplateResponse, self)._set_content(value)
+ HttpResponse.content.fset(self, value)
@alex Collaborator
alex added a note

You can just do super(SimpleTemplateResponse, self).content = value AFAIK.

@claudep Collaborator
claudep added a note

AttributeError: 'super' object has no attribute 'content'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
self._is_rendered = True
- content = property(_get_content, _set_content)
-
class TemplateResponse(SimpleTemplateResponse):
rendering_attrs = SimpleTemplateResponse.rendering_attrs + \
Please sign in to comment.
Something went wrong with that request. Please try again.