source: main/waeup.kofa/trunk/src/waeup/kofa/hostels/export.py @ 14052

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

Add more exporter documentation.

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1## $Id: export.py 12859 2015-04-16 18:49:08Z 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 hostels and beds.
19"""
20import grok
21from waeup.kofa.interfaces import ICSVExporter
22from waeup.kofa.interfaces import MessageFactory as _
23from waeup.kofa.utils.batching import ExporterBase
24from waeup.kofa.utils.helpers import iface_names
25from waeup.kofa.hostels.interfaces import IBed, IHostel
26
27class HostelExporter(grok.GlobalUtility, ExporterBase):
28    """The Hostel Exporter exports container data. It does not
29    export beds inside the container.
30    """
31    grok.implements(ICSVExporter)
32    grok.name('hostels')
33
34    fields = tuple(sorted(iface_names(IHostel)))
35    title = _(u'Hostels')
36
37    def export_all(self, site, filepath=None):
38        """Export hostels into filepath as CSV data.
39        If `filepath` is ``None``, a raw string with CSV data is returned.
40        """
41        writer, outfile = self.get_csv_writer(filepath)
42        hostels = site.get('hostels', {})
43        for hostel in hostels.values():
44            self.write_item(hostel, writer)
45        return self.close_outfile(filepath, outfile)
46
47
48class BedExporter(grok.GlobalUtility, ExporterBase):
49    """The Bed Exporter exports all beds stored in the
50    hostel containers. The exporter iterates over all hostels
51    and over all beds inside each hostel container.
52    """
53    grok.implements(ICSVExporter)
54    grok.name('beds')
55
56    fields = tuple(sorted(iface_names(IBed))) + (
57        'hall', 'block', 'room', 'bed', 'special_handling', 'sex', 'bt')
58    title = _(u'Beds')
59
60    def export_all(self, site, filepath=None):
61        """Export beds into filepath as CSV data.
62        If `filepath` is ``None``, a raw string with CSV data is returned.
63        """
64        writer, outfile = self.get_csv_writer(filepath)
65        hostels = site.get('hostels', {})
66        for hostel in hostels.values():
67            for bed in hostel.values():
68                self.write_item(bed, writer)
69        return self.close_outfile(filepath, outfile)
70
Note: See TracBrowser for help on using the repository browser.