changeset 4065:d54e233a8784

package pages: feature added to optionally include attachments.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Mon, 01 Sep 2008 09:16:49 +0200
parents fd35b36d6282
children 639501065b61
files MoinMoin/action/PackagePages.py MoinMoin/script/export/package.py
diffstat 2 files changed, 34 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/PackagePages.py	Sun Aug 31 22:14:02 2008 +0200
+++ b/MoinMoin/action/PackagePages.py	Mon Sep 01 09:16:49 2008 +0200
@@ -70,6 +70,7 @@
         # Get new name from form and normalize.
         pagelist = form.get('pagelist', [u''])[0]
         packagename = form.get('packagename', [u''])[0]
+        include_attachments = form.get('include_attachments', [False])[0]
 
         if not form.get('submit', [None])[0]:
             self.request.theme.add_msg(self.makeform(), "dialog")
@@ -91,7 +92,7 @@
 
         # Generate a package
         output = open(fpath, "wb")
-        package = self.collectpackage(unpackLine(pagelist, ","), output, target)
+        package = self.collectpackage(unpackLine(pagelist, ","), output, target, include_attachments)
 
         if package:
             self.request.theme.add_msg(self.makeform(), "dialog")
@@ -119,6 +120,7 @@
             'action': self.__class__.__name__,
             'pagename': wikiutil.escape(self.pagename, True),
             'pagename_quoted': wikiutil.quoteWikinameURL(self.pagename),
+            'include_attachments_label': _('Include all attachments?'),
             'package': _('Package pages'),
             'cancel': _('Cancel'),
             'newname_label': _("Package name"),
@@ -142,6 +144,11 @@
         </td>
     </tr>
     <tr>
+        <td class="label">
+        %(include_attachments_label)s<input type="checkbox" name="include_attachments" value="0" %(include_attachments_label)s>
+    </td>
+    </tr>
+    <tr>
         <td></td>
         <td class="buttons">
             <input type="submit" name="submit" value="%(package)s">
@@ -172,7 +179,7 @@
                 titles.append(title.page_name)
         return titles
 
-    def collectpackage(self, pagelist, fileobject, pkgname=""):
+    def collectpackage(self, pagelist, fileobject, pkgname="", include_attachments=False):
         """ Expects a list of pages as an argument, and fileobject to be an open
         file object, which a zipfile will get written to.
 
@@ -181,6 +188,8 @@
         @param pkgname: optional file name, to prevent self packaging
         @rtype: string or None
         @return: error message, if one happened
+        @rtype: boolean
+        @param include_attachments: True if you want attachments collected
         """
         _ = self.request.getText
         COMPRESSION_LEVEL = zipfile.ZIP_DEFLATED
@@ -211,13 +220,14 @@
             zi = zipfile.ZipInfo(filename=str(cnt), date_time=datetime.fromtimestamp(timestamp).timetuple()[:6])
             zi.compress_type = COMPRESSION_LEVEL
             zf.writestr(zi, page.get_raw_body().encode("utf-8"))
-            for attname in files:
-                if attname != pkgname:
-                    cnt += 1
-                    zipname = "%d_attachment" % cnt
-                    script.append(packLine(["AddAttachment", zipname, attname, page.page_name, userid, "Created by the PackagePages action."]))
-                    filename = AttachFile.getFilename(self.request, page.page_name, attname)
-                    zf.write(filename, zipname)
+            if include_attachments:
+                for attname in files:
+                    if attname != pkgname:
+                        cnt += 1
+                        zipname = "%d_attachment" % cnt
+                        script.append(packLine(["AddAttachment", zipname, attname, page.page_name, userid, "Created by the PackagePages action."]))
+                        filename = AttachFile.getFilename(self.request, page.page_name, attname)
+                        zf.write(filename, zipname)
         script += [packLine(['Print', 'Thank you for using PackagePages!'])]
 
         zf.writestr(MOIN_PACKAGE_FILE, u"\n".join(script).encode("utf-8"))
--- a/MoinMoin/script/export/package.py	Sun Aug 31 22:14:02 2008 +0200
+++ b/MoinMoin/script/export/package.py	Mon Sep 01 09:16:49 2008 +0200
@@ -42,6 +42,9 @@
 
     4. Optionally, the --user argument could be added to any of the above examples,
        causing the script to respect ACLs.
+
+    5. Optionally, the --include_attachments argument could be added to any of the above examples,
+       causing the script to include attachments into the output file.
 """
 
     def __init__(self, argv=None, def_values=None):
@@ -51,6 +54,10 @@
             help="List of pages to package. Can be regular expressions, comma seperated lists, or a lone * for everything."
         )
         self.parser.add_option(
+            "-a", "--include_attachments", action="store_true", dest="attachment",
+            help="Include attachments from each page"
+        )
+        self.parser.add_option(
             "-o", "--output", dest="output",
             help="Output file for the package."
         )
@@ -80,6 +87,10 @@
         elif not self.options.pages and not self.options.search:
             script.log(_("No pages specified using --pages or --search, assuming full package."))
 
+        include_attachments = self.options.attachment or False
+        if include_attachments:
+            script.log(_("All attachments included into the package."))
+
         # Sanity checks
         if os.path.exists(self.options.output):
             script.fatal(_("Output file already exists! Cowardly refusing to continue!"))
@@ -96,14 +107,15 @@
         packageoutput = open(self.options.output, "wb")
         if self.options.search:
             packagedata = package.collectpackage(package.searchpackage(request,
-                                                                       self.options.search), packageoutput)
+                                                                       self.options.search), packageoutput,
+                                                                       include_attachments=include_attachments)
         elif self.options.pages:
-            packagedata = package.collectpackage(self.options.pages.split(","), packageoutput)
+            packagedata = package.collectpackage(self.options.pages.split(","), packageoutput, include_attachments=include_attachments)
         else:
             packagedata = package.collectpackage(request.rootpage.getPageList(
                                 include_underlay=False,
                                 filter=lambda name: not wikiutil.isSystemPage(request, name)),
-                                packageoutput)
+                                packageoutput, include_attachments=include_attachments)
         if packagedata:
             script.fatal(packagedata)