changeset 4231:a29fd3a9e91f

Try using werkzeugs Href object for URL generation (first in AttachFile)
author Florian Krupicka <florian.krupicka@googlemail.com>
date Sun, 20 Jul 2008 01:48:20 +0200
parents 2074c2204d03
children 42352c0b2a51
files MoinMoin/action/AttachFile.py MoinMoin/web/request.py
diffstat 2 files changed, 17 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Sat Jul 19 01:53:28 2008 +0200
+++ b/MoinMoin/action/AttachFile.py	Sun Jul 20 01:48:20 2008 +0200
@@ -76,34 +76,20 @@
     else:
         return u"/".join(pieces[:-1]), pieces[-1]
 
-
-def attachUrl(request, pagename, filename=None, **kw):
-    # filename is not used yet, but should be used later to make a sub-item url
-    if kw:
-        qs = '?%s' % wikiutil.makeQueryString(kw, want_unicode=False)
-    else:
-        qs = ''
-    return "%s/%s%s" % (request.script_root, wikiutil.quoteWikinameURL(pagename), qs)
-
-
 def getAttachUrl(pagename, filename, request, addts=0, escaped=0, do='get', drawing='', upload=False):
     """ Get URL that points to attachment `filename` of page `pagename`. """
     if upload:
         if not drawing:
-            url = attachUrl(request, pagename, filename,
-                            rename=wikiutil.taintfilename(filename), action=action_name)
+            url = request.href(pagename, rename=wikiutil.taintfilename(filename),
+                               action=action_name)
         else:
-            url = attachUrl(request, pagename, filename,
-                            rename=wikiutil.taintfilename(filename), drawing=drawing, action=action_name)
+            url = request.href(pagename, rename=wikiutil.taintfilename(filename),
+                               drawing=drawing, action=action_name)
     else:
         if not drawing:
-            url = attachUrl(request, pagename, filename,
-                            target=filename, action=action_name, do=do)
+            url = request.href(pagename, target=filename, action=action_name, do=do)
         else:
-            url = attachUrl(request, pagename, filename,
-                            drawing=drawing, action=action_name)
-    if escaped:
-        url = wikiutil.escape(url)
+            url = request.href(pagename, drawing=drawing, action=action_name)
     return url
 
 
@@ -123,7 +109,7 @@
     fmt = request.formatter
     attach_count = _('[%d attachments]') % len(files)
     attach_icon = request.theme.make_icon('attach', vars={'attach_count': attach_count})
-    attach_link = (fmt.url(1, attachUrl(request, pagename, action=action_name), rel='nofollow') +
+    attach_link = (fmt.url(1, request.href(pagename, action=action_name), rel='nofollow') +
                    attach_icon +
                    fmt.url(0))
     return attach_link
@@ -411,9 +397,9 @@
     basename = request.form['drawing']
     drawpath = getAttachUrl(pagename, basename + '.draw', request, escaped=1)
     pngpath = getAttachUrl(pagename, basename + '.png', request, escaped=1)
-    pagelink = attachUrl(request, pagename, '', action=action_name, ts=now)
+    pagelink = request.href(pagename, action=action_name, ts=now)
     helplink = Page(request, "HelpOnActions/AttachFile").url(request)
-    savelink = attachUrl(request, pagename, '', action=action_name, do='savedrawing')
+    savelink = request.href(pagename, action=action_name, do='savedrawing')
     #savelink = Page(request, pagename).url(request) # XXX include target filename param here for twisted
                                            # request, {'savename': request.form['drawing']+'.draw'}
     #savelink = '/cgi-bin/dumpform.bat'
@@ -460,7 +446,7 @@
     if writeable:
         request.write('<h2>' + _("New Attachment") + '</h2>')
         request.write("""
-<form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
+<form action="%(url)s" method="POST" enctype="multipart/form-data">
 <dl>
 <dt>%(upload_label_file)s</dt>
 <dd><input type="file" name="file" size="50"></dd>
@@ -477,8 +463,7 @@
 </p>
 </form>
 """ % {
-    'baseurl': request.script_root,
-    'pagename': wikiutil.quoteWikinameURL(pagename),
+    'url': request.href(pagename),
     'action_name': action_name,
     'upload_label_file': _('File to upload'),
     'upload_label_rename': _('Rename to'),
--- a/MoinMoin/web/request.py	Sat Jul 19 01:53:28 2008 +0200
+++ b/MoinMoin/web/request.py	Sun Jul 20 01:48:20 2008 +0200
@@ -11,7 +11,7 @@
 
 from werkzeug.wrappers import Request as WerkzeugRequest
 from werkzeug.wrappers import Response as WerkzeugResponse
-from werkzeug.utils import EnvironHeaders, cached_property
+from werkzeug.utils import EnvironHeaders, cached_property, Href
 from werkzeug.utils import create_environ, url_encode
 from werkzeug.http import parse_cache_control_header
 
@@ -37,6 +37,11 @@
         WerkzeugRequest.__init__(self, environ, populate_request, shallow)
         WerkzeugResponse.__init__(self, response, status, headers,
                                   mimetype, content_type)
+        if self.script_root:
+            self.href = Href(self.script_root, self.charset)
+        else:
+            self.href = Href('/')
+        self.abs_href = Href(self.url_root, self.charset)
 
     data = WerkzeugResponse.data
     stream = WerkzeugResponse.stream