[9199] | 1 | ## $Id: export.py 9307 2012-10-07 21:45:46Z 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 | """ |
---|
| 20 | import grok |
---|
| 21 | from waeup.kofa.interfaces import ICSVExporter |
---|
| 22 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 23 | from waeup.kofa.utils.batching import ExporterBase |
---|
| 24 | from waeup.kofa.utils.helpers import iface_names |
---|
| 25 | from waeup.kofa.hostels.interfaces import IBed, IHostel |
---|
| 26 | |
---|
| 27 | class HostelExporter(grok.GlobalUtility, ExporterBase): |
---|
| 28 | """Exporter for hostels. |
---|
| 29 | """ |
---|
| 30 | grok.implements(ICSVExporter) |
---|
| 31 | grok.name('hostels') |
---|
| 32 | |
---|
| 33 | #: Fieldnames considered by this exporter |
---|
| 34 | fields = tuple(sorted(iface_names(IHostel))) |
---|
| 35 | |
---|
| 36 | #: The title under which this exporter will be displayed |
---|
| 37 | title = _(u'Hostels') |
---|
| 38 | |
---|
| 39 | def export_all(self, site, filepath=None): |
---|
| 40 | """Export hostels into filepath as CSV data. |
---|
| 41 | |
---|
| 42 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 43 | """ |
---|
| 44 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 45 | hostels = site.get('hostels', {}) |
---|
| 46 | for hostel in hostels.values(): |
---|
| 47 | self.write_item(hostel, writer) |
---|
| 48 | return self.close_outfile(filepath, outfile) |
---|
| 49 | |
---|
| 50 | class BedExporter(grok.GlobalUtility, ExporterBase): |
---|
| 51 | """Exporter for courses. |
---|
| 52 | """ |
---|
| 53 | grok.implements(ICSVExporter) |
---|
| 54 | grok.name('beds') |
---|
| 55 | |
---|
| 56 | #: Fieldnames considered by this exporter |
---|
| 57 | fields = tuple(sorted(iface_names(IBed))) + ( |
---|
| 58 | 'hall', 'block', 'room', 'bed', 'special_handling', 'sex', 'bt') |
---|
| 59 | |
---|
| 60 | #: The title under which this exporter will be displayed |
---|
| 61 | title = _(u'Beds') |
---|
| 62 | |
---|
| 63 | def export_all(self, site, filepath=None): |
---|
| 64 | """Export beds into filepath as CSV data. |
---|
| 65 | |
---|
| 66 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 67 | """ |
---|
| 68 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 69 | hostels = site.get('hostels', {}) |
---|
| 70 | for hostel in hostels.values(): |
---|
| 71 | for bed in hostel.values(): |
---|
| 72 | self.write_item(bed, writer) |
---|
| 73 | return self.close_outfile(filepath, outfile) |
---|
| 74 | |
---|