Ticket #1539: magic.querydict.patch
File magic.querydict.patch, 3.5 KB (added by , 19 years ago) |
---|
-
django/http/__init__.py
69 69 class QueryDict(MultiValueDict): 70 70 """A specialized MultiValueDict that takes a query string when initialized. 71 71 This is immutable unless you create a copy of it.""" 72 def __init__(self, query_string ):72 def __init__(self, query_string, mutable=False): 73 73 MultiValueDict.__init__(self) 74 74 self._mutable = True 75 75 for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True 76 76 self.appendlist(key, value) 77 self._mutable = False77 self._mutable = mutable 78 78 79 79 def _assert_mutable(self): 80 80 if not self._mutable: 81 81 raise AttributeError, "This QueryDict instance is immutable" 82 82 83 def _ setitem_if_mutable(self, key, value):83 def __setitem__(self, key, value): 84 84 self._assert_mutable() 85 85 MultiValueDict.__setitem__(self, key, value) 86 __setitem__ = _setitem_if_mutable87 86 87 def __copy__(self): 88 result = self.__class__('', mutable=True) 89 for key, value in dict.items(self): 90 dict.__setitem__(result, key, value) 91 return result 92 93 def __deepcopy__(self, memo={}): 94 import copy 95 result = self.__class__('', mutable=True) 96 memo[id(self)] = result 97 for key, value in dict.items(self): 98 dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo)) 99 return result 100 88 101 def setlist(self, key, list_): 89 102 self._assert_mutable() 90 103 MultiValueDict.setlist(self, key, list_) … … 115 128 116 129 def copy(self): 117 130 "Returns a mutable copy of this object." 118 import copy 119 # Our custom __setitem__ must be disabled for copying machinery. 120 QueryDict.__setitem__ = dict.__setitem__ 121 cp = copy.deepcopy(self) 122 QueryDict.__setitem__ = QueryDict._setitem_if_mutable 123 cp._mutable = True 124 return cp 131 return self.__deepcopy__() 125 132 126 133 def urlencode(self): 127 134 output = [] -
django/utils/datastructures.py
117 117 except IndexError: 118 118 return [] 119 119 120 def _ setitem_list(self, key, value):120 def __setitem__(self, key, value): 121 121 dict.__setitem__(self, key, [value]) 122 __setitem__ = _setitem_list123 122 123 def __copy__(self): 124 return self.__class__(dict.items(self)) 125 126 def __deepcopy__(self, memo={}): 127 import copy 128 result = self.__class__() 129 memo[id(self)] = result 130 for key, value in dict.items(self): 131 dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo)) 132 return result 133 124 134 def get(self, key, default=None): 125 135 "Returns the default value if the requested data doesn't exist" 126 136 try: … … 173 183 174 184 def copy(self): 175 185 "Returns a copy of this object." 176 import copy 177 # Our custom __setitem__ must be disabled for copying machinery. 178 MultiValueDict.__setitem__ = dict.__setitem__ 179 cp = copy.deepcopy(self) 180 MultiValueDict.__setitem__ = MultiValueDict._setitem_list 181 return cp 186 return self.__deepcopy__() 182 187 183 188 def update(self, other_dict): 184 189 "update() extends rather than replaces existing key lists."