Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Used the decorator syntax for properties in django.http

  • Loading branch information...
commit e2b4eddc11b391bc0047032776e9ca7c2f9328b3 1 parent dfe63a5
Claude Paroz authored August 22, 2012
44  django/http/__init__.py
@@ -243,7 +243,12 @@ def is_secure(self):
243 243
     def is_ajax(self):
244 244
         return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
245 245
 
246  
-    def _set_encoding(self, val):
  246
+    @property
  247
+    def encoding(self):
  248
+        return self._encoding
  249
+
  250
+    @encoding.setter
  251
+    def encoding(self, val):
247 252
         """
248 253
         Sets the encoding used for GET/POST accesses. If the GET or POST
249 254
         dictionary has already been created, it is removed and recreated on the
@@ -255,27 +260,22 @@ def _set_encoding(self, val):
255 260
         if hasattr(self, '_post'):
256 261
             del self._post
257 262
 
258  
-    def _get_encoding(self):
259  
-        return self._encoding
260  
-
261  
-    encoding = property(_get_encoding, _set_encoding)
262  
-
263 263
     def _initialize_handlers(self):
264 264
         self._upload_handlers = [uploadhandler.load_handler(handler, self)
265 265
                                  for handler in settings.FILE_UPLOAD_HANDLERS]
266 266
 
267  
-    def _set_upload_handlers(self, upload_handlers):
268  
-        if hasattr(self, '_files'):
269  
-            raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
270  
-        self._upload_handlers = upload_handlers
271  
-
272  
-    def _get_upload_handlers(self):
  267
+    @property
  268
+    def upload_handlers(self):
273 269
         if not self._upload_handlers:
274 270
             # If there are no upload handlers defined, initialize them from settings.
275 271
             self._initialize_handlers()
276 272
         return self._upload_handlers
277 273
 
278  
-    upload_handlers = property(_get_upload_handlers, _set_upload_handlers)
  274
+    @upload_handlers.setter
  275
+    def upload_handlers(self, upload_handlers):
  276
+        if hasattr(self, '_files'):
  277
+            raise AttributeError("You cannot set the upload handlers after the upload has been processed.")
  278
+        self._upload_handlers = upload_handlers
279 279
 
280 280
     def parse_file_upload(self, META, post_data):
281 281
         """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
@@ -397,16 +397,16 @@ def __init__(self, query_string, mutable=False, encoding=None):
397 397
                                 force_text(value, encoding, errors='replace'))
398 398
         self._mutable = mutable
399 399
 
400  
-    def _get_encoding(self):
  400
+    @property
  401
+    def encoding(self):
401 402
         if self._encoding is None:
402 403
             self._encoding = settings.DEFAULT_CHARSET
403 404
         return self._encoding
404 405
 
405  
-    def _set_encoding(self, value):
  406
+    @encoding.setter
  407
+    def encoding(self, value):
406 408
         self._encoding = value
407 409
 
408  
-    encoding = property(_get_encoding, _set_encoding)
409  
-
410 410
     def _assert_mutable(self):
411 411
         if not self._mutable:
412 412
             raise AttributeError("This QueryDict instance is immutable")
@@ -539,7 +539,7 @@ def __init__(self, content='', content_type=None, status=None,
539 539
         if not content_type:
540 540
             content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
541 541
                     self._charset)
542  
-        # content is a bytestring. See _get_content / _set_content.
  542
+        # content is a bytestring. See the content property methods.
543 543
         self.content = content
544 544
         self.cookies = SimpleCookie()
545 545
         if status:
@@ -669,7 +669,8 @@ def delete_cookie(self, key, path='/', domain=None):
669 669
         self.set_cookie(key, max_age=0, path=path, domain=domain,
670 670
                         expires='Thu, 01-Jan-1970 00:00:00 GMT')
671 671
 
672  
-    def _get_content(self):
  672
+    @property
  673
+    def content(self):
673 674
         if self.has_header('Content-Encoding'):
674 675
             def make_bytes(value):
675 676
                 if isinstance(value, int):
@@ -681,7 +682,8 @@ def make_bytes(value):
681 682
             return b''.join(make_bytes(e) for e in self._container)
682 683
         return b''.join(smart_bytes(e, self._charset) for e in self._container)
683 684
 
684  
-    def _set_content(self, value):
  685
+    @content.setter
  686
+    def content(self, value):
685 687
         if hasattr(value, '__iter__') and not isinstance(value, (bytes, six.string_types)):
686 688
             self._container = value
687 689
             self._base_content_is_iter = True
@@ -689,8 +691,6 @@ def _set_content(self, value):
689 691
             self._container = [value]
690 692
             self._base_content_is_iter = False
691 693
 
692  
-    content = property(_get_content, _set_content)
693  
-
694 694
     def __iter__(self):
695 695
         self._iterator = iter(self._container)
696 696
         return self
14  django/template/response.py
@@ -102,7 +102,7 @@ def render(self):
102 102
         """
103 103
         retval = self
104 104
         if not self._is_rendered:
105  
-            self._set_content(self.rendered_content)
  105
+            self.content = self.rendered_content
106 106
             for post_callback in self._post_render_callbacks:
107 107
                 newretval = post_callback(retval)
108 108
                 if newretval is not None:
@@ -119,20 +119,20 @@ def __iter__(self):
119 119
                                           'rendered before it can be iterated over.')
120 120
         return super(SimpleTemplateResponse, self).__iter__()
121 121
 
122  
-    def _get_content(self):
  122
+    @property
  123
+    def content(self):
123 124
         if not self._is_rendered:
124 125
             raise ContentNotRenderedError('The response content must be '
125 126
                                           'rendered before it can be accessed.')
126  
-        return super(SimpleTemplateResponse, self)._get_content()
  127
+        return super(SimpleTemplateResponse, self).content
127 128
 
128  
-    def _set_content(self, value):
  129
+    @content.setter
  130
+    def content(self, value):
129 131
         """Sets the content for the response
130 132
         """
131  
-        super(SimpleTemplateResponse, self)._set_content(value)
  133
+        HttpResponse.content.fset(self, value)
132 134
         self._is_rendered = True
133 135
 
134  
-    content = property(_get_content, _set_content)
135  
-
136 136
 
137 137
 class TemplateResponse(SimpleTemplateResponse):
138 138
     rendering_attrs = SimpleTemplateResponse.rendering_attrs + \

0 notes on commit e2b4edd

Alex Gaynor

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

Claude Paroz

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

Please sign in to comment.
Something went wrong with that request. Please try again.