[Zope3-checkins] SVN: Zope3/trunk/ - New path adapter: 'url' for providing inline url quoting capabilities

Christian Theune ct at gocept.com
Thu Jun 17 11:52:09 EDT 2004


Log message for revision 25886:
 - New path adapter: 'url' for providing inline url quoting capabilities
   from page templates. 



-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2004-06-17 14:54:41 UTC (rev 25885)
+++ Zope3/trunk/doc/CHANGES.txt	2004-06-17 15:52:08 UTC (rev 25886)
@@ -24,6 +24,13 @@
         Try e.g. http://localhost:8080/++debug++source,tal/@@contents.html
         and view the source of the resulting page.
 
+      - Added 'url' adapter for IPathAdapter. It provides quoting mechanisms
+        to strings (and string-like objects) from within ZPT, e.g:
+
+        tal:attributes="href string:login.html?nextURL=${request/URL/url:quote}
+        
+        Other functions available are quote_plus, unquote, unquote_plus.
+        
     Restructuring
 
       - Templated Pages are now called ZPT Pages.

Modified: Zope3/trunk/src/zope/app/pagetemplate/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/configure.zcml	2004-06-17 14:54:41 UTC (rev 25885)
+++ Zope3/trunk/src/zope/app/pagetemplate/configure.zcml	2004-06-17 15:52:08 UTC (rev 25886)
@@ -18,6 +18,12 @@
       name="zope" 
       />
 
+  <adapter
+      for="*"
+      provides="zope.app.traversing.interfaces.IPathAdapter"
+      factory=".urlquote.URLQuote"
+      name="url"/> 
+
  <content class="zope.tales.tales.Iterator">
     <allow interface="zope.tales.interfaces.ITALESIterator" />
  </content>

Modified: Zope3/trunk/src/zope/app/pagetemplate/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/interfaces.py	2004-06-17 14:54:41 UTC (rev 25885)
+++ Zope3/trunk/src/zope/app/pagetemplate/interfaces.py	2004-06-17 15:52:08 UTC (rev 25886)
@@ -17,6 +17,7 @@
 """
 from zope.app.dublincore.interfaces import IDCDescriptiveProperties
 from zope.app.dublincore.interfaces import IDCTimes
+from zope.interface import Interface
 
 class IZopeTalesAPI(IDCDescriptiveProperties, IDCTimes):
 
@@ -39,3 +40,17 @@
         (e.g. the object has no size), an empty string is returned.
         """
     
+class IURLQuote(Interface):
+
+    def quote():
+        """Return the objects URL quote representation."""
+
+    def quote_plus():
+        """Return the objects URL quote_plus representation."""
+
+    def unquote():
+        """Return the objects URL unquote representation."""
+
+    def unquote_plus():
+        """Return the objects URL unquote_plus  representation."""
+

Added: Zope3/trunk/src/zope/app/pagetemplate/tests/test_urlquote.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/tests/test_urlquote.py	2004-06-17 14:54:41 UTC (rev 25885)
+++ Zope3/trunk/src/zope/app/pagetemplate/tests/test_urlquote.py	2004-06-17 15:52:08 UTC (rev 25886)
@@ -0,0 +1,62 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""URLQuote Tests
+
+I kept the tests quite small, just covering that the functions actually do
+something (and don't really scramble stuff). We are relying on the python urllib
+to be functional to avoid test duplication.
+
+$Id: test_talesapi.py 25177 2004-06-02 13:17:31Z jim $
+"""
+
+from zope.testing.doctestunit import DocTestSuite
+from zope.app.pagetemplate.urlquote import URLQuote
+
+
+class TestObject(object):
+
+    def __str__(self):
+        return "www.google.de"
+
+def quote_simple():
+    """
+    >>> q = URLQuote(u"www.google.de")
+    >>> q.quote()
+    u'www.google.de'
+    >>> q.unquote()
+    u'www.google.de'
+    >>> q.quote_plus()
+    u'www.google.de'
+    >>> q.unquote_plus()
+    u'www.google.de'
+    """
+
+def quote_cast_needed():
+    """
+    >>> q = URLQuote(TestObject())
+    >>> q.quote()
+    'www.google.de'
+    >>> q.unquote()
+    'www.google.de'
+    >>> q.quote_plus()
+    'www.google.de'
+    >>> q.unquote_plus()
+    'www.google.de'
+    """
+
+def test_suite():
+    return DocTestSuite()
+
+if __name__ == '__main__':
+    unittest.main()

Added: Zope3/trunk/src/zope/app/pagetemplate/urlquote.py
===================================================================
--- Zope3/trunk/src/zope/app/pagetemplate/urlquote.py	2004-06-17 14:54:41 UTC (rev 25885)
+++ Zope3/trunk/src/zope/app/pagetemplate/urlquote.py	2004-06-17 15:52:08 UTC (rev 25886)
@@ -0,0 +1,48 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""URL quoting for ZPT
+
+$Id: simpleviewclass.py 25177 2004-06-02 13:17:31Z jim $
+"""
+
+import urllib
+from zope.interface import implements
+from zope.app.traversing.interfaces import IPathAdapter
+
+class URLQuote(object):
+
+    __used_for__ = basestring
+    implements(IPathAdapter)
+
+    def __init__(self, context):
+        if not isinstance(context, basestring):
+            context = str(context)
+        self.context = context
+
+    def quote(self):
+        """Return the objects URL quote representation."""
+        return urllib.quote(self.context)
+
+    def quote_plus(self):
+        """Return the objects URL quote_plus representation."""
+        return urllib.quote_plus(self.context)
+
+    def unquote(self):
+        """Return the objects URL unquote representation."""
+        return urllib.unquote(self.context)
+
+    def unquote_plus(self):
+        """Return the objects URL unquote_plus representation."""
+        return urllib.unquote_plus(self.context)
+

Modified: Zope3/trunk/src/zope/app/rotterdam/template.pt
===================================================================
--- Zope3/trunk/src/zope/app/rotterdam/template.pt	2004-06-17 14:54:41 UTC (rev 25885)
+++ Zope3/trunk/src/zope/app/rotterdam/template.pt	2004-06-17 15:52:08 UTC (rev 25886)
@@ -49,12 +49,12 @@
           </tal:block>
         </metal:block>
           <a href=""
-            tal:attributes="href string:@@logout.html?nextURL=${request/URL}"
+            tal:attributes="href string:@@logout.html?nextURL=${request/URL/url:quote}"
             tal:condition="python: hasattr(request.principal, 'getLogin')"
             i18n:translate="">
             [Logout]</a>
           <a href=""
-            tal:attributes="href string:@@login.html?nextURL=${request/URL}"
+            tal:attributes="href string:@@login.html?nextURL=${request/URL/url:quote}"
             tal:condition="python: not hasattr(request.principal, 'getLogin')"
             i18n:translate="">
             [Login]</a>




More information about the Zope3-Checkins mailing list