source: main/waeup.kofa/trunk/src/waeup/kofa/documents/export.py @ 17954

Last change on this file since 17954 was 13144, checked in by Henrik Bettermann, 9 years ago

More docs and adjustments in Python code.

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1## $Id: export.py 13144 2015-07-07 05:21:14Z 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.kofa.interfaces import ICSVExporter
23from waeup.kofa.interfaces import MessageFactory as _
24from waeup.kofa.utils.batching import ExporterBase
25from waeup.kofa.utils.helpers import iface_names
26from waeup.kofa.documents.interfaces import (
27    IPDFDocument, IHTMLDocument, IRESTDocument)
28
29
30class DocumentExporterBase(grok.GlobalUtility, ExporterBase):
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.
38    """
39    grok.implements(ICSVExporter)
40    grok.baseclass()
41    class_name = None
42    iface = None
43    title = None
44
45    @property
46    def fields(self):
47        return tuple(sorted(iface_names(self.iface, exclude_attribs=False,
48            omit=['translated_state',
49                  'formatted_transition_date',
50                  'translated_class_name',
51                  'form_fields_interface',
52                  'filenames',
53                  'local_roles',
54                  ]
55            ))) + (
56            'users_with_local_roles',)
57
58    def mangle_value(self, value, name, context=None):
59        """The mangler prepares the history messages and computes the
60        `users_with_local_roles` value which is a Python expression like:
61
62        ``[{'user_name': u'bob', 'local_role': u'bobsrole'},
63        {'user_name': u'anna', 'local_role': u'annasrole'}]``
64        """
65        if name == 'users_with_local_roles':
66            value = []
67            role_map = IPrincipalRoleMap(context)
68            for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
69                value.append({'user_name':user_name,'local_role':local_role})
70        if name == 'history':
71            value = value.messages
72        return super(DocumentExporterBase, self).mangle_value(
73            value, name, context)
74
75    def export(self, documents, filepath=None):
76        """Export `documents`, an iterable, as CSV file.
77
78        If `filepath` is ``None``, a raw string with CSV data is returned.
79        """
80        writer, outfile = self.get_csv_writer(filepath)
81        for document in documents:
82            self.write_item(document, writer)
83        return self.close_outfile(filepath, outfile)
84
85    def export_all(self, site, filepath=None):
86        """Export documents in documentscontainer into filepath as CSV data.
87
88        If `filepath` is ``None``, a raw string with CSV data is returned.
89        """
90        documents = site.get('documents', {}).values()
91        documents = [doc for doc in documents
92                     if doc.class_name == self.class_name]
93        return self.export(documents, filepath)
94
95
96class PDFDocumentExporter(DocumentExporterBase):
97    """Exporter for documents.
98    """
99    grok.name('pdfdocuments')
100    iface = IPDFDocument
101    class_name = 'PDFDocument'
102    title = _(u'Public PDF Documents')
103
104
105class HTMLDocumentExporter(DocumentExporterBase):
106    """Exporter for documents.
107    """
108    grok.name('htmldocuments')
109    iface = IHTMLDocument
110    class_name = 'HTMLDocument'
111    title = _(u'Public HTML Documents')
112
113
114class RESTDocumentExporter(DocumentExporterBase):
115    """Exporter for documents.
116    """
117    grok.name('restdocuments')
118    iface = IRESTDocument
119    class_name = 'RESTDocument'
120    title = _(u'Public REST Documents')
Note: See TracBrowser for help on using the repository browser.