1 | ## $Id: export.py 13134 2015-07-03 10:19:55Z 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.kofa.interfaces import ICSVExporter |
---|
23 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
24 | from waeup.kofa.utils.batching import ExporterBase |
---|
25 | from waeup.kofa.utils.helpers import iface_names |
---|
26 | from waeup.kofa.documents.interfaces import ( |
---|
27 | IPDFDocument, IHTMLDocument, IRESTDocument) |
---|
28 | |
---|
29 | |
---|
30 | class DocumentExporterBase(grok.GlobalUtility, ExporterBase): |
---|
31 | """Exporter for documents. |
---|
32 | """ |
---|
33 | grok.implements(ICSVExporter) |
---|
34 | grok.baseclass() |
---|
35 | class_name = None |
---|
36 | iface = None |
---|
37 | title = None |
---|
38 | |
---|
39 | @property |
---|
40 | def fields(self): |
---|
41 | return tuple(sorted(iface_names(self.iface, exclude_attribs=False, |
---|
42 | omit=['translated_state', |
---|
43 | 'formatted_transition_date', |
---|
44 | 'translated_class_name', |
---|
45 | 'form_fields_interface', |
---|
46 | 'filenames', |
---|
47 | 'local_roles', |
---|
48 | ] |
---|
49 | ))) + ( |
---|
50 | 'users_with_local_roles',) |
---|
51 | |
---|
52 | def mangle_value(self, value, name, context=None): |
---|
53 | """Hook for mangling values in derived classes |
---|
54 | """ |
---|
55 | if name == 'users_with_local_roles': |
---|
56 | value = [] |
---|
57 | role_map = IPrincipalRoleMap(context) |
---|
58 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
59 | value.append({'user_name':user_name,'local_role':local_role}) |
---|
60 | if name == 'history': |
---|
61 | value = value.messages |
---|
62 | return super(DocumentExporterBase, self).mangle_value( |
---|
63 | value, name, context) |
---|
64 | |
---|
65 | def export(self, documents, filepath=None): |
---|
66 | """Export `documents`, an iterable, as CSV file. |
---|
67 | |
---|
68 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
69 | """ |
---|
70 | writer, outfile = self.get_csv_writer(filepath) |
---|
71 | for document in documents: |
---|
72 | self.write_item(document, writer) |
---|
73 | return self.close_outfile(filepath, outfile) |
---|
74 | |
---|
75 | def export_all(self, site, filepath=None): |
---|
76 | """Export documents in documentscontainer into filepath as CSV data. |
---|
77 | |
---|
78 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
79 | """ |
---|
80 | documents = site.get('documents', {}).values() |
---|
81 | documents = [doc for doc in documents |
---|
82 | if doc.class_name == self.class_name] |
---|
83 | return self.export(documents, filepath) |
---|
84 | |
---|
85 | |
---|
86 | class PDFDocumentExporter(DocumentExporterBase): |
---|
87 | """Exporter for documents. |
---|
88 | """ |
---|
89 | grok.name('pdfdocuments') |
---|
90 | iface = IPDFDocument |
---|
91 | class_name = 'PDFDocument' |
---|
92 | title = _(u'Public PDF Documents') |
---|
93 | |
---|
94 | |
---|
95 | class HTMLDocumentExporter(DocumentExporterBase): |
---|
96 | """Exporter for documents. |
---|
97 | """ |
---|
98 | grok.name('htmldocuments') |
---|
99 | iface = IHTMLDocument |
---|
100 | class_name = 'HTMLDocument' |
---|
101 | title = _(u'Public HTML Documents') |
---|
102 | |
---|
103 | |
---|
104 | class RESTDocumentExporter(DocumentExporterBase): |
---|
105 | """Exporter for documents. |
---|
106 | """ |
---|
107 | grok.name('restdocuments') |
---|
108 | iface = IRESTDocument |
---|
109 | class_name = 'RESTDocument' |
---|
110 | title = _(u'Public REST Documents') |
---|