Cleaned up the QueryDict implementation.

- Use super().
- Don't poke at internals.
- Don't override methods for no reason.
This commit is contained in:
Alex Gaynor 2012-07-14 14:07:11 -07:00
parent 9a25d8618a
commit 8f002867b2
3 changed files with 18 additions and 30 deletions

View File

@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
import copy
import datetime import datetime
import os import os
import re import re
@ -360,6 +361,7 @@ class HttpRequest(object):
def readlines(self): def readlines(self):
return list(iter(self)) return list(iter(self))
class QueryDict(MultiValueDict): class QueryDict(MultiValueDict):
""" """
A specialized MultiValueDict that takes a query string when initialized. A specialized MultiValueDict that takes a query string when initialized.
@ -374,7 +376,7 @@ class QueryDict(MultiValueDict):
_encoding = None _encoding = None
def __init__(self, query_string, mutable=False, encoding=None): def __init__(self, query_string, mutable=False, encoding=None):
MultiValueDict.__init__(self) super(QueryDict, self).__init__()
if not encoding: if not encoding:
encoding = settings.DEFAULT_CHARSET encoding = settings.DEFAULT_CHARSET
self.encoding = encoding self.encoding = encoding
@ -401,7 +403,7 @@ class QueryDict(MultiValueDict):
self._assert_mutable() self._assert_mutable()
key = str_to_unicode(key, self.encoding) key = str_to_unicode(key, self.encoding)
value = str_to_unicode(value, self.encoding) value = str_to_unicode(value, self.encoding)
MultiValueDict.__setitem__(self, key, value) super(QueryDict, self).__setitem__(key, value)
def __delitem__(self, key): def __delitem__(self, key):
self._assert_mutable() self._assert_mutable()
@ -409,64 +411,50 @@ class QueryDict(MultiValueDict):
def __copy__(self): def __copy__(self):
result = self.__class__('', mutable=True, encoding=self.encoding) result = self.__class__('', mutable=True, encoding=self.encoding)
for key, value in dict.items(self): for key, value in self.iterlists():
dict.__setitem__(result, key, value) result.setlist(key, value)
return result return result
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
import copy
result = self.__class__('', mutable=True, encoding=self.encoding) result = self.__class__('', mutable=True, encoding=self.encoding)
memo[id(self)] = result memo[id(self)] = result
for key, value in dict.items(self): for key, value in self.iterlists():
dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo)) result.setlist(copy.deepcopy(key, memo), copy.deepcopy(value, memo))
return result return result
def setlist(self, key, list_): def setlist(self, key, list_):
self._assert_mutable() self._assert_mutable()
key = str_to_unicode(key, self.encoding) key = str_to_unicode(key, self.encoding)
list_ = [str_to_unicode(elt, self.encoding) for elt in list_] 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() self._assert_mutable()
if key not in self: return super(QueryDict, self).setlistdefault(key, default_list)
self.setlist(key, default_list)
return MultiValueDict.getlist(self, key)
def appendlist(self, key, value): def appendlist(self, key, value):
self._assert_mutable() self._assert_mutable()
key = str_to_unicode(key, self.encoding) key = str_to_unicode(key, self.encoding)
value = str_to_unicode(value, self.encoding) value = str_to_unicode(value, self.encoding)
MultiValueDict.appendlist(self, key, value) super(QueryDict, self).appendlist(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)
def pop(self, key, *args): def pop(self, key, *args):
self._assert_mutable() self._assert_mutable()
return MultiValueDict.pop(self, key, *args) return super(QueryDict, self).pop(key, *args)
def popitem(self): def popitem(self):
self._assert_mutable() self._assert_mutable()
return MultiValueDict.popitem(self) return super(QueryDict, self).popitem()
def clear(self): def clear(self):
self._assert_mutable() self._assert_mutable()
MultiValueDict.clear(self) super(QueryDict, self).clear()
def setdefault(self, key, default=None): def setdefault(self, key, default=None):
self._assert_mutable() self._assert_mutable()
key = str_to_unicode(key, self.encoding) key = str_to_unicode(key, self.encoding)
default = str_to_unicode(default, 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): def copy(self):
"""Returns a mutable copy of this object.""" """Returns a mutable copy of this object."""
@ -499,6 +487,7 @@ class QueryDict(MultiValueDict):
for v in list_]) for v in list_])
return '&'.join(output) return '&'.join(output)
def parse_cookie(cookie): def parse_cookie(cookie):
if cookie == '': if cookie == '':
return {} return {}

View File

@ -334,7 +334,6 @@ class MultiValueDict(dict):
if default_list is None: if default_list is None:
default_list = [] default_list = []
self.setlist(key, default_list) self.setlist(key, default_list)
return default_list
return self.getlist(key) return self.getlist(key)
def appendlist(self, key, value): def appendlist(self, key, value):

View File

@ -189,7 +189,7 @@ class QueryDictTests(unittest.TestCase):
self.assertEqual(q == q1, True) self.assertEqual(q == q1, True)
q = QueryDict('a=b&c=d&a=1') q = QueryDict('a=b&c=d&a=1')
q1 = pickle.loads(pickle.dumps(q, 2)) q1 = pickle.loads(pickle.dumps(q, 2))
self.assertEqual(q == q1 , True) self.assertEqual(q == q1, True)
def test_update_from_querydict(self): def test_update_from_querydict(self):
"""Regression test for #8278: QueryDict.update(QueryDict)""" """Regression test for #8278: QueryDict.update(QueryDict)"""