| 1 |
import urllib |
|---|
| 2 |
from email.Utils import formatdate |
|---|
| 3 |
|
|---|
| 4 |
from django.utils.encoding import smart_str, force_unicode |
|---|
| 5 |
from django.utils.functional import allow_lazy |
|---|
| 6 |
|
|---|
| 7 |
def urlquote(url, safe='/'): |
|---|
| 8 |
""" |
|---|
| 9 |
A version of Python's urllib.quote() function that can operate on unicode |
|---|
| 10 |
strings. The url is first UTF-8 encoded before quoting. The returned string |
|---|
| 11 |
can safely be used as part of an argument to a subsequent iri_to_uri() call |
|---|
| 12 |
without double-quoting occurring. |
|---|
| 13 |
""" |
|---|
| 14 |
return force_unicode(urllib.quote(smart_str(url), safe)) |
|---|
| 15 |
|
|---|
| 16 |
urlquote = allow_lazy(urlquote, unicode) |
|---|
| 17 |
|
|---|
| 18 |
def urlquote_plus(url, safe=''): |
|---|
| 19 |
""" |
|---|
| 20 |
A version of Python's urllib.quote_plus() function that can operate on |
|---|
| 21 |
unicode strings. The url is first UTF-8 encoded before quoting. The |
|---|
| 22 |
returned string can safely be used as part of an argument to a subsequent |
|---|
| 23 |
iri_to_uri() call without double-quoting occurring. |
|---|
| 24 |
""" |
|---|
| 25 |
return force_unicode(urllib.quote_plus(smart_str(url), safe)) |
|---|
| 26 |
urlquote_plus = allow_lazy(urlquote_plus, unicode) |
|---|
| 27 |
|
|---|
| 28 |
def urlencode(query, doseq=0): |
|---|
| 29 |
""" |
|---|
| 30 |
A version of Python's urllib.urlencode() function that can operate on |
|---|
| 31 |
unicode strings. The parameters are first case to UTF-8 encoded strings and |
|---|
| 32 |
then encoded as per normal. |
|---|
| 33 |
""" |
|---|
| 34 |
if hasattr(query, 'items'): |
|---|
| 35 |
query = query.items() |
|---|
| 36 |
return urllib.urlencode( |
|---|
| 37 |
[(smart_str(k), |
|---|
| 38 |
isinstance(v, (list,tuple)) and [smart_str(i) for i in v] or smart_str(v)) |
|---|
| 39 |
for k, v in query], |
|---|
| 40 |
doseq) |
|---|
| 41 |
|
|---|
| 42 |
def cookie_date(epoch_seconds=None): |
|---|
| 43 |
""" |
|---|
| 44 |
Formats the time to ensure compatibility with Netscape's cookie standard. |
|---|
| 45 |
|
|---|
| 46 |
Accepts a floating point number expressed in seconds since the epoch, in |
|---|
| 47 |
UTC - such as that outputted by time.time(). If set to None, defaults to |
|---|
| 48 |
the current time. |
|---|
| 49 |
|
|---|
| 50 |
Outputs a string in the format 'Wdy, DD-Mon-YYYY HH:MM:SS GMT'. |
|---|
| 51 |
""" |
|---|
| 52 |
rfcdate = formatdate(epoch_seconds) |
|---|
| 53 |
return '%s-%s-%s GMT' % (rfcdate[:7], rfcdate[8:11], rfcdate[12:25]) |
|---|
| 54 |
|
|---|
| 55 |
def http_date(epoch_seconds=None): |
|---|
| 56 |
""" |
|---|
| 57 |
Formats the time to match the RFC1123 date format as specified by HTTP |
|---|
| 58 |
RFC2616 section 3.3.1. |
|---|
| 59 |
|
|---|
| 60 |
Accepts a floating point number expressed in seconds since the epoch, in |
|---|
| 61 |
UTC - such as that outputted by time.time(). If set to None, defaults to |
|---|
| 62 |
the current time. |
|---|
| 63 |
|
|---|
| 64 |
Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'. |
|---|
| 65 |
""" |
|---|
| 66 |
rfcdate = formatdate(epoch_seconds) |
|---|
| 67 |
return '%s GMT' % rfcdate[:25] |
|---|
| 68 |
|
|---|
| 69 |
# Base 36 functions: useful for generating compact URLs |
|---|
| 70 |
|
|---|
| 71 |
def base36_to_int(s): |
|---|
| 72 |
""" |
|---|
| 73 |
Convertd a base 36 string to an integer |
|---|
| 74 |
""" |
|---|
| 75 |
return int(s, 36) |
|---|
| 76 |
|
|---|
| 77 |
def int_to_base36(i): |
|---|
| 78 |
""" |
|---|
| 79 |
Converts an integer to a base36 string |
|---|
| 80 |
""" |
|---|
| 81 |
digits = "0123456789abcdefghijklmnopqrstuvwxyz" |
|---|
| 82 |
factor = 0 |
|---|
| 83 |
# Find starting factor |
|---|
| 84 |
while True: |
|---|
| 85 |
factor += 1 |
|---|
| 86 |
if i < 36 ** factor: |
|---|
| 87 |
factor -= 1 |
|---|
| 88 |
break |
|---|
| 89 |
base36 = [] |
|---|
| 90 |
# Construct base36 representation |
|---|
| 91 |
while factor >= 0: |
|---|
| 92 |
j = 36 ** factor |
|---|
| 93 |
base36.append(digits[i / j]) |
|---|
| 94 |
i = i % j |
|---|
| 95 |
factor -= 1 |
|---|
| 96 |
return ''.join(base36) |
|---|