Changeset 7994 for main/waeup.kofa
- Timestamp:
- 28 Mar 2012, 06:12:21 (13 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/students
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/students/export.py
r7973 r7994 6 6 from waeup.kofa.interfaces import ICSVExporter 7 7 from waeup.kofa.interfaces import MessageFactory as _ 8 from waeup.kofa.students.interfaces import IStudent 8 from waeup.kofa.students.interfaces import IStudent, IStudentStudyCourse 9 9 from waeup.kofa.utils.batching import ExporterBase 10 10 from waeup.kofa.utils.helpers import iface_names … … 56 56 student_id=(None, None)) 57 57 return self.export(students, filepath) 58 59 class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase): 60 """Exporter for StudentStudyCourses. 61 """ 62 grok.implements(ICSVExporter) 63 grok.name('studentstudycourses') 64 65 #: Fieldnames considered by this exporter 66 fields = tuple(sorted(iface_names(IStudentStudyCourse))) 67 68 #: The title under which this exporter will be displayed 69 title = _(u'Student Study Courses') 70 71 def mangle_value(self, value, name, context=None): 72 """Add the hash symbol at the end of date_of_birth. 73 74 This is to avoid annoying automatic date transformation 75 by Excel or Calc. 76 """ 77 if name == 'certificate' and value is not None: 78 # XXX: hopefully cert codes are unique site-wide 79 value = value.code 80 return super( 81 StudentStudyCourseExporter, self).mangle_value( 82 value, name, context=context) 83 84 85 def export(self, study_courses, filepath=None): 86 """Export `study_courses`, an iterable, as CSV file. 87 88 If `filepath` is ``None``, a raw string with CSV data is returned. 89 """ 90 writer, outfile = self.get_csv_writer(filepath) 91 for study_course in study_courses: 92 self.write_item(study_course, writer) 93 return self.close_outfile(filepath, outfile) 94 95 def export_all(self, site, filepath=None): 96 """Export study courses into filepath as CSV data. 97 98 If `filepath` is ``None``, a raw string with CSV data is returned. 99 """ 100 catalog = queryUtility( 101 ICatalog, context=site, name='students_catalog', default=None) 102 if catalog is None: 103 return self.export([], filepath) 104 students = catalog.searchResults( 105 student_id=(None, None)) 106 study_courses = [x.get('studycourse', None) for x in students 107 if x is not None] 108 return self.export(study_courses, filepath) -
main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py
r7993 r7994 6 6 from zope.intid.interfaces import IIntIds 7 7 from waeup.kofa.interfaces import ICSVExporter 8 from waeup.kofa.students.export import StudentsExporter 9 from waeup.kofa.students.student import Student 8 from waeup.kofa.students.export import ( 9 StudentsExporter, StudentStudyCourseExporter) 10 from waeup.kofa.students.student import Student, StudentStudyCourse 10 11 from waeup.kofa.students.tests.test_batching import StudentImportExportSetup 11 12 from waeup.kofa.testing import FunctionalLayer … … 95 96 return 96 97 98 class StudentStudyCourseExporterTest(StudentImportExportSetup): 99 100 layer = FunctionalLayer 101 102 def setUp(self): 103 super(StudentStudyCourseExporterTest, self).setUp() 104 student = Student() 105 student.student_id = u'A111111' 106 self.app['students'][student.student_id] = self.student = student 107 self.outfile = os.path.join(self.workdir, 'myoutput.csv') 108 self.cat = getUtility(ICatalog, name='students_catalog') 109 self.intids = getUtility(IIntIds) 110 #self.study_course = StudyCourse() 111 return 112 113 def test_ifaces(self): 114 # make sure we fullfill interface contracts 115 obj = StudentStudyCourseExporter() 116 verifyObject(ICSVExporter, obj) 117 verifyClass(ICSVExporter, StudentStudyCourseExporter) 118 return 119 120 def test_get_as_utility(self): 121 # we can get an student exporter as utility 122 result = queryUtility(ICSVExporter, name="studentstudycourses") 123 self.assertTrue(result is not None) 124 return 125 126 def setup_student(self, student): 127 # set predictable values for `student` 128 student.matric_number = u'M123456' 129 student.adm_code = u'my adm code' 130 student.clearance_locked = False 131 student.clr_code = u'my clr code' 132 student.perm_address = u'Studentroad 21\nLagos 123456\n' 133 student.reg_number = u'123456' 134 student.student_id = u'A111111' 135 student.firstname = u'Anna' 136 student.lastname = u'Tester' 137 student.middlename = u'M.' 138 student.date_of_birth = datetime.date(1981, 2, 4) 139 student.sex = 'f' 140 student.email = 'anna@sample.com' 141 student.phone = u'+234-123-12345' 142 student.notice = u'Some notice\nin lines.' 143 144 study_course = student.get('studycourse') 145 student['studycourse'].certificate = self.certificate 146 student['studycourse'].entry_mode = 'ug_ft' 147 student['studycourse'].entry_session = 2010 148 student['studycourse'].current_session = 2012 149 student['studycourse'].current_level = int(self.certificate.start_level) 150 return student 151 152 def test_export_empty(self): 153 # we can export a nearly empty study course 154 study_course = StudentStudyCourse() 155 exporter = StudentStudyCourseExporter() 156 exporter.export([study_course], self.outfile) 157 result = open(self.outfile, 'rb').read() 158 self.assertEqual( 159 result, 160 'certificate,current_level,current_session,current_verdict,' 161 'entry_mode,entry_session,previous_verdict\r\n' 162 163 ',,,0,,,0\r\n' 164 ) 165 return 166 167 def test_export(self): 168 # we can really export study courses. 169 # set values we can expect in export file 170 self.setup_student(self.student) 171 study_course = self.student.get('studycourse') 172 exporter = StudentStudyCourseExporter() 173 exporter.export([study_course], self.outfile) 174 result = open(self.outfile, 'rb').read() 175 self.assertEqual( 176 result, 177 'certificate,current_level,current_session,current_verdict,' 178 'entry_mode,entry_session,previous_verdict\r\n' 179 180 'CERT1,200,2012,0,ug_ft,2010,0\r\n' 181 ) 182 return 183 184 def test_export_all(self): 185 # we can really export students 186 # set values we can expect in export file 187 self.setup_student(self.student) 188 exporter = StudentStudyCourseExporter() 189 exporter.export_all(self.app, self.outfile) 190 result = open(self.outfile, 'rb').read() 191 self.assertEqual( 192 result, 193 'certificate,current_level,current_session,current_verdict,' 194 'entry_mode,entry_session,previous_verdict\r\n' 195 196 'CERT1,200,2012,0,ug_ft,2010,0\r\n' 197 ) 198 return
Note: See TracChangeset for help on using the changeset viewer.