Skip to content

Commit

Permalink
Used the decorator syntax for properties in django.http
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep committed Aug 22, 2012
1 parent dfe63a5 commit e2b4edd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
44 changes: 22 additions & 22 deletions django/http/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ def is_secure(self):
def is_ajax(self): def is_ajax(self):
return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' 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 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 dictionary has already been created, it is removed and recreated on the
Expand All @@ -255,27 +260,22 @@ def _set_encoding(self, val):
if hasattr(self, '_post'): if hasattr(self, '_post'):
del self._post del self._post


def _get_encoding(self):
return self._encoding

encoding = property(_get_encoding, _set_encoding)

def _initialize_handlers(self): def _initialize_handlers(self):
self._upload_handlers = [uploadhandler.load_handler(handler, self) self._upload_handlers = [uploadhandler.load_handler(handler, self)
for handler in settings.FILE_UPLOAD_HANDLERS] for handler in settings.FILE_UPLOAD_HANDLERS]


def _set_upload_handlers(self, upload_handlers): @property
if hasattr(self, '_files'): def upload_handlers(self):
raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
self._upload_handlers = upload_handlers

def _get_upload_handlers(self):
if not self._upload_handlers: if not self._upload_handlers:
# If there are no upload handlers defined, initialize them from settings. # If there are no upload handlers defined, initialize them from settings.
self._initialize_handlers() self._initialize_handlers()
return self._upload_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): def parse_file_upload(self, META, post_data):
"""Returns a tuple of (POST QueryDict, FILES MultiValueDict).""" """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
Expand Down Expand Up @@ -397,16 +397,16 @@ def __init__(self, query_string, mutable=False, encoding=None):
force_text(value, encoding, errors='replace')) force_text(value, encoding, errors='replace'))
self._mutable = mutable self._mutable = mutable


def _get_encoding(self): @property
def encoding(self):
if self._encoding is None: if self._encoding is None:
self._encoding = settings.DEFAULT_CHARSET self._encoding = settings.DEFAULT_CHARSET
return self._encoding return self._encoding


def _set_encoding(self, value): @encoding.setter
def encoding(self, value):
self._encoding = value self._encoding = value


encoding = property(_get_encoding, _set_encoding)

def _assert_mutable(self): def _assert_mutable(self):
if not self._mutable: if not self._mutable:
raise AttributeError("This QueryDict instance is immutable") raise AttributeError("This QueryDict instance is immutable")
Expand Down Expand Up @@ -539,7 +539,7 @@ def __init__(self, content='', content_type=None, status=None,
if not content_type: if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
self._charset) self._charset)
# content is a bytestring. See _get_content / _set_content. # content is a bytestring. See the content property methods.
self.content = content self.content = content
self.cookies = SimpleCookie() self.cookies = SimpleCookie()
if status: if status:
Expand Down Expand Up @@ -669,7 +669,8 @@ def delete_cookie(self, key, path='/', domain=None):
self.set_cookie(key, max_age=0, path=path, domain=domain, self.set_cookie(key, max_age=0, path=path, domain=domain,
expires='Thu, 01-Jan-1970 00:00:00 GMT') expires='Thu, 01-Jan-1970 00:00:00 GMT')


def _get_content(self): @property
def content(self):
if self.has_header('Content-Encoding'): if self.has_header('Content-Encoding'):
def make_bytes(value): def make_bytes(value):
if isinstance(value, int): if isinstance(value, int):
Expand All @@ -681,16 +682,15 @@ def make_bytes(value):
return b''.join(make_bytes(e) for e in self._container) return b''.join(make_bytes(e) for e in self._container)
return b''.join(smart_bytes(e, self._charset) 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)): if hasattr(value, '__iter__') and not isinstance(value, (bytes, six.string_types)):
self._container = value self._container = value
self._base_content_is_iter = True self._base_content_is_iter = True
else: else:
self._container = [value] self._container = [value]
self._base_content_is_iter = False self._base_content_is_iter = False


content = property(_get_content, _set_content)

def __iter__(self): def __iter__(self):
self._iterator = iter(self._container) self._iterator = iter(self._container)
return self return self
Expand Down
14 changes: 7 additions & 7 deletions django/template/response.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def render(self):
""" """
retval = self retval = self
if not self._is_rendered: if not self._is_rendered:
self._set_content(self.rendered_content) self.content = self.rendered_content
for post_callback in self._post_render_callbacks: for post_callback in self._post_render_callbacks:
newretval = post_callback(retval) newretval = post_callback(retval)
if newretval is not None: if newretval is not None:
Expand All @@ -119,20 +119,20 @@ def __iter__(self):
'rendered before it can be iterated over.') 'rendered before it can be iterated over.')
return super(SimpleTemplateResponse, self).__iter__() return super(SimpleTemplateResponse, self).__iter__()


def _get_content(self): @property
def content(self):
if not self._is_rendered: if not self._is_rendered:
raise ContentNotRenderedError('The response content must be ' raise ContentNotRenderedError('The response content must be '
'rendered before it can be accessed.') '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 """Sets the content for the response
""" """
super(SimpleTemplateResponse, self)._set_content(value) HttpResponse.content.fset(self, value)

This comment has been minimized.

Copy link
@alex

alex Aug 22, 2012

Member

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

This comment has been minimized.

Copy link
@claudep

claudep Aug 22, 2012

Author Member

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

This comment has been minimized.

Copy link
@charettes
self._is_rendered = True self._is_rendered = True


content = property(_get_content, _set_content)



class TemplateResponse(SimpleTemplateResponse): class TemplateResponse(SimpleTemplateResponse):
rendering_attrs = SimpleTemplateResponse.rendering_attrs + \ rendering_attrs = SimpleTemplateResponse.rendering_attrs + \
Expand Down

0 comments on commit e2b4edd

Please sign in to comment.