[12249] | 1 | ## $Id: export.py 12249 2014-12-15 16:27:44Z 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 | |
---|
| 29 | class PDFDocumentExporter(grok.GlobalUtility, ExporterBase): |
---|
| 30 | """Exporter for documents. |
---|
| 31 | """ |
---|
| 32 | grok.implements(ICSVExporter) |
---|
| 33 | grok.name('pdfdocuments') |
---|
| 34 | |
---|
| 35 | #: Fieldnames considered by this exporter |
---|
| 36 | fields = tuple(sorted(iface_names(IPDFDocument))) + ( |
---|
| 37 | 'users_with_local_roles',) |
---|
| 38 | |
---|
| 39 | #: The title under which this exporter will be displayed |
---|
| 40 | title = _(u'Public PDF Documents') |
---|
| 41 | |
---|
| 42 | def mangle_value(self, value, name, context=None): |
---|
| 43 | """Hook for mangling values in derived classes |
---|
| 44 | """ |
---|
| 45 | if name == 'users_with_local_roles': |
---|
| 46 | value = [] |
---|
| 47 | role_map = IPrincipalRoleMap(context) |
---|
| 48 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 49 | value.append({'user_name':user_name,'local_role':local_role}) |
---|
| 50 | return super(PDFDocumentExporter, self).mangle_value( |
---|
| 51 | value, name, context) |
---|
| 52 | |
---|
| 53 | def export(self, documents, filepath=None): |
---|
| 54 | """Export `documents`, an iterable, as CSV file. |
---|
| 55 | |
---|
| 56 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 57 | """ |
---|
| 58 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 59 | for document in documents: |
---|
| 60 | self.write_item(document, writer) |
---|
| 61 | return self.close_outfile(filepath, outfile) |
---|
| 62 | |
---|
| 63 | def export_all(self, site, filepath=None): |
---|
| 64 | """Export documents in documentscontainer into filepath as CSV data. |
---|
| 65 | |
---|
| 66 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 67 | """ |
---|
| 68 | documents = site.get('documents', {}) |
---|
| 69 | return self.export(documents.values(), filepath) |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | class HTMLDocumentExporter(PDFDocumentExporter): |
---|
| 73 | """Exporter for documents. |
---|
| 74 | """ |
---|
| 75 | grok.name('htmldocuments') |
---|
| 76 | |
---|
| 77 | #: Fieldnames considered by this exporter |
---|
| 78 | fields = tuple(sorted(iface_names(IHTMLDocument))) + ( |
---|
| 79 | 'users_with_local_roles',) |
---|
| 80 | |
---|
| 81 | #: The title under which this exporter will be displayed |
---|
| 82 | title = _(u'Public HTML Documents') |
---|