2
from email.Utils import formatdate
4
from django.utils.encoding import smart_str, force_unicode
5
from django.utils.functional import allow_lazy
7
def urlquote(url, safe='/'):
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.
14
return force_unicode(urllib.quote(smart_str(url), safe))
16
urlquote = allow_lazy(urlquote, unicode)
18
def urlquote_plus(url, safe=''):
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.
25
return force_unicode(urllib.quote_plus(smart_str(url), safe))
26
urlquote_plus = allow_lazy(urlquote_plus, unicode)
28
def urlencode(query, doseq=0):
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.
34
if hasattr(query, 'items'):
36
return urllib.urlencode(
38
isinstance(v, (list,tuple)) and [smart_str(i) for i in v] or smart_str(v))
42
def cookie_date(epoch_seconds=None):
44
Formats the time to ensure compatibility with Netscape's cookie standard.
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
50
Outputs a string in the format 'Wdy, DD-Mon-YYYY HH:MM:SS GMT'.
52
rfcdate = formatdate(epoch_seconds)
53
return '%s-%s-%s GMT' % (rfcdate[:7], rfcdate[8:11], rfcdate[12:25])
55
def http_date(epoch_seconds=None):
57
Formats the time to match the RFC1123 date format as specified by HTTP
58
RFC2616 section 3.3.1.
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
64
Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'.
66
rfcdate = formatdate(epoch_seconds)
67
return '%s GMT' % rfcdate[:25]
69
# Base 36 functions: useful for generating compact URLs
73
Convertd a base 36 string to an integer
79
Converts an integer to a base36 string
81
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
83
# Find starting factor
90
# Construct base36 representation
93
base36.append(digits[i / j])
96
return ''.join(base36)