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

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

Choose consistent exporter names.

  • Property svn:keywords set to Id
File size: 3.5 KB
RevLine 
[9261]1## $Id: export.py 12180 2014-12-09 05:21:31Z 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
[9262]35    fields = tuple(sorted(iface_names(IAccessCodeBatch))) + ('batch_id',)
[9261]36
37    #: The title under which this exporter will be displayed
[12180]38    title = _(u'Access Code Batches')
[9261]39
[9262]40    def mangle_value(self, value, name, context=None):
41        if name == 'batch_id' and context is not None:
42            value = u'%s-%s' % (getattr(context, 'prefix', None),
43                getattr(context, 'num', None))
44        return super(
45            AccessCodeBatchExporter, self).mangle_value(
46            value, name, context=context)
47
[9261]48    def export_all(self, site, filepath=None):
49        """Export accesscode batches into filepath as CSV data.
50
51        If `filepath` is ``None``, a raw string with CSV data is returned.
52        """
53        writer, outfile = self.get_csv_writer(filepath)
54        accesscodebatches = site.get('accesscodes', {})
55        for batch in accesscodebatches.values():
56            self.write_item(batch, writer)
57        return self.close_outfile(filepath, outfile)
58
59class AccessCodeExporter(grok.GlobalUtility, ExporterBase):
60    """Exporter for courses.
61    """
62    grok.implements(ICSVExporter)
63    grok.name('accesscodes')
64
65    #: Fieldnames considered by this exporter
66    fields = tuple(sorted(iface_names(IAccessCode)))
67
68    #: The title under which this exporter will be displayed
[12180]69    title = _(u'Access Codes')
[9261]70
71    def mangle_value(self, value, name, context=None):
72        if name == 'random_num' and value is not None:
73            # Append hash '#' to numbers to circumvent
74            # unwanted excel automatic
75            value = str('%s#' % value)
76        return super(
77            AccessCodeExporter, self).mangle_value(
78            value, name, context=context)
79
80    def export_all(self, site, filepath=None):
81        """Export accesscodes into filepath as CSV data.
82
83        If `filepath` is ``None``, a raw string with CSV data is returned.
84        """
85        writer, outfile = self.get_csv_writer(filepath)
86        accesscodebatches = site.get('accesscodes', {})
87        for batch in accesscodebatches.values():
88            for ac in batch.values():
89                self.write_item(ac, writer)
90        return self.close_outfile(filepath, outfile)
91
Note: See TracBrowser for help on using the repository browser.