changeset 102:a54cebaab73e

move make / parse query string functions to wikiutil, make unicode-safe imported from: moin--main--1.5--patch-104
author Thomas Waldmann <tw@waldmann-edv.de>
date Sat, 15 Oct 2005 18:03:12 +0000
parents 2202f548cbb0
children 8b3945dbbb4d
files MoinMoin/Page.py MoinMoin/_tests/test_util_web.py MoinMoin/action/AttachFile.py MoinMoin/logfile/eventlog.py MoinMoin/search.py MoinMoin/stats/hitcounts.py MoinMoin/stats/pagesize.py MoinMoin/stats/useragents.py MoinMoin/util/web.py MoinMoin/wikiutil.py docs/CHANGES
diffstat 11 files changed, 57 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/Page.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/Page.py	Sat Oct 15 18:03:12 2005 +0000
@@ -10,7 +10,7 @@
 
 from MoinMoin import config, caching, user, util, wikiutil
 from MoinMoin.logfile import eventlog
-from MoinMoin.util import filesys, web
+from MoinMoin.util import filesys
 from MoinMoin.util.datetime import formathttpdate
 
 class Page:
@@ -788,7 +788,7 @@
 
         @param request: the request object
         @param querystr: the query string to add after a "?" after the url
-            (str or dict, see util.web.makeQueryString)
+            (str or dict, see wikiutil.makeQueryString)
         @param escpae: escape url for html, to be backward compatible
             with old code (bool)
         @rtype: str
@@ -798,7 +798,7 @@
                      wikiutil.quoteWikinameURL(self.page_name))
 
         if querystr:
-            querystr = web.makeQueryString(querystr)
+            querystr = wikiutil.makeQueryString(querystr)
 
             # TODO: remove in 2.0
             # Escape query string to be compatible with old 3rd party code
@@ -835,7 +835,7 @@
         # Create url, excluding scriptname
         url = wikiutil.quoteWikinameURL(self.page_name)
         if querystr:
-            querystr = web.makeQueryString(querystr)
+            querystr = wikiutil.makeQueryString(querystr)
             # makeQueryString does not escape any more
             querystr = wikiutil.escape(querystr)
             url = "%s?%s" % (url, querystr)
--- a/MoinMoin/_tests/test_util_web.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/_tests/test_util_web.py	Sat Oct 15 18:03:12 2005 +0000
@@ -25,7 +25,7 @@
             )
 
         for description, arg, expected in tests:
-            result = web.makeQueryString(arg)
+            result = wikiutil.makeQueryString(arg)
             self.assertEqual(result, expected,
                              ('%(description)s: expected "%(expected)s" '
                               'but got "%(result)s"') % locals())
@@ -33,7 +33,7 @@
     def testMakeQueryStringFromKeywords(self):
         """ util.web: make query sting from keywords """
         expected = 'a=1&b=string'
-        result = web.makeQueryString(a=1, b='string')
+        result = wikiutil.makeQueryString(a=1, b='string')
         self.assertEqual(result, expected,
                          'Expected "%(expected)s" but got "%(result)s"' % locals())
 
@@ -49,7 +49,7 @@
         
         for description, arg, expected in tests:
             # Call makeQueryString with both arg and keyword
-            result = web.makeQueryString(arg, b='kw')
+            result = wikiutil.makeQueryString(arg, b='kw')
             self.assertEqual(result, expected,
                              ('%(description)s: expected "%(expected)s" '
                               'but got "%(result)s"') % locals())
--- a/MoinMoin/action/AttachFile.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/action/AttachFile.py	Sat Oct 15 18:03:12 2005 +0000
@@ -28,7 +28,7 @@
 import os, mimetypes, time, zipfile
 from MoinMoin import config, user, util, wikiutil, packages
 from MoinMoin.Page import Page
-from MoinMoin.util import MoinMoinNoFooter, filesys, web
+from MoinMoin.util import MoinMoinNoFooter, filesys
 
 action_name = __name__.split('.')[-1]
 
@@ -304,7 +304,7 @@
     drawpath = getAttachUrl(pagename, basename + '.draw', request, escaped=1)
     pngpath = getAttachUrl(pagename, basename + '.png', request, escaped=1)
     querystr = {'action': 'AttachFile', 'ts': now}
-    querystr = wikiutil.escape(web.makeQueryString(querystr))
+    querystr = wikiutil.escape(wikiutil.makeQueryString(querystr))
     pagelink = '%s/%s?%s' % (request.getScriptname(), wikiutil.quoteWikinameURL(pagename), querystr)
     helplink = Page(request, "HelpOnActions/AttachFile").url(request)
     savelink = Page(request, pagename).url(request) # XXX include target filename param here for twisted
--- a/MoinMoin/logfile/eventlog.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/logfile/eventlog.py	Sat Oct 15 18:03:12 2005 +0000
@@ -42,8 +42,8 @@
                 if value:
                     # Save those http headers in UPPERcase
                     values[key.upper()] = value
-        # Encode values in a query string TODO: use more reaable format
-        values = web.makeQueryString(values)
+        # Encode values in a query string TODO: use more readable format
+        values = wikiutil.makeQueryString(values)
         self._add(u"%d\t%s\t%s\n" % (mtime_usecs, eventtype, values))
 
     def parser(self, line):
@@ -52,7 +52,7 @@
         except ValueError:
             # badly formatted line in file, skip it
             return None
-        return long(time_usecs), eventtype, web.parseQueryString(kvpairs)
+        return long(time_usecs), eventtype, wikiutil.parseQueryString(kvpairs)
                                                                         
     def set_filter(self, event_types = None):
         if event_types == None:
--- a/MoinMoin/search.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/search.py	Sat Oct 15 18:03:12 2005 +0000
@@ -1066,10 +1066,8 @@
 
     def querystring(self):
         """ Return query string, used in the page link """
-        from MoinMoin.util import web
-                
         querystr = {'highlight': self.query.highlight_re()}
-        querystr = web.makeQueryString(querystr)
+        querystr = wikiutil.makeQueryString(querystr)
         querystr = wikiutil.escape(querystr)
         return querystr
 
--- a/MoinMoin/stats/hitcounts.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/stats/hitcounts.py	Sat Oct 15 18:03:12 2005 +0000
@@ -22,7 +22,6 @@
 
 
 def linkto(pagename, request, params=''):
-    from MoinMoin.util import web
     _ = request.getText
 
     if not request.cfg.chart_options:
@@ -36,7 +35,7 @@
 
     # Create escaped query string from dict and params
     querystr = {'action': 'chart', 'type': 'hitcounts'}
-    querystr = web.makeQueryString(querystr)
+    querystr = wikiutil.makeQueryString(querystr)
     querystr = wikiutil.escape(querystr)
     if params:
         querystr += '&amp;' + params
--- a/MoinMoin/stats/pagesize.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/stats/pagesize.py	Sat Oct 15 18:03:12 2005 +0000
@@ -16,7 +16,6 @@
 
 
 def linkto(pagename, request, params=''):
-    from MoinMoin.util import web
     _ = request.getText
 
     if not request.cfg.chart_options:
@@ -31,7 +30,7 @@
 
     # Create escaped query string from dict and params
     querystr = {'action': 'chart', 'type': 'pagesize'}
-    querystr = web.makeQueryString(querystr)
+    querystr = wikiutil.makeQueryString(querystr)
     querystr = wikiutil.escape(querystr)
     if params:
         querystr += '&amp;' + params
--- a/MoinMoin/stats/useragents.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/stats/useragents.py	Sat Oct 15 18:03:12 2005 +0000
@@ -20,7 +20,6 @@
 
 
 def linkto(pagename, request, params=''):
-    from MoinMoin.util import web
     _ = request.getText
 
     if not request.cfg.chart_options:
@@ -32,7 +31,7 @@
 
     # Create escaped query string from dict and params
     querystr = {'action': 'chart', 'type': 'useragents'}
-    querystr = web.makeQueryString(querystr)
+    querystr = wikiutil.makeQueryString(querystr)
     querystr = wikiutil.escape(querystr)
     if params:
         querystr += '&amp;' + params
--- a/MoinMoin/util/web.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/util/web.py	Sat Oct 15 18:03:12 2005 +0000
@@ -7,7 +7,7 @@
 """
 
 import re
-from MoinMoin import config, wikiutil
+from MoinMoin import config
 
 def isSpiderAgent(request):
     """ Return True if user agent appears to be a spider.
@@ -22,41 +22,6 @@
     return re.search(request.cfg.ua_spiders, ua, re.I) is not None
 
 
-def parseQueryString(qstr):
-    """ Parse a querystring "key=value&..." into a dict.
-    """
-    import cgi
-    values = {}
-    for key, value in cgi.parse_qs(qstr).items():
-        if len(value) < 2:
-            values[key] = ''.join(value)
-    return values
-
-
-def makeQueryString(qstr=None, **kw):
-    """ Make a querystring from arguments.
-        
-    kw arguments overide values in qstr.
-
-    If a string is passed in, it's returned verbatim and
-    keyword parameters are ignored.
-
-    @param qstr: dict to format as query string, using either ascii or unicode
-    @param kw: same as dict when using keywords, using assci or unicode
-    @rtype: string
-    @return: query string ready to use in a url
-    """
-    if qstr is None:
-        qstr = {}
-    if isinstance(qstr, type({})):
-        qstr.update(kw)        
-        q = lambda x: wikiutil.url_quote_plus(x)
-        items = ['%s=%s' % (q(key), q(value)) for key, value in qstr.items()]
-        qstr = '&'.join(items)
-    
-    return qstr
-
-
 def getIntegerInput(request, fieldname, default=None, minval=None, maxval=None):
     """ Get an integer value from a request parameter. If the value
         is out of bounds, it's made to fit into those bounds.
--- a/MoinMoin/wikiutil.py	Sat Oct 15 16:56:39 2005 +0000
+++ b/MoinMoin/wikiutil.py	Sat Oct 15 18:03:12 2005 +0000
@@ -6,7 +6,7 @@
     @license: GNU GPL, see COPYING for details.
 """
     
-import os, re, difflib, urllib
+import os, re, difflib, urllib, cgi
 
 from MoinMoin import util, version, config
 from MoinMoin.util import pysupport
@@ -153,6 +153,42 @@
         s = s.decode(config.charset)
     return s
 
+def parseQueryString(qstr, want_unicode=False):
+    """ Parse a querystring "key=value&..." into a dict.
+    """
+    is_unicode = isinstance(qstr, unicode)
+    if is_unicode:
+        qstr = qstr.encode(config.charset)
+    values = {}
+    for key, value in cgi.parse_qs(qstr).items():
+        if len(value) < 2:
+            v = ''.join(value)
+            if want_unicode:
+                v = v.decode(config.charset)
+            values[key] = v
+    return values
+
+def makeQueryString(qstr=None, want_unicode=False, **kw):
+    """ Make a querystring from arguments.
+        
+    kw arguments overide values in qstr.
+
+    If a string is passed in, it's returned verbatim and
+    keyword parameters are ignored.
+
+    @param qstr: dict to format as query string, using either ascii or unicode
+    @param kw: same as dict when using keywords, using assci or unicode
+    @rtype: string
+    @return: query string ready to use in a url
+    """
+    if qstr is None:
+        qstr = {}
+    if isinstance(qstr, type({})):
+        qstr.update(kw)
+        items = ['%s=%s' % (url_quote_plus(key, want_unicode=want_unicode), url_quote_plus(value, want_unicode=want_unicode)) for key, value in qstr.items()]
+        qstr = '&'.join(items)
+    return qstr
+
 
 # FIXME: better name would be quoteURL, as this is useful for any
 # string, not only wiki names.
@@ -1226,7 +1262,7 @@
         not keywords.get('print_mode', 0) and
         request.user.edit_on_doubleclick):
         if request.user.may.write(pagename): # separating this gains speed
-            querystr = escape(util.web.makeQueryString({'action': 'edit'}))
+            querystr = escape(makeQueryString({'action': 'edit'}))
             # TODO: remove escape=0 in 2.0
             url = page.url(request, querystr, escape=0)
             bodyattr.append(''' ondblclick="location.href='%s'"''' % url)
--- a/docs/CHANGES	Sat Oct 15 16:56:39 2005 +0000
+++ b/docs/CHANGES	Sat Oct 15 18:03:12 2005 +0000
@@ -241,7 +241,8 @@
       fixes display of LocalSiteMap and rendering of such links.
     * Hopefully fixed urllib problems (esp. with py 2.4.2, but also before) by
       using our own urllib wrapper that handles encoding/decoding to/from
-      unicode, see wikiutil.py.
+      unicode, see wikiutil.py. Also made a similar fix for making and parsing
+      query strings.
     
 Version 1.4: