Permalink
Browse files

Cleaned up the QueryDict implementation.

- Use super().
- Don't poke at internals.
- Don't override methods for no reason.
  • Loading branch information...
alex committed Jul 14, 2012
1 parent 9a25d86 commit 8f002867b2aa414c4f98bbd7992e3b44825b228a
Showing with 18 additions and 30 deletions.
  1. +17 −28 django/http/__init__.py
  2. +0 −1 django/utils/datastructures.py
  3. +1 −1 tests/regressiontests/httpwrappers/tests.py
View
@@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals
+import copy
import datetime
import os
import re
@@ -360,6 +361,7 @@ def xreadlines(self):
def readlines(self):
return list(iter(self))
+
class QueryDict(MultiValueDict):
"""
A specialized MultiValueDict that takes a query string when initialized.
@@ -374,7 +376,7 @@ class QueryDict(MultiValueDict):
_encoding = None
def __init__(self, query_string, mutable=False, encoding=None):
- MultiValueDict.__init__(self)
+ super(QueryDict, self).__init__()
if not encoding:
encoding = settings.DEFAULT_CHARSET
self.encoding = encoding
@@ -401,72 +403,58 @@ def __setitem__(self, key, value):
self._assert_mutable()
key = str_to_unicode(key, self.encoding)
value = str_to_unicode(value, self.encoding)
- MultiValueDict.__setitem__(self, key, value)
+ super(QueryDict, self).__setitem__(key, value)
def __delitem__(self, key):
self._assert_mutable()
super(QueryDict, self).__delitem__(key)
def __copy__(self):
result = self.__class__('', mutable=True, encoding=self.encoding)
- for key, value in dict.items(self):
- dict.__setitem__(result, key, value)
+ for key, value in self.iterlists():
+ result.setlist(key, value)
return result
def __deepcopy__(self, memo):
- import copy
result = self.__class__('', mutable=True, encoding=self.encoding)
memo[id(self)] = result
- for key, value in dict.items(self):
- dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo))
+ for key, value in self.iterlists():
+ result.setlist(copy.deepcopy(key, memo), copy.deepcopy(value, memo))
return result
def setlist(self, key, list_):
self._assert_mutable()
key = str_to_unicode(key, self.encoding)
list_ = [str_to_unicode(elt, self.encoding) for elt in list_]
- MultiValueDict.setlist(self, key, list_)
+ super(QueryDict, self).setlist(key, list_)
- def setlistdefault(self, key, default_list=()):
+ def setlistdefault(self, key, default_list=None):
self._assert_mutable()
- if key not in self:
- self.setlist(key, default_list)
- return MultiValueDict.getlist(self, key)
+ return super(QueryDict, self).setlistdefault(key, default_list)
def appendlist(self, key, value):
self._assert_mutable()
key = str_to_unicode(key, self.encoding)
value = str_to_unicode(value, self.encoding)
- MultiValueDict.appendlist(self, key, value)
-
- def update(self, other_dict):
- self._assert_mutable()
- f = lambda s: str_to_unicode(s, self.encoding)
- if hasattr(other_dict, 'lists'):
- for key, valuelist in other_dict.lists():
- for value in valuelist:
- MultiValueDict.update(self, {f(key): f(value)})
- else:
- d = dict([(f(k), f(v)) for k, v in other_dict.items()])
- MultiValueDict.update(self, d)
+ super(QueryDict, self).appendlist(key, value)
def pop(self, key, *args):
self._assert_mutable()
- return MultiValueDict.pop(self, key, *args)
+ return super(QueryDict, self).pop(key, *args)
def popitem(self):
self._assert_mutable()
- return MultiValueDict.popitem(self)
+ return super(QueryDict, self).popitem()
def clear(self):
self._assert_mutable()
- MultiValueDict.clear(self)
+ super(QueryDict, self).clear()
def setdefault(self, key, default=None):
self._assert_mutable()
key = str_to_unicode(key, self.encoding)
default = str_to_unicode(default, self.encoding)
- return MultiValueDict.setdefault(self, key, default)
+ return super(QueryDict, self).setdefault(key, default)
def copy(self):
"""Returns a mutable copy of this object."""
@@ -499,6 +487,7 @@ def urlencode(self, safe=None):
for v in list_])
return '&'.join(output)
+
def parse_cookie(cookie):
if cookie == '':
return {}
@@ -334,7 +334,6 @@ def setlistdefault(self, key, default_list=None):
if default_list is None:
default_list = []
self.setlist(key, default_list)
- return default_list
return self.getlist(key)
def appendlist(self, key, value):
@@ -189,7 +189,7 @@ def test_pickle(self):
self.assertEqual(q == q1, True)
q = QueryDict('a=b&c=d&a=1')
q1 = pickle.loads(pickle.dumps(q, 2))
- self.assertEqual(q == q1 , True)
+ self.assertEqual(q == q1, True)
def test_update_from_querydict(self):
"""Regression test for #8278: QueryDict.update(QueryDict)"""

0 comments on commit 8f00286

Please sign in to comment.