Django

Code

Changeset 5184

Show
Ignore:
Timestamp:
05/11/07 01:03:40 (2 years ago)
Author:
mtredinnick
Message:

unicode: First part of the form input changes. Form parameters are now
automatically returned as unicode.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/unicode/django/http/__init__.py

    r5126 r5184  
    44from urllib import urlencode, quote 
    55from django.utils.datastructures import MultiValueDict 
     6from django.utils.encoding import smart_str 
    67 
    78RESERVED_CHARS="!*'();:@&=+$,/?%#[]" 
     
    7576 
    7677class QueryDict(MultiValueDict): 
    77     """A specialized MultiValueDict that takes a query string when initialized. 
    78     This is immutable unless you create a copy of it.""" 
    79     def __init__(self, query_string, mutable=False): 
     78    """ 
     79    A specialized MultiValueDict that takes a query string when initialized. 
     80    This is immutable unless you create a copy of it. 
     81 
     82    Values retrieved from this class are converted from the default encoding to 
     83    unicode (this is done on retrieval, rather than input to avoid breaking 
     84    references or mutating referenced objects). 
     85    """ 
     86    def __init__(self, query_string, mutable=False, encoding=None): 
    8087        MultiValueDict.__init__(self) 
     88        if not encoding: 
     89            # *Important*: do not import settings any earlier because of note 
     90            # in core.handlers.modpython. 
     91            from django.conf import settings 
     92            self.encoding = settings.DEFAULT_CHARSET 
     93        else: 
     94            self.encoding = encoding 
    8195        self._mutable = True 
    8296        for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True 
     
    88102            raise AttributeError, "This QueryDict instance is immutable" 
    89103 
     104    def __getitem__(self, key): 
     105        return str_to_unicode(MultiValueDict.__getitem__(self, key), self.encoding) 
     106 
    90107    def __setitem__(self, key, value): 
    91108        self._assert_mutable() 
    92109        MultiValueDict.__setitem__(self, key, value) 
     110 
     111    def get(self, key, default=None): 
     112        return str_to_unicode(MultiValueDict.get(self, key, default), self.encoding) 
    93113 
    94114    def __copy__(self): 
     
    106126        return result 
    107127 
     128    def getlist(self, key): 
     129        """ 
     130        Returns a copy of the list associated with "key". This isn't a 
     131        reference to the original list because this method converts all the 
     132        values to unicode (without changing the original). 
     133        """ 
     134        return [str_to_unicode(v, self.encoding) for v in MultiValueDict.getlist(self, key)] 
     135 
    108136    def setlist(self, key, list_): 
    109137        self._assert_mutable() 
    110138        MultiValueDict.setlist(self, key, list_) 
    111139 
     140    def setlistdefault(self, key, default_list=()): 
     141        self._assert_mutable() 
     142        if key not in self: 
     143            self.setlist(key, default_list) 
     144        return MultiValueDict.getlist(self, key) 
     145 
    112146    def appendlist(self, key, value): 
    113147        self._assert_mutable() 
     
    120154    def pop(self, key): 
    121155        self._assert_mutable() 
    122         return MultiValueDict.pop(self, key) 
     156        return [str_to_unicode(v, self.encoding) for v in MultiValueDict.pop(self, key)] 
    123157 
    124158    def popitem(self): 
    125159        self._assert_mutable() 
    126         return MultiValueDict.popitem(self) 
     160        key, values = MultiValueDict.popitem(self) 
     161        return str_to_unicode(key, self.encoding), [str_to_unicode(v, self.encoding) for v in values] 
     162 
     163    def keys(self): 
     164        return [str_to_unicode(k, self.encoding) for k in MultiValueDict.keys(self)] 
     165 
     166    def values(self): 
     167        return [str_to_unicode(v, self.encoding) for v in MultiValueDict.values(self)] 
     168 
     169    def items(self): 
     170        return [(str_to_unicode(k, self.encoding), str_to_unicode(v, self.encoding)) for k, v in MultiValueDict.items(self)] 
     171 
     172    def lists(self): 
     173        return [(str_to_unicode(k, self.encoding), [str_to_unicode(v, self.encoding) for v in v_list]) for k, v_list in MultiValueDict.lists(self)] 
    127174 
    128175    def clear(self): 
     
    141188        output = [] 
    142189        for k, list_ in self.lists(): 
    143             output.extend([urlencode({k: v}) for v in list_]) 
     190            k = smart_str(k, self.encoding) 
     191            output.extend([urlencode({k: smart_str(v, self.encoding)}) for v in list_]) 
    144192        return '&'.join(output) 
    145193 
     
    307355        host = request.META.get('HTTP_HOST', '') 
    308356    return host 
     357 
     358# It's neither necessary nor appropriate to use 
     359# django.utils.encoding.smart_unicode for parsing URLs and form inputs. Thus, 
     360# this slightly more restricted function. 
     361def str_to_unicode(s, encoding): 
     362    """ 
     363    Convert basestring objects to unicode, using the given encoding. 
     364 
     365    Returns any non-basestring objects without change. 
     366    """ 
     367    if isinstance(s, str): 
     368        return unicode(s, encoding) 
     369    else: 
     370        return s 
     371 
  • django/branches/unicode/tests/regressiontests/httpwrappers/tests.py

    r5126 r5184  
    1717 
    1818>>> q.get('foo', 'default') 
    19 'default' 
     19u'default' 
    2020 
    2121>>> q.getlist('foo') 
     
    9595 
    9696>>> q['name'] 
    97 'john' 
    98  
    99 >>> q.get('foo', 'default') 
    100 'default' 
     97u'john' 
     98 
     99>>> q.get('foo', 'default') 
     100u'default' 
    101101 
    102102>>> q.get('name', 'default') 
    103 'john' 
     103u'john' 
    104104 
    105105>>> q.getlist('name') 
    106 ['john'] 
     106[u'john'] 
    107107 
    108108>>> q.getlist('foo') 
     
    112112 
    113113>>> q.get('foo', 'default') 
    114 'baz' 
    115  
    116 >>> q.getlist('foo') 
    117 ['bar', 'baz'] 
     114u'baz' 
     115 
     116>>> q.getlist('foo') 
     117[u'bar', u'baz'] 
    118118 
    119119>>> q.appendlist('foo', 'another') 
    120120 
    121121>>> q.getlist('foo') 
    122 ['bar', 'baz', 'another'] 
    123  
    124 >>> q['foo'] 
    125 'another' 
     122[u'bar', u'baz', u'another'] 
     123 
     124>>> q['foo'] 
     125u'another' 
    126126 
    127127>>> q.has_key('foo') 
     
    132132 
    133133>>> q.items() 
    134 [('foo', 'another'), ('name', 'john')] 
     134[(u'foo', u'another'), (u'name', u'john')] 
    135135 
    136136>>> q.lists() 
    137 [('foo', ['bar', 'baz', 'another']), ('name', ['john'])] 
     137[(u'foo', [u'bar', u'baz', u'another']), (u'name', [u'john'])] 
    138138 
    139139>>> q.keys() 
    140 ['foo', 'name'] 
     140[u'foo', u'name'] 
    141141 
    142142>>> q.values() 
    143 ['another', 'john'] 
     143[u'another', u'john'] 
    144144 
    145145>>> len(q) 
     
    150150# Displays last value 
    151151>>> q['foo'] 
    152 'hello' 
     152u'hello' 
    153153 
    154154>>> q.get('foo', 'not available') 
    155 'hello' 
    156  
    157 >>> q.getlist('foo') 
    158 ['bar', 'baz', 'another', 'hello'] 
     155u'hello' 
     156 
     157>>> q.getlist('foo') 
     158[u'bar', u'baz', u'another', u'hello'] 
    159159 
    160160>>> q.pop('foo') 
    161 ['bar', 'baz', 'another', 'hello'] 
     161[u'bar', u'baz', u'another', u'hello'] 
    162162 
    163163>>> q.get('foo', 'not there') 
    164 'not there' 
     164u'not there' 
    165165 
    166166>>> q.setdefault('foo', 'bar') 
    167 'bar' 
    168  
    169 >>> q['foo'] 
    170 'bar' 
    171  
    172 >>> q.getlist('foo') 
    173 ['bar'] 
     167u'bar' 
     168 
     169>>> q['foo'] 
     170u'bar' 
     171 
     172>>> q.getlist('foo') 
     173[u'bar'] 
    174174 
    175175>>> q.urlencode() 
     
    188188 
    189189>>> q['foo'] 
    190 'bar' 
     190u'bar' 
    191191 
    192192>>> q['bar'] 
     
    201201 
    202202>>> q.get('foo', 'default') 
    203 'bar' 
     203u'bar' 
    204204 
    205205>>> q.get('bar', 'default') 
    206 'default' 
    207  
    208 >>> q.getlist('foo') 
    209 ['bar'] 
     206u'default' 
     207 
     208>>> q.getlist('foo') 
     209[u'bar'] 
    210210 
    211211>>> q.getlist('bar') 
     
    235235 
    236236>>> q.items() 
    237 [('foo', 'bar')] 
     237[(u'foo', u'bar')] 
    238238 
    239239>>> q.lists() 
    240 [('foo', ['bar'])] 
     240[(u'foo', [u'bar'])] 
    241241 
    242242>>> q.keys() 
    243 ['foo'] 
     243[u'foo'] 
    244244 
    245245>>> q.values() 
    246 ['bar'] 
     246[u'bar'] 
    247247 
    248248>>> len(q) 
     
    284284 
    285285>>> q['vote'] 
    286 'no' 
     286u'no' 
    287287 
    288288>>> q['something'] = 'bar' 
     
    292292 
    293293>>> q.get('vote', 'default') 
    294 'no' 
    295  
    296 >>> q.get('foo', 'default') 
    297 'default' 
     294u'no' 
     295 
     296>>> q.get('foo', 'default') 
     297u'default' 
    298298 
    299299>>> q.getlist('vote') 
    300 ['yes', 'no'] 
     300[u'yes', u'no'] 
    301301 
    302302>>> q.getlist('foo') 
     
    326326 
    327327>>> q.items() 
    328 [('vote', 'no')] 
     328[(u'vote', u'no')] 
    329329 
    330330>>> q.lists() 
    331 [('vote', ['yes', 'no'])] 
     331[(u'vote', [u'yes', u'no'])] 
    332332 
    333333>>> q.keys() 
    334 ['vote'] 
     334[u'vote'] 
    335335 
    336336>>> q.values() 
    337 ['no'] 
     337[u'no'] 
    338338 
    339339>>> len(q)