## $Id: export.py 8160 2012-04-15 06:40:01Z henrik $ ## ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """Exporters for student related stuff. """ import grok from zope.catalog.interfaces import ICatalog from zope.component import queryUtility from waeup.kofa.interfaces import ICSVExporter from waeup.kofa.interfaces import MessageFactory as _ from waeup.kofa.students.interfaces import ( IStudent, IStudentStudyCourse, IStudentStudyLevel,) from waeup.kofa.utils.batching import ExporterBase from waeup.kofa.utils.helpers import iface_names class StudentsExporter(grok.GlobalUtility, ExporterBase): """Exporter for Students. """ grok.implements(ICSVExporter) grok.name('students') #: Fieldnames considered by this exporter fields = tuple(sorted(iface_names(IStudent, omit=['loggerInfo']))) #: The title under which this exporter will be displayed title = _(u'Students') def mangle_value(self, value, name, context=None): """Add the hash symbol at the end of date_of_birth. This is to avoid annoying automatic date transformation by Excel or Calc. """ if name == 'date_of_birth': value = str(value) + '#' return super( StudentsExporter, self).mangle_value( value, name, context=context) def export(self, students, filepath=None): """Export `students`, an iterable, as CSV file. If `filepath` is ``None``, a raw string with CSV data is returned. """ writer, outfile = self.get_csv_writer(filepath) for student in students: self.write_item(student, writer) return self.close_outfile(filepath, outfile) def export_all(self, site, filepath=None): """Export students into filepath as CSV data. If `filepath` is ``None``, a raw string with CSV data is returned. """ catalog = queryUtility( ICatalog, context=site, name='students_catalog', default=None) if catalog is None: return self.export([], filepath) students = catalog.searchResults( student_id=(None, None)) return self.export(students, filepath) class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase): """Exporter for StudentStudyCourses. """ grok.implements(ICSVExporter) grok.name('studentstudycourses') #: Fieldnames considered by this exporter fields = tuple(sorted(iface_names(IStudentStudyCourse))) #: The title under which this exporter will be displayed title = _(u'Student Study Courses') def mangle_value(self, value, name, context=None): """Add the hash symbol at the end of date_of_birth. This is to avoid annoying automatic date transformation by Excel or Calc. """ if name == 'certificate' and value is not None: # XXX: hopefully cert codes are unique site-wide value = value.code return super( StudentStudyCourseExporter, self).mangle_value( value, name, context=context) def export(self, study_courses, filepath=None): """Export `study_courses`, an iterable, as CSV file. If `filepath` is ``None``, a raw string with CSV data is returned. """ writer, outfile = self.get_csv_writer(filepath) for study_course in study_courses: self.write_item(study_course, writer) return self.close_outfile(filepath, outfile) def export_all(self, site, filepath=None): """Export study courses into filepath as CSV data. If `filepath` is ``None``, a raw string with CSV data is returned. """ catalog = queryUtility( ICatalog, context=site, name='students_catalog', default=None) if catalog is None: return self.export([], filepath) students = catalog.searchResults( student_id=(None, None)) study_courses = [x.get('studycourse', None) for x in students if x is not None] return self.export(study_courses, filepath) class StudentStudyLevelExporter(grok.GlobalUtility, ExporterBase): """Exporter for StudentStudyLevels. """ grok.implements(ICSVExporter) grok.name('studentstudylevels') #: Fieldnames considered by this exporter fields = tuple(sorted(iface_names(IStudentStudyLevel) + [ 'reg_number', 'matric_number', 'level'])) #: The title under which this exporter will be displayed title = _(u'Student Study Levels') def mangle_value(self, value, name, context=None): """Add the hash symbol at the end of date_of_birth. This is to avoid annoying automatic date transformation by Excel or Calc. """ if name == 'reg_number' and context is not None: value = getattr( getattr(getattr(context, '__parent__', None), '__parent__', None), 'reg_number', None) if name == 'matric_number' and context is not None: value = getattr( getattr(getattr(context, '__parent__', None), '__parent__', None), 'matric_number', None) return super( StudentStudyLevelExporter, self).mangle_value( value, name, context=context) def export(self, study_levels, filepath=None): """Export `study_levels`, an iterable, as CSV file. If `filepath` is ``None``, a raw string with CSV data is returned. """ writer, outfile = self.get_csv_writer(filepath) for study_level in study_levels: self.write_item(study_level, writer) return self.close_outfile(filepath, outfile) def export_all(self, site, filepath=None): """Export study levels into filepath as CSV data. If `filepath` is ``None``, a raw string with CSV data is returned. """ catalog = queryUtility( ICatalog, context=site, name='students_catalog', default=None) if catalog is None: return self.export([], filepath) students = catalog.searchResults( student_id=(None, None)) levels = [] study_courses = [x.get('studycourse', None) for x in students if x is not None] for course in study_courses: for level in course.values(): levels.append(level) return self.export(levels, filepath)