source: main/waeup.ikoba/trunk/src/waeup/ikoba/documents/export.py @ 12316

Last change on this file since 12316 was 12287, checked in by Henrik Bettermann, 10 years ago

Extend DocumentExporterBase?. We need to know more about exported documents.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
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"""
20import grok
21from zope.securitypolicy.interfaces import IPrincipalRoleMap
22from waeup.ikoba.interfaces import ICSVExporter
23from waeup.ikoba.interfaces import MessageFactory as _
24from waeup.ikoba.utils.batching import ExporterBase
25from waeup.ikoba.utils.helpers import iface_names
26from waeup.ikoba.documents.interfaces import IPDFDocument, IHTMLDocument
27
28
29class DocumentExporterBase(grok.GlobalUtility, ExporterBase):
30    """Exporter for documents.
31    """
32    grok.implements(ICSVExporter)
33    grok.baseclass()
34    class_name = None
35    iface = None
36    title = None
37
38    @property
39    def fields(self):
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',)
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})
59        if name == 'history':
60            value = value.messages
61        return super(DocumentExporterBase, self).mangle_value(
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        """
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)
83
84
85class PDFDocumentExporter(DocumentExporterBase):
86    """Exporter for documents.
87    """
88    grok.name('pdfdocuments')
89    iface = IPDFDocument
90    class_name = 'PDFDocument'
91    title = _(u'Public PDF Documents')
92
93
94class HTMLDocumentExporter(DocumentExporterBase):
95    """Exporter for documents.
96    """
97    grok.name('htmldocuments')
98    iface = IHTMLDocument
99    class_name = 'HTMLDocument'
100    title = _(u'Public HTML Documents')
Note: See TracBrowser for help on using the repository browser.