[12249] | 1 | ## $Id: export.py 13146 2015-07-07 05:43:23Z 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 |
---|
[12410] | 26 | from waeup.ikoba.documents.interfaces import ( |
---|
| 27 | IPDFDocument, IHTMLDocument, IRESTDocument) |
---|
[12249] | 28 | |
---|
| 29 | |
---|
[12266] | 30 | class DocumentExporterBase(grok.GlobalUtility, ExporterBase): |
---|
[13146] | 31 | """This is the exporter base class for all kinds of document exporters. |
---|
| 32 | Derived classes export all documents of type `class_name` inside the |
---|
| 33 | documents container. |
---|
| 34 | |
---|
| 35 | All schema fields and the property attributes |
---|
| 36 | `history`, `state`, `class_name` and `users_with_local_roles` |
---|
| 37 | are being exported. |
---|
[12249] | 38 | """ |
---|
| 39 | grok.implements(ICSVExporter) |
---|
[12266] | 40 | grok.baseclass() |
---|
| 41 | class_name = None |
---|
| 42 | iface = None |
---|
| 43 | title = None |
---|
[12249] | 44 | |
---|
[12266] | 45 | @property |
---|
| 46 | def fields(self): |
---|
[12287] | 47 | return tuple(sorted(iface_names(self.iface, exclude_attribs=False, |
---|
| 48 | omit=['is_verifiable', |
---|
| 49 | 'translated_state', |
---|
| 50 | 'user_id', |
---|
| 51 | 'formatted_transition_date', |
---|
| 52 | 'translated_class_name', |
---|
| 53 | 'connected_files', # Could be used to export file URLs |
---|
[13135] | 54 | 'form_fields_interface', |
---|
| 55 | 'filenames', |
---|
| 56 | 'local_roles', |
---|
[12287] | 57 | ] |
---|
| 58 | ))) + ( |
---|
| 59 | 'users_with_local_roles',) |
---|
[12249] | 60 | |
---|
| 61 | def mangle_value(self, value, name, context=None): |
---|
[13146] | 62 | """The mangler prepares the history messages and computes the |
---|
| 63 | `users_with_local_roles` value which is a Python expression like: |
---|
| 64 | |
---|
| 65 | ``[{'user_name': u'bob', 'local_role': u'bobsrole'}, |
---|
| 66 | {'user_name': u'anna', 'local_role': u'annasrole'}]`` |
---|
[12249] | 67 | """ |
---|
| 68 | if name == 'users_with_local_roles': |
---|
| 69 | value = [] |
---|
| 70 | role_map = IPrincipalRoleMap(context) |
---|
| 71 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 72 | value.append({'user_name':user_name,'local_role':local_role}) |
---|
[12287] | 73 | if name == 'history': |
---|
| 74 | value = value.messages |
---|
[12266] | 75 | return super(DocumentExporterBase, self).mangle_value( |
---|
[12249] | 76 | value, name, context) |
---|
| 77 | |
---|
| 78 | def export(self, documents, filepath=None): |
---|
| 79 | """Export `documents`, an iterable, as CSV file. |
---|
| 80 | |
---|
| 81 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 82 | """ |
---|
| 83 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 84 | for document in documents: |
---|
| 85 | self.write_item(document, writer) |
---|
| 86 | return self.close_outfile(filepath, outfile) |
---|
| 87 | |
---|
| 88 | def export_all(self, site, filepath=None): |
---|
| 89 | """Export documents in documentscontainer into filepath as CSV data. |
---|
| 90 | |
---|
| 91 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 92 | """ |
---|
[12266] | 93 | documents = site.get('documents', {}).values() |
---|
| 94 | documents = [doc for doc in documents |
---|
| 95 | if doc.class_name == self.class_name] |
---|
| 96 | return self.export(documents, filepath) |
---|
[12249] | 97 | |
---|
| 98 | |
---|
[12266] | 99 | class PDFDocumentExporter(DocumentExporterBase): |
---|
[12249] | 100 | """Exporter for documents. |
---|
| 101 | """ |
---|
[12266] | 102 | grok.name('pdfdocuments') |
---|
| 103 | iface = IPDFDocument |
---|
| 104 | class_name = 'PDFDocument' |
---|
| 105 | title = _(u'Public PDF Documents') |
---|
[12249] | 106 | |
---|
| 107 | |
---|
[12266] | 108 | class HTMLDocumentExporter(DocumentExporterBase): |
---|
| 109 | """Exporter for documents. |
---|
| 110 | """ |
---|
| 111 | grok.name('htmldocuments') |
---|
| 112 | iface = IHTMLDocument |
---|
| 113 | class_name = 'HTMLDocument' |
---|
[12410] | 114 | title = _(u'Public HTML Documents') |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | class RESTDocumentExporter(DocumentExporterBase): |
---|
| 118 | """Exporter for documents. |
---|
| 119 | """ |
---|
| 120 | grok.name('restdocuments') |
---|
| 121 | iface = IRESTDocument |
---|
| 122 | class_name = 'RESTDocument' |
---|
| 123 | title = _(u'Public REST Documents') |
---|