Django

Code

Show
Ignore:
Timestamp:
07/16/08 14:21:15 (6 months ago)
Author:
brosner
Message:

newforms-admin: Fixed #5490 -- Properly quote special characters in primary keys in the admin. Added tests to ensure functionality. This also moves quote and unquote to django/contrib/admin/util.py. Thanks jdetaeye and shanx for all your help.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/contrib/admin/util.py

    r7685 r7935  
    66from django.utils.encoding import force_unicode 
    77from django.utils.translation import ugettext as _ 
     8 
     9 
     10def quote(s): 
     11    """ 
     12    Ensure that primary key values do not confuse the admin URLs by escaping 
     13    any '/', '_' and ':' characters. Similar to urllib.quote, except that the 
     14    quoting is slightly different so that it doesn't get automatically 
     15    unquoted by the Web browser. 
     16    """ 
     17    if not isinstance(s, basestring): 
     18        return s 
     19    res = list(s) 
     20    for i in range(len(res)): 
     21        c = res[i] 
     22        if c in """:/_#?;@&=+$,"<>%\\""": 
     23            res[i] = '_%02X' % ord(c) 
     24    return ''.join(res) 
     25 
     26def unquote(s): 
     27    """ 
     28    Undo the effects of quote(). Based heavily on urllib.unquote(). 
     29    """ 
     30    mychr = chr 
     31    myatoi = int 
     32    list = s.split('_') 
     33    res = [list[0]] 
     34    myappend = res.append 
     35    del list[0] 
     36    for item in list: 
     37        if item[1:2]: 
     38            try: 
     39                myappend(mychr(myatoi(item[:2], 16)) + item[2:]) 
     40            except ValueError: 
     41                myappend('_' + item) 
     42        else: 
     43            myappend('_' + item) 
     44    return "".join(res) 
    845 
    946def _nest_help(obj, depth, val):