Ticket #1539: magic.querydict.patch

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

A patch for the magic-removal branch.

  • django/http/__init__.py

     
    6969class QueryDict(MultiValueDict):
    7070    """A specialized MultiValueDict that takes a query string when initialized.
    7171    This is immutable unless you create a copy of it."""
    72     def __init__(self, query_string):
     72    def __init__(self, query_string, mutable=False):
    7373        MultiValueDict.__init__(self)
    7474        self._mutable = True
    7575        for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
    7676            self.appendlist(key, value)
    77         self._mutable = False
     77        self._mutable = mutable
    7878
    7979    def _assert_mutable(self):
    8080        if not self._mutable:
    8181            raise AttributeError, "This QueryDict instance is immutable"
    8282
    83     def _setitem_if_mutable(self, key, value):
     83    def __setitem__(self, key, value):
    8484        self._assert_mutable()
    8585        MultiValueDict.__setitem__(self, key, value)
    86     __setitem__ = _setitem_if_mutable
    8786
     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
    88101    def setlist(self, key, list_):
    89102        self._assert_mutable()
    90103        MultiValueDict.setlist(self, key, list_)
     
    115128
    116129    def copy(self):
    117130        "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__()
    125132
    126133    def urlencode(self):
    127134        output = []
  • django/utils/datastructures.py

     
    117117        except IndexError:
    118118            return []
    119119
    120     def _setitem_list(self, key, value):
     120    def __setitem__(self, key, value):
    121121        dict.__setitem__(self, key, [value])
    122     __setitem__ = _setitem_list
    123122
     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
    124134    def get(self, key, default=None):
    125135        "Returns the default value if the requested data doesn't exist"
    126136        try:
     
    173183
    174184    def copy(self):
    175185        "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__()
    182187
    183188    def update(self, other_dict):
    184189        "update() extends rather than replaces existing key lists."
Back to Top