changeset 1910:5c3dffe2abf1

new function AttachFile.exists to reduce code duplication
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 25 Mar 2007 22:37:45 +0200
parents be140a589f11
children 0b2ad3099fcf
files MoinMoin/action/AttachFile.py MoinMoin/formatter/text_gedit.py MoinMoin/formatter/text_html.py MoinMoin/macro/EmbedObject.py MoinMoin/macro/ImageLink.py MoinMoin/parser/text_rst.py
diffstat 6 files changed, 18 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Sun Mar 25 21:55:13 2007 +0200
+++ b/MoinMoin/action/AttachFile.py	Sun Mar 25 22:37:45 2007 +0200
@@ -148,6 +148,10 @@
     """
     return os.path.join(getAttachDir(request, pagename), filename).encode(config.charset)
 
+def exists(request, pagename, filename):
+    """ check if page <pagename> has a file <filename> attached """
+    fpath = getFilename(request, pagename, filename)
+    return os.path.exists(fpath)
 
 def info(pagename, request):
     """ Generate snippet with info on the attachment for page `pagename`.
--- a/MoinMoin/formatter/text_gedit.py	Sun Mar 25 21:55:13 2007 +0200
+++ b/MoinMoin/formatter/text_gedit.py	Sun Mar 25 22:37:45 2007 +0200
@@ -85,8 +85,6 @@
         _ = self.request.getText
         pagename = self.page.page_name
         image = url + u'.png'
-        fname = wikiutil.taintfilename(image)
-        fpath = AttachFile.getFilename(self.request, pagename, fname)
         return self.image(
             title="drawing:%s" % wikiutil.quoteWikinameURL(url),
             src=AttachFile.getAttachUrl(pagename, image, self.request, addts=1))
--- a/MoinMoin/formatter/text_html.py	Sun Mar 25 21:55:13 2007 +0200
+++ b/MoinMoin/formatter/text_html.py	Sun Mar 25 22:37:45 2007 +0200
@@ -633,8 +633,7 @@
         pagename, filename = AttachFile.absoluteName(url, self.page.page_name)
         #self.request.log("attachment_link: url %s pagename %s filename %s" % (url, pagename, filename))
         fname = wikiutil.taintfilename(filename)
-        fpath = AttachFile.getFilename(self.request, pagename, fname)
-        if not os.path.exists(fpath):
+        if not AttachFile.exists(self.request, pagename, fname):
             linktext = _('Upload new attachment "%(filename)s"')
             return wikiutil.link_tag(
                 self.request,
@@ -651,8 +650,7 @@
         _ = self.request.getText
         pagename, filename = AttachFile.absoluteName(url, self.page.page_name)
         fname = wikiutil.taintfilename(filename)
-        fpath = AttachFile.getFilename(self.request, pagename, fname)
-        if not os.path.exists(fpath):
+        if not AttachFile.exists(self.request, pagename, fname):
             linktext = _('Upload new attachment "%(filename)s"')
             return wikiutil.link_tag(
                 self.request,
@@ -673,16 +671,16 @@
         fname = fname + u".png"
         filename = filename + u".png"
         # fallback for old gif drawings (1.1 -> 1.2)
-        fpath = AttachFile.getFilename(self.request, pagename, fname)
-        if not os.path.exists(fpath):
+        exists = AttachFile.exists(self.request, pagename, fname)
+        if not exists:
             gfname = fname[:-4] + u".gif"
             gfilename = filename[:-4] + u".gif"
-            gfpath = AttachFile.getFilename(self.request, pagename, gfname)
-            if os.path.exists(gfpath):
-                fname, filename, fpath = gfname, gfilename, gfpath
+            exists = AttachFile.exists(self.request, pagename, gfname)
+            if exists:
+                fname, filename = gfname, gfilename
 
         # check whether attachment exists, possibly point to upload form
-        if not os.path.exists(fpath):
+        if not exists:
             linktext = _('Create new drawing "%(filename)s"')
             return wikiutil.link_tag(
                 self.request,
--- a/MoinMoin/macro/EmbedObject.py	Sun Mar 25 21:55:13 2007 +0200
+++ b/MoinMoin/macro/EmbedObject.py	Sun Mar 25 22:37:45 2007 +0200
@@ -256,9 +256,8 @@
             return "%s%s%s" % (self.formatter.sysmsg(1), self.formatter.text(msg), self.formatter.sysmsg(0))
 
         pagename, attname = AttachFile.absoluteName(self.target, self.formatter.page.page_name)
-        attachment_fname = AttachFile.getFilename(self.request, pagename, attname)
 
-        if not os.path.exists(attachment_fname):
+        if not AttachFile.exists(self.request, pagename, attname):
             linktext = _('Upload new attachment "%(filename)s"')
             return wikiutil.link_tag(self.request,
                 ('%s?action=AttachFile&rename=%s' % (
--- a/MoinMoin/macro/ImageLink.py	Sun Mar 25 21:55:13 2007 +0200
+++ b/MoinMoin/macro/ImageLink.py	Sun Mar 25 22:37:45 2007 +0200
@@ -143,8 +143,7 @@
                 pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
                 target = AttachFile.getAttachUrl(pagename, target, request, do='view')
 
-            attachment_fname = AttachFile.getFilename(request, pagename, attname)
-            if not os.path.exists(attachment_fname):
+            if not AttachFile.exists(request, pagename, attname):
                 linktext = _('Upload new attachment "%(filename)s"')
                 return wikiutil.link_tag(request,
                                          ('%s?action=AttachFile&rename=%s' % (
@@ -165,8 +164,7 @@
     else:
         pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
         kw['src'] = AttachFile.getAttachUrl(pagename, attname, request)
-        attachment_fname = AttachFile.getFilename(request, pagename, attname)
-        if not os.path.exists(attachment_fname):
+        if not AttachFile.exists(request, pagename, attname):
             linktext = _('Upload new attachment "%(filename)s"')
             return wikiutil.link_tag(request,
                                      ('%s?action=AttachFile&rename=%s' % (
--- a/MoinMoin/parser/text_rst.py	Sun Mar 25 21:55:13 2007 +0200
+++ b/MoinMoin/parser/text_rst.py	Sun Mar 25 22:37:45 2007 +0200
@@ -349,11 +349,8 @@
             # From here down, all links are handled by docutils (except 
             # missing attachments), just fixup node['refuri'].
             if prefix == 'attachment':
-                attach_file = AttachFile.getFilename(self.request, 
-                        self.request.page.page_name, link)
-                if not os.path.exists(attach_file):
-                    # Attachment doesn't exist, give to MoinMoin to insert
-                    # upload text.
+                if not AttachFile.exists(self.request, self.request.page.page_name, link):
+                    # Attachment doesn't exist, give to MoinMoin to insert upload text.
                     self.process_wiki_text(refuri)
                     self.wiki_text = self.fixup_wiki_formatting(self.wiki_text)
                     self.add_wiki_markup()
@@ -408,9 +405,7 @@
             attach_name = uri.split(':', 1)[1]
         # if prefix isn't URL, try to display in page
         if not prefix.lower() in ('file', 'http', 'https', 'ftp'):
-            attach_file = AttachFile.getFilename(self.request, 
-                    self.request.page.page_name, attach_name)
-            if not os.path.exists(attach_file):
+            if not AttachFile.exists(self.request, self.request.page.page_name, attach_name):
                 # Attachment doesn't exist, MoinMoin should process it
                 if prefix == '':
                     prefix = 'inline:'