source: main/waeup.kofa/trunk/src/waeup/kofa/accesscodes/export.py @ 9261

Last change on this file since 9261 was 9261, checked in by Henrik Bettermann, 12 years ago

Add exporter for accesscodes and accesscode batches. Only users with waeup.manageACBatches permission are allowed to use the accecodes exporter.

  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1## $Id: export.py 9261 2012-09-30 20:30:43Z 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 access codes and access code batches.
19"""
20import grok
21from zope.securitypolicy.interfaces import IPrincipalRoleManager
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.accesscodes.interfaces import IAccessCode, IAccessCodeBatch
27
28class AccessCodeBatchExporter(grok.GlobalUtility, ExporterBase):
29    """Exporter for accesscodebatches.
30    """
31    grok.implements(ICSVExporter)
32    grok.name('accesscodebatches')
33
34    #: Fieldnames considered by this exporter
35    fields = tuple(sorted(iface_names(IAccessCodeBatch)))
36
37    #: The title under which this exporter will be displayed
38    title = _(u'AccessCodeBatches')
39
40    def export_all(self, site, filepath=None):
41        """Export accesscode batches into filepath as CSV data.
42
43        If `filepath` is ``None``, a raw string with CSV data is returned.
44        """
45        writer, outfile = self.get_csv_writer(filepath)
46        accesscodebatches = site.get('accesscodes', {})
47        for batch in accesscodebatches.values():
48            self.write_item(batch, writer)
49        return self.close_outfile(filepath, outfile)
50
51class AccessCodeExporter(grok.GlobalUtility, ExporterBase):
52    """Exporter for courses.
53    """
54    grok.implements(ICSVExporter)
55    grok.name('accesscodes')
56
57    #: Fieldnames considered by this exporter
58    fields = tuple(sorted(iface_names(IAccessCode)))
59
60    #: The title under which this exporter will be displayed
61    title = _(u'AccessCodes')
62
63    def mangle_value(self, value, name, context=None):
64        if name == 'random_num' and value is not None:
65            # Append hash '#' to numbers to circumvent
66            # unwanted excel automatic
67            value = str('%s#' % value)
68        return super(
69            AccessCodeExporter, self).mangle_value(
70            value, name, context=context)
71
72    def export_all(self, site, filepath=None):
73        """Export accesscodes into filepath as CSV data.
74
75        If `filepath` is ``None``, a raw string with CSV data is returned.
76        """
77        writer, outfile = self.get_csv_writer(filepath)
78        accesscodebatches = site.get('accesscodes', {})
79        for batch in accesscodebatches.values():
80            for ac in batch.values():
81                self.write_item(ac, writer)
82        return self.close_outfile(filepath, outfile)
83
Note: See TracBrowser for help on using the repository browser.