"""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 from waeup.kofa.utils.batching import ExporterBase from waeup.kofa.utils.helpers import iface_names class StudentsExporter(grok.GlobalUtility, ExporterBase): """Exporter for Applicants. """ 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)