Ticket #1539: django.querydict.patch

File django.querydict.patch, 3.5 KB (added by eugene@…, 19 years ago)

A patch for the Django truck.

  • django/utils/datastructures.py

     
    8080        except IndexError:
    8181            return []
    8282
    83     def _setitem_list(self, key, value):
     83    def __setitem__(self, key, value):
    8484        dict.__setitem__(self, key, [value])
    85     __setitem__ = _setitem_list
    8685
     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
    8797    def get(self, key, default=None):
    8898        "Returns the default value if the requested data doesn't exist"
    8999        try:
     
    136146
    137147    def copy(self):
    138148        "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__()
    145150
    146151    def update(self, other_dict):
    147152        "update() extends rather than replaces existing key lists."
  • django/utils/httpwrappers.py

     
    6666class QueryDict(MultiValueDict):
    6767    """A specialized MultiValueDict that takes a query string when initialized.
    6868    This is immutable unless you create a copy of it."""
    69     def __init__(self, query_string):
     69    def __init__(self, query_string, mutable=False):
    7070        MultiValueDict.__init__(self)
    7171        self._mutable = True
    7272        for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
    7373            self.appendlist(key, value)
    74         self._mutable = False
     74        self._mutable = mutable
    7575
    7676    def _assert_mutable(self):
    7777        if not self._mutable:
    7878            raise AttributeError, "This QueryDict instance is immutable"
    7979
    80     def _setitem_if_mutable(self, key, value):
     80    def __setitem__(self, key, value):
    8181        self._assert_mutable()
    8282        MultiValueDict.__setitem__(self, key, value)
    83     __setitem__ = _setitem_if_mutable
    8483
     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
    8598    def setlist(self, key, list_):
    8699        self._assert_mutable()
    87100        MultiValueDict.setlist(self, key, list_)
     
    112125
    113126    def copy(self):
    114127        "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__()
    122129
    123130    def urlencode(self):
    124131        output = []
Back to Top