comparison MoinMoin/action/PackagePages.py @ 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 e458af23d64d
children 62177a952833
comparison
equal deleted inserted replaced
4061:fd35b36d6282 4065:d54e233a8784
68 form = self.request.form 68 form = self.request.form
69 69
70 # Get new name from form and normalize. 70 # Get new name from form and normalize.
71 pagelist = form.get('pagelist', [u''])[0] 71 pagelist = form.get('pagelist', [u''])[0]
72 packagename = form.get('packagename', [u''])[0] 72 packagename = form.get('packagename', [u''])[0]
73 include_attachments = form.get('include_attachments', [False])[0]
73 74
74 if not form.get('submit', [None])[0]: 75 if not form.get('submit', [None])[0]:
75 self.request.theme.add_msg(self.makeform(), "dialog") 76 self.request.theme.add_msg(self.makeform(), "dialog")
76 raise ActionError 77 raise ActionError
77 78
89 'target': wikiutil.escape(target), 'filename': wikiutil.escape(target)}, "error") 90 'target': wikiutil.escape(target), 'filename': wikiutil.escape(target)}, "error")
90 raise ActionError 91 raise ActionError
91 92
92 # Generate a package 93 # Generate a package
93 output = open(fpath, "wb") 94 output = open(fpath, "wb")
94 package = self.collectpackage(unpackLine(pagelist, ","), output, target) 95 package = self.collectpackage(unpackLine(pagelist, ","), output, target, include_attachments)
95 96
96 if package: 97 if package:
97 self.request.theme.add_msg(self.makeform(), "dialog") 98 self.request.theme.add_msg(self.makeform(), "dialog")
98 raise ActionError 99 raise ActionError
99 100
117 'baseurl': self.request.getScriptname(), 118 'baseurl': self.request.getScriptname(),
118 'error': error, 119 'error': error,
119 'action': self.__class__.__name__, 120 'action': self.__class__.__name__,
120 'pagename': wikiutil.escape(self.pagename, True), 121 'pagename': wikiutil.escape(self.pagename, True),
121 'pagename_quoted': wikiutil.quoteWikinameURL(self.pagename), 122 'pagename_quoted': wikiutil.quoteWikinameURL(self.pagename),
123 'include_attachments_label': _('Include all attachments?'),
122 'package': _('Package pages'), 124 'package': _('Package pages'),
123 'cancel': _('Cancel'), 125 'cancel': _('Cancel'),
124 'newname_label': _("Package name"), 126 'newname_label': _("Package name"),
125 'list_label': _("List of page names - separated by a comma"), 127 'list_label': _("List of page names - separated by a comma"),
126 } 128 }
140 <td class="content"> 142 <td class="content">
141 <input type="text" name="pagelist" size="80" maxlength="200" value="%(pagename)s"> 143 <input type="text" name="pagelist" size="80" maxlength="200" value="%(pagename)s">
142 </td> 144 </td>
143 </tr> 145 </tr>
144 <tr> 146 <tr>
147 <td class="label">
148 %(include_attachments_label)s<input type="checkbox" name="include_attachments" value="0" %(include_attachments_label)s>
149 </td>
150 </tr>
151 <tr>
145 <td></td> 152 <td></td>
146 <td class="buttons"> 153 <td class="buttons">
147 <input type="submit" name="submit" value="%(package)s"> 154 <input type="submit" name="submit" value="%(package)s">
148 <input type="submit" name="cancel" value="%(cancel)s"> 155 <input type="submit" name="cancel" value="%(cancel)s">
149 </td> 156 </td>
170 for title in pagelist.hits: 177 for title in pagelist.hits:
171 if not wikiutil.isSystemPage(request, title.page_name) or not title.page.getPageStatus()[0]: 178 if not wikiutil.isSystemPage(request, title.page_name) or not title.page.getPageStatus()[0]:
172 titles.append(title.page_name) 179 titles.append(title.page_name)
173 return titles 180 return titles
174 181
175 def collectpackage(self, pagelist, fileobject, pkgname=""): 182 def collectpackage(self, pagelist, fileobject, pkgname="", include_attachments=False):
176 """ Expects a list of pages as an argument, and fileobject to be an open 183 """ Expects a list of pages as an argument, and fileobject to be an open
177 file object, which a zipfile will get written to. 184 file object, which a zipfile will get written to.
178 185
179 @param pagelist: pages to package 186 @param pagelist: pages to package
180 @param fileobject: open file object to write to 187 @param fileobject: open file object to write to
181 @param pkgname: optional file name, to prevent self packaging 188 @param pkgname: optional file name, to prevent self packaging
182 @rtype: string or None 189 @rtype: string or None
183 @return: error message, if one happened 190 @return: error message, if one happened
191 @rtype: boolean
192 @param include_attachments: True if you want attachments collected
184 """ 193 """
185 _ = self.request.getText 194 _ = self.request.getText
186 COMPRESSION_LEVEL = zipfile.ZIP_DEFLATED 195 COMPRESSION_LEVEL = zipfile.ZIP_DEFLATED
187 196
188 pages = [] 197 pages = []
209 218
210 timestamp = wikiutil.version2timestamp(page.mtime_usecs()) 219 timestamp = wikiutil.version2timestamp(page.mtime_usecs())
211 zi = zipfile.ZipInfo(filename=str(cnt), date_time=datetime.fromtimestamp(timestamp).timetuple()[:6]) 220 zi = zipfile.ZipInfo(filename=str(cnt), date_time=datetime.fromtimestamp(timestamp).timetuple()[:6])
212 zi.compress_type = COMPRESSION_LEVEL 221 zi.compress_type = COMPRESSION_LEVEL
213 zf.writestr(zi, page.get_raw_body().encode("utf-8")) 222 zf.writestr(zi, page.get_raw_body().encode("utf-8"))
214 for attname in files: 223 if include_attachments:
215 if attname != pkgname: 224 for attname in files:
216 cnt += 1 225 if attname != pkgname:
217 zipname = "%d_attachment" % cnt 226 cnt += 1
218 script.append(packLine(["AddAttachment", zipname, attname, page.page_name, userid, "Created by the PackagePages action."])) 227 zipname = "%d_attachment" % cnt
219 filename = AttachFile.getFilename(self.request, page.page_name, attname) 228 script.append(packLine(["AddAttachment", zipname, attname, page.page_name, userid, "Created by the PackagePages action."]))
220 zf.write(filename, zipname) 229 filename = AttachFile.getFilename(self.request, page.page_name, attname)
230 zf.write(filename, zipname)
221 script += [packLine(['Print', 'Thank you for using PackagePages!'])] 231 script += [packLine(['Print', 'Thank you for using PackagePages!'])]
222 232
223 zf.writestr(MOIN_PACKAGE_FILE, u"\n".join(script).encode("utf-8")) 233 zf.writestr(MOIN_PACKAGE_FILE, u"\n".join(script).encode("utf-8"))
224 zf.close() 234 zf.close()
225 235