Details

Applies to

MoinMoin - WSGI application (wsgiapp.py)

Purpose

Expose page attachments at the natural URL PageName/attachment.ext as well as PageName?action=AttachFile&do=get&target=attachment.ext

Description

The patch changes handle_action to test if the requested content URL (i.e. PageName/file.tgz) would be a valid attachment if it were in a [[attachment:...]] clause. If so, the context redirects to the valid attachment URL string (PageName?action=AttachFile&do=get&target=file.tgz).

Author

JohnCLinford

Patch

   1 diff -Naur MoinMoin/wsgiapp.py MoinMoin-patched/wsgiapp.py
   2 --- MoinMoin/wsgiapp.py	2010-06-26 15:46:46.000000000 -0600
   3 +++ MoinMoin-patched/wsgiapp.py	2012-03-03 22:45:39.000000000 -0700
   4 @@ -182,6 +182,12 @@
   5      # Try action
   6      else:
   7          from MoinMoin import action
   8 +
   9 +        p, f = action.AttachFile.absoluteName(context.page.page_name, context.page.page_name)
  10 +        if action.AttachFile.exists(context, p, f, create=0):
  11 +            url = action.AttachFile.getAttachUrl(p, f, context)
  12 +            return context.http_redirect(url)
  13 +
  14          handler = action.getHandler(context, action_name)
  15          if handler is None:
  16              msg = _("You are not allowed to do %(action_name)s on this page.") % {
  17 diff -Naur MoinMoin/action/AttachFile.py MoinMoin-patched/action/AttachFile.py
  18 --- MoinMoin/action/AttachFile.py	2010-06-26 15:46:46.000000000 -0600
  19 +++ MoinMoin-patched/action/AttachFile.py	2012-03-03 22:45:39.000000000 -0700
  20 @@ -152,7 +152,7 @@
  21      return attach_link
  22  
  23  
  24 -def getFilename(request, pagename, filename):
  25 +def getFilename(request, pagename, filename, create=1):
  26      """ make complete pathfilename of file "name" attached to some page "pagename"
  27          @param request: request object
  28          @param pagename: name of page where the file is attached to (unicode)
  29 @@ -162,12 +162,12 @@
  30      """
  31      if isinstance(filename, unicode):
  32          filename = filename.encode(config.charset)
  33 -    return os.path.join(getAttachDir(request, pagename, create=1), filename)
  34 +    return os.path.join(getAttachDir(request, pagename, create), filename)
  35  
  36  
  37 -def exists(request, pagename, filename):
  38 +def exists(request, pagename, filename, create=1):
  39      """ check if page <pagename> has a file <filename> attached """
  40 -    fpath = getFilename(request, pagename, filename)
  41 +    fpath = getFilename(request, pagename, filename, create)
  42      return os.path.exists(fpath)
  43  
  44  
NaturalAttachmentUrls.patch

Discussion

In addition to providing attachments at nice URLs, this functionality is very useful if you are converting an old site to MoinMoin and you've already published download links in literature, business cards, or other media that can't be easily changed. The disadvantage, of course, is that you can no longer have both a page named PageName/file.tgz and a file named "file.tgz" attached to PageName, but methinks that would just be confusing in any case.

Nice patch (esp. considering that attachments will be converted to subitems using exactly the same name in moin2 anyway). OTOH, as you have already noted, it is not unproblematic. -- ThomasWaldmann 2012-03-15 22:08:22

Plan


CategoryMoinMoinPatch

MoinMoin: MoinMoinPatch/NaturalAttachmentUrls (last edited 2012-03-26 16:18:23 by JohnCLinford)