Ticket #1539: django.querydict.patch
File django.querydict.patch, 3.5 KB (added by , 19 years ago) |
---|
-
django/utils/datastructures.py
80 80 except IndexError: 81 81 return [] 82 82 83 def _ setitem_list(self, key, value):83 def __setitem__(self, key, value): 84 84 dict.__setitem__(self, key, [value]) 85 __setitem__ = _setitem_list86 85 86 def __copy__(self): 87 return self.__class__(dict.items(self)) 88 89 def __deepcopy__(self, memo={}): 90 import copy 91 result = self.__class__() 92 memo[id(self)] = result 93 for key, value in dict.items(self): 94 dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo)) 95 return result 96 87 97 def get(self, key, default=None): 88 98 "Returns the default value if the requested data doesn't exist" 89 99 try: … … 136 146 137 147 def copy(self): 138 148 "Returns a copy of this object." 139 import copy 140 # Our custom __setitem__ must be disabled for copying machinery. 141 MultiValueDict.__setitem__ = dict.__setitem__ 142 cp = copy.deepcopy(self) 143 MultiValueDict.__setitem__ = MultiValueDict._setitem_list 144 return cp 149 return self.__deepcopy__() 145 150 146 151 def update(self, other_dict): 147 152 "update() extends rather than replaces existing key lists." -
django/utils/httpwrappers.py
66 66 class QueryDict(MultiValueDict): 67 67 """A specialized MultiValueDict that takes a query string when initialized. 68 68 This is immutable unless you create a copy of it.""" 69 def __init__(self, query_string ):69 def __init__(self, query_string, mutable=False): 70 70 MultiValueDict.__init__(self) 71 71 self._mutable = True 72 72 for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True 73 73 self.appendlist(key, value) 74 self._mutable = False74 self._mutable = mutable 75 75 76 76 def _assert_mutable(self): 77 77 if not self._mutable: 78 78 raise AttributeError, "This QueryDict instance is immutable" 79 79 80 def _ setitem_if_mutable(self, key, value):80 def __setitem__(self, key, value): 81 81 self._assert_mutable() 82 82 MultiValueDict.__setitem__(self, key, value) 83 __setitem__ = _setitem_if_mutable84 83 84 def __copy__(self): 85 result = self.__class__('', mutable=True) 86 for key, value in dict.items(self): 87 dict.__setitem__(result, key, value) 88 return result 89 90 def __deepcopy__(self, memo={}): 91 import copy 92 result = self.__class__('', mutable=True) 93 memo[id(self)] = result 94 for key, value in dict.items(self): 95 dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo)) 96 return result 97 85 98 def setlist(self, key, list_): 86 99 self._assert_mutable() 87 100 MultiValueDict.setlist(self, key, list_) … … 112 125 113 126 def copy(self): 114 127 "Returns a mutable copy of this object." 115 import copy 116 # Our custom __setitem__ must be disabled for copying machinery. 117 QueryDict.__setitem__ = dict.__setitem__ 118 cp = copy.deepcopy(self) 119 QueryDict.__setitem__ = QueryDict._setitem_if_mutable 120 cp._mutable = True 121 return cp 128 return self.__deepcopy__() 122 129 123 130 def urlencode(self): 124 131 output = []