| | 8 | |
|---|
| | 9 | |
|---|
| | 10 | def 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 | |
|---|
| | 26 | def 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) |
|---|