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

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

Add exporter and batch processor for REST documents.

Fix DocumentProcessorBase?.

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1## $Id: export.py 12410 2015-01-07 08:49:38Z 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 (
27    IPDFDocument, IHTMLDocument, IRESTDocument)
28
29
30class 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=['is_verifiable',
43                  'translated_state',
44                  'user_id',
45                  'formatted_transition_date',
46                  'translated_class_name',
47                  'connected_files',   # Could be used to export file URLs
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
86class 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
95class 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
104class RESTDocumentExporter(DocumentExporterBase):
105    """Exporter for documents.
106    """
107    grok.name('restdocuments')
108    iface = IRESTDocument
109    class_name = 'RESTDocument'
110    title = _(u'Public REST Documents')
Note: See TracBrowser for help on using the repository browser.