[12249] | 1 | ## $Id: export.py 12266 2014-12-19 16:17:17Z 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): |
---|
| 40 | return tuple(sorted(iface_names(self.iface))) + ( |
---|
[12249] | 41 | 'users_with_local_roles',) |
---|
| 42 | |
---|
| 43 | def mangle_value(self, value, name, context=None): |
---|
| 44 | """Hook for mangling values in derived classes |
---|
| 45 | """ |
---|
| 46 | if name == 'users_with_local_roles': |
---|
| 47 | value = [] |
---|
| 48 | role_map = IPrincipalRoleMap(context) |
---|
| 49 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 50 | value.append({'user_name':user_name,'local_role':local_role}) |
---|
[12266] | 51 | return super(DocumentExporterBase, self).mangle_value( |
---|
[12249] | 52 | value, name, context) |
---|
| 53 | |
---|
| 54 | def export(self, documents, filepath=None): |
---|
| 55 | """Export `documents`, an iterable, as CSV file. |
---|
| 56 | |
---|
| 57 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 58 | """ |
---|
| 59 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 60 | for document in documents: |
---|
| 61 | self.write_item(document, writer) |
---|
| 62 | return self.close_outfile(filepath, outfile) |
---|
| 63 | |
---|
| 64 | def export_all(self, site, filepath=None): |
---|
| 65 | """Export documents in documentscontainer into filepath as CSV data. |
---|
| 66 | |
---|
| 67 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 68 | """ |
---|
[12266] | 69 | documents = site.get('documents', {}).values() |
---|
| 70 | documents = [doc for doc in documents |
---|
| 71 | if doc.class_name == self.class_name] |
---|
| 72 | return self.export(documents, filepath) |
---|
[12249] | 73 | |
---|
| 74 | |
---|
[12266] | 75 | class PDFDocumentExporter(DocumentExporterBase): |
---|
[12249] | 76 | """Exporter for documents. |
---|
| 77 | """ |
---|
[12266] | 78 | grok.name('pdfdocuments') |
---|
| 79 | iface = IPDFDocument |
---|
| 80 | class_name = 'PDFDocument' |
---|
| 81 | title = _(u'Public PDF Documents') |
---|
[12249] | 82 | |
---|
| 83 | |
---|
[12266] | 84 | class HTMLDocumentExporter(DocumentExporterBase): |
---|
| 85 | """Exporter for documents. |
---|
| 86 | """ |
---|
| 87 | grok.name('htmldocuments') |
---|
| 88 | iface = IHTMLDocument |
---|
| 89 | class_name = 'HTMLDocument' |
---|
[12249] | 90 | title = _(u'Public HTML Documents') |
---|