"""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 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) 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)