Commit ebaa51cb authored by Chris McDonough's avatar Chris McDonough

Added 'url_unquote' and 'url_unquote_plus' modifiers to DTML as

well as made these functions available in PythonScripts via
the 'standard' module.
parent 40cb7804
...@@ -114,6 +114,12 @@ __doc__='''Variable insertion parameters ...@@ -114,6 +114,12 @@ __doc__='''Variable insertion parameters
space characters with '+'. This is needed for building space characters with '+'. This is needed for building
query strings in some cases. query strings in some cases.
'url_unquote' -- convert HTML character entities in strings
back to their real values.
'url_unquote_plus' -- like url_unquote, but also
replace '+' characters with spaces.
'sql_quote' -- Convert single quotes to pairs of single 'sql_quote' -- Convert single quotes to pairs of single
quotes. This is needed to safely include values in quotes. This is needed to safely include values in
Standard Query Language (SQL) strings. Standard Query Language (SQL) strings.
...@@ -145,12 +151,12 @@ Evaluating expressions without rendering results ...@@ -145,12 +151,12 @@ Evaluating expressions without rendering results
''' # ' ''' # '
__rcs_id__='$Id: DT_Var.py,v 1.54 2002/08/01 16:00:39 mj Exp $' __rcs_id__='$Id: DT_Var.py,v 1.55 2002/08/14 15:46:57 chrism Exp $'
__version__='$Revision: 1.54 $'[11:-2] __version__='$Revision: 1.55 $'[11:-2]
from DT_Util import parse_params, name_param, str, ustr from DT_Util import parse_params, name_param, str, ustr
import os, string, re, sys import os, string, re, sys
from urllib import quote, quote_plus from urllib import quote, quote_plus, unquote, unquote_plus
from cgi import escape from cgi import escape
from html_quote import html_quote # for import by other modules, dont remove! from html_quote import html_quote # for import by other modules, dont remove!
from types import StringType from types import StringType
...@@ -167,7 +173,8 @@ class Var: ...@@ -167,7 +173,8 @@ class Var:
capitalize=1, spacify=1, null='', fmt='s', capitalize=1, spacify=1, null='', fmt='s',
size=0, etc='...', thousands_commas=1, size=0, etc='...', thousands_commas=1,
html_quote=1, url_quote=1, sql_quote=1, html_quote=1, url_quote=1, sql_quote=1,
url_quote_plus=1, missing='', url_quote_plus=1, url_unquote=1,
url_unquote_plus=1,missing='',
newline_to_br=1, url=1) newline_to_br=1, url=1)
self.args=args self.args=args
...@@ -333,6 +340,11 @@ def url_quote(v, name='(Unknown name)', md={}): ...@@ -333,6 +340,11 @@ def url_quote(v, name='(Unknown name)', md={}):
def url_quote_plus(v, name='(Unknown name)', md={}): def url_quote_plus(v, name='(Unknown name)', md={}):
return quote_plus(str(v)) return quote_plus(str(v))
def url_unquote(v, name='(Unknown name)', md={}):
return unquote(str(v))
def url_unquote_plus(v, name='(Unknown name)', md={}):
return unquote_plus(str(v))
def newline_to_br(v, name='(Unknown name)', md={}): def newline_to_br(v, name='(Unknown name)', md={}):
# Unsafe data is explicitly quoted here; we don't expect this to be HTML # Unsafe data is explicitly quoted here; we don't expect this to be HTML
...@@ -422,6 +434,8 @@ special_formats={ ...@@ -422,6 +434,8 @@ special_formats={
'html-quote': html_quote, 'html-quote': html_quote,
'url-quote': url_quote, 'url-quote': url_quote,
'url-quote-plus': url_quote_plus, 'url-quote-plus': url_quote_plus,
'url-unquote': url_unquote,
'url-unquote-plus': url_unquote_plus,
'multi-line': newline_to_br, 'multi-line': newline_to_br,
'comma-numeric': thousands_commas, 'comma-numeric': thousands_commas,
'dollars-with-commas': whole_dollars_with_commas, 'dollars-with-commas': whole_dollars_with_commas,
...@@ -434,7 +448,7 @@ def spacify(val): ...@@ -434,7 +448,7 @@ def spacify(val):
modifiers=(html_quote, url_quote, url_quote_plus, newline_to_br, modifiers=(html_quote, url_quote, url_quote_plus, newline_to_br,
string.lower, string.upper, string.capitalize, spacify, string.lower, string.upper, string.capitalize, spacify,
thousands_commas, sql_quote) thousands_commas, sql_quote, url_unquote, url_unquote_plus)
modifiers=map(lambda f: (f.__name__, f), modifiers) modifiers=map(lambda f: (f.__name__, f), modifiers)
class Comment: class Comment:
......
...@@ -13,8 +13,8 @@ ...@@ -13,8 +13,8 @@
"""Document Template Tests """Document Template Tests
""" """
__rcs_id__='$Id: testDTML.py,v 1.12 2002/05/23 13:19:16 chrisw Exp $' __rcs_id__='$Id: testDTML.py,v 1.13 2002/08/14 15:46:58 chrism Exp $'
__version__='$Revision: 1.12 $'[11:-2] __version__='$Revision: 1.13 $'[11:-2]
import sys, os import sys, os
import unittest import unittest
...@@ -248,6 +248,45 @@ class DTMLTests (unittest.TestCase): ...@@ -248,6 +248,45 @@ class DTMLTests (unittest.TestCase):
res = html(spam=42) + html(spam=None) res = html(spam=42) + html(spam=None)
assert res == expected, res assert res == expected, res
def testUrlUnquote(self):
html1 = self.doc_class(
"""
<dtml-var expr="'http%3A//www.zope.org%3Fa%3Db%20123'" fmt=url-unquote>
"""
)
html2 = self.doc_class(
"""
<dtml-var expr="'http%3A%2F%2Fwww.zope.org%3Fa%3Db+123'" fmt=url-unquote-plus>
"""
)
expected = (
"""
http://www.zope.org?a=b 123
"""
)
self.assertEqual(html1(), expected)
self.assertEqual(html2(), expected)
html1 = self.doc_class(
"""
<dtml-var expr="'http%3A//www.zope.org%3Fa%3Db%20123'" url_unquote>
"""
)
html2 = self.doc_class(
"""
<dtml-var expr="'http%3A%2F%2Fwww.zope.org%3Fa%3Db+123'" url_unquote_plus>
"""
)
expected = (
"""
http://www.zope.org?a=b 123
"""
)
self.assertEqual(html1(), expected)
self.assertEqual(html2(), expected)
def test_fmt(self): def test_fmt(self):
html=self.doc_class( html=self.doc_class(
""" """
......
...@@ -85,6 +85,12 @@ var: Inserts a variable ...@@ -85,6 +85,12 @@ var: Inserts a variable
url_quote_plus -- URL quotes character, like 'url_quote' but also url_quote_plus -- URL quotes character, like 'url_quote' but also
converts spaces to plus signs. converts spaces to plus signs.
url_unquote -- convert HTML character entities in strings back to
their real values.
url_unquote_plus -- like url_unquote, but also replace "+"
characters with spaces.
sql_quote -- Converts single quotes to pairs of single sql_quote -- Converts single quotes to pairs of single
quotes. This is needed to safely include values in SQL strings. quotes. This is needed to safely include values in SQL strings.
......
...@@ -70,6 +70,28 @@ def url_quote_plus(s): ...@@ -70,6 +70,28 @@ def url_quote_plus(s):
""" """
def url_unquote(s):
"""
Convert HTML character entities in strings back to their real values.
See Also
"Python 'urllib'
module":http://www.python.org/doc/current/lib/module-urllib.html
'url_unquote' function.
"""
def url_unquote(s):
"""
Like url_unquote, but also replace '+' characters with spaces.
See Also
"Python 'urllib'
module":http://www.python.org/doc/current/lib/module-urllib.html
'url_unquote_plus' function.
"""
def newline_to_br(s): def newline_to_br(s):
""" """
Convert newlines and carriage-return and newline combinations to Convert newlines and carriage-return and newline combinations to
......
...@@ -18,7 +18,7 @@ Scripts. It can be accessed from Python with the statement ...@@ -18,7 +18,7 @@ Scripts. It can be accessed from Python with the statement
"import Products.PythonScripts.standard" "import Products.PythonScripts.standard"
""" """
__version__='$Revision: 1.7 $'[11:-2] __version__='$Revision: 1.8 $'[11:-2]
from AccessControl import ModuleSecurityInfo, getSecurityManager from AccessControl import ModuleSecurityInfo, getSecurityManager
security = ModuleSecurityInfo() security = ModuleSecurityInfo()
...@@ -27,10 +27,12 @@ security.declarePublic('special_formats', 'whole_dollars', ...@@ -27,10 +27,12 @@ security.declarePublic('special_formats', 'whole_dollars',
'dollars_and_cents', 'structured_text', 'dollars_and_cents', 'structured_text',
'sql_quote', 'html_quote', 'url_quote', 'sql_quote', 'html_quote', 'url_quote',
'url_quote_plus', 'newline_to_br', 'url_quote_plus', 'newline_to_br',
'thousands_commas') 'thousands_commas', 'url_unquote',
'url_unquote_plus')
from DocumentTemplate.DT_Var import special_formats, \ from DocumentTemplate.DT_Var import special_formats, \
whole_dollars, dollars_and_cents, structured_text, sql_quote, \ whole_dollars, dollars_and_cents, structured_text, sql_quote, \
html_quote, url_quote, url_quote_plus, newline_to_br, thousands_commas html_quote, url_quote, url_quote_plus, newline_to_br, thousands_commas, \
url_unquote, url_unquote_plus
from Globals import HTML from Globals import HTML
from AccessControl.DTML import RestrictedDTML from AccessControl.DTML import RestrictedDTML
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment