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

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

Add exporter tests and fix exporter.

  • Property svn:keywords set to Id
File size: 3.2 KB
RevLine 
[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"""
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
[12266]29class 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]75class 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]84class 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')
Note: See TracBrowser for help on using the repository browser.