Django

Code

root/django/branches/newforms-admin/django/utils/http.py

Revision 6656, 2.4 kB (checked in by jkocherhans, 1 year ago)

newforms-admin: Merged to [6652]

  • Property svn:eol-style set to native
Line 
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]
Note: See TracBrowser for help on using the browser.