[12249] | 1 | ## $Id: export.py 12287 2014-12-21 17:00:10Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | """Exporters for documents. |
---|
| 19 | """ |
---|
| 20 | import grok |
---|
| 21 | from zope.securitypolicy.interfaces import IPrincipalRoleMap |
---|
| 22 | from waeup.ikoba.interfaces import ICSVExporter |
---|
| 23 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 24 | from waeup.ikoba.utils.batching import ExporterBase |
---|
| 25 | from waeup.ikoba.utils.helpers import iface_names |
---|
| 26 | from waeup.ikoba.documents.interfaces import IPDFDocument, IHTMLDocument |
---|
| 27 | |
---|
| 28 | |
---|
[12266] | 29 | class DocumentExporterBase(grok.GlobalUtility, ExporterBase): |
---|
[12249] | 30 | """Exporter for documents. |
---|
| 31 | """ |
---|
| 32 | grok.implements(ICSVExporter) |
---|
[12266] | 33 | grok.baseclass() |
---|
| 34 | class_name = None |
---|
| 35 | iface = None |
---|
| 36 | title = None |
---|
[12249] | 37 | |
---|
[12266] | 38 | @property |
---|
| 39 | def fields(self): |
---|
[12287] | 40 | return tuple(sorted(iface_names(self.iface, exclude_attribs=False, |
---|
| 41 | omit=['is_verifiable', |
---|
| 42 | 'translated_state', |
---|
| 43 | 'user_id', |
---|
| 44 | 'formatted_transition_date', |
---|
| 45 | 'translated_class_name', |
---|
| 46 | 'connected_files', # Could be used to export file URLs |
---|
| 47 | ] |
---|
| 48 | ))) + ( |
---|
| 49 | 'users_with_local_roles',) |
---|
[12249] | 50 | |
---|
| 51 | def mangle_value(self, value, name, context=None): |
---|
| 52 | """Hook for mangling values in derived classes |
---|
| 53 | """ |
---|
| 54 | if name == 'users_with_local_roles': |
---|
| 55 | value = [] |
---|
| 56 | role_map = IPrincipalRoleMap(context) |
---|
| 57 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 58 | value.append({'user_name':user_name,'local_role':local_role}) |
---|
[12287] | 59 | if name == 'history': |
---|
| 60 | value = value.messages |
---|
[12266] | 61 | return super(DocumentExporterBase, self).mangle_value( |
---|
[12249] | 62 | value, name, context) |
---|
| 63 | |
---|
| 64 | def export(self, documents, filepath=None): |
---|
| 65 | """Export `documents`, an iterable, as CSV file. |
---|
| 66 | |
---|
| 67 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 68 | """ |
---|
| 69 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 70 | for document in documents: |
---|
| 71 | self.write_item(document, writer) |
---|
| 72 | return self.close_outfile(filepath, outfile) |
---|
| 73 | |
---|
| 74 | def export_all(self, site, filepath=None): |
---|
| 75 | """Export documents in documentscontainer into filepath as CSV data. |
---|
| 76 | |
---|
| 77 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 78 | """ |
---|
[12266] | 79 | documents = site.get('documents', {}).values() |
---|
| 80 | documents = [doc for doc in documents |
---|
| 81 | if doc.class_name == self.class_name] |
---|
| 82 | return self.export(documents, filepath) |
---|
[12249] | 83 | |
---|
| 84 | |
---|
[12266] | 85 | class PDFDocumentExporter(DocumentExporterBase): |
---|
[12249] | 86 | """Exporter for documents. |
---|
| 87 | """ |
---|
[12266] | 88 | grok.name('pdfdocuments') |
---|
| 89 | iface = IPDFDocument |
---|
| 90 | class_name = 'PDFDocument' |
---|
| 91 | title = _(u'Public PDF Documents') |
---|
[12249] | 92 | |
---|
| 93 | |
---|
[12266] | 94 | class HTMLDocumentExporter(DocumentExporterBase): |
---|
| 95 | """Exporter for documents. |
---|
| 96 | """ |
---|
| 97 | grok.name('htmldocuments') |
---|
| 98 | iface = IHTMLDocument |
---|
| 99 | class_name = 'HTMLDocument' |
---|
[12249] | 100 | title = _(u'Public HTML Documents') |
---|