Changeset 8015
- Timestamp:
- 2 Apr 2012, 09:58:35 (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
r7994 r8015 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, IStudentStudyCourse 8 from waeup.kofa.students.interfaces import ( 9 IStudent, IStudentStudyCourse, IStudentStudyLevel,) 9 10 from waeup.kofa.utils.batching import ExporterBase 10 11 from waeup.kofa.utils.helpers import iface_names … … 107 108 if x is not None] 108 109 return self.export(study_courses, filepath) 110 111 class StudentStudyLevelExporter(grok.GlobalUtility, ExporterBase): 112 """Exporter for StudentStudyLevels. 113 """ 114 grok.implements(ICSVExporter) 115 grok.name('studentstudylevels') 116 117 #: Fieldnames considered by this exporter 118 fields = tuple(sorted(iface_names(IStudentStudyLevel) + [ 119 'reg_number', 'matric_number', 'level'])) 120 121 #: The title under which this exporter will be displayed 122 title = _(u'Student Study Levels') 123 124 def mangle_value(self, value, name, context=None): 125 """Add the hash symbol at the end of date_of_birth. 126 127 This is to avoid annoying automatic date transformation 128 by Excel or Calc. 129 """ 130 131 if name == 'reg_number' and context is not None: 132 value = getattr( 133 getattr(getattr(context, '__parent__', None), 134 '__parent__', None), 'reg_number', None) 135 if name == 'matric_number' and context is not None: 136 value = getattr( 137 getattr(getattr(context, '__parent__', None), 138 '__parent__', None), 'matric_number', None) 139 return super( 140 StudentStudyLevelExporter, self).mangle_value( 141 value, name, context=context) 142 143 def export(self, study_levels, filepath=None): 144 """Export `study_levels`, an iterable, as CSV file. 145 146 If `filepath` is ``None``, a raw string with CSV data is returned. 147 """ 148 writer, outfile = self.get_csv_writer(filepath) 149 for study_level in study_levels: 150 self.write_item(study_level, writer) 151 return self.close_outfile(filepath, outfile) 152 153 def export_all(self, site, filepath=None): 154 """Export study levels into filepath as CSV data. 155 156 If `filepath` is ``None``, a raw string with CSV data is returned. 157 """ 158 catalog = queryUtility( 159 ICatalog, context=site, name='students_catalog', default=None) 160 if catalog is None: 161 return self.export([], filepath) 162 students = catalog.searchResults( 163 student_id=(None, None)) 164 levels = [] 165 study_courses = [x.get('studycourse', None) for x in students 166 if x is not None] 167 for course in study_courses: 168 for level in course.values(): 169 levels.append(level) 170 return self.export(levels, filepath) -
main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py
r7994 r8015 7 7 from waeup.kofa.interfaces import ICSVExporter 8 8 from waeup.kofa.students.export import ( 9 StudentsExporter, StudentStudyCourseExporter )9 StudentsExporter, StudentStudyCourseExporter, StudentStudyLevelExporter,) 10 10 from waeup.kofa.students.student import Student, StudentStudyCourse 11 from waeup.kofa.students.studylevel import StudentStudyLevel 11 12 from waeup.kofa.students.tests.test_batching import StudentImportExportSetup 12 13 from waeup.kofa.testing import FunctionalLayer … … 197 198 ) 198 199 return 200 201 class StudentStudyLevelExporterTest(StudentImportExportSetup): 202 203 layer = FunctionalLayer 204 205 def setUp(self): 206 super(StudentStudyLevelExporterTest, self).setUp() 207 student = Student() 208 student.student_id = u'A111111' 209 self.app['students'][student.student_id] = self.student = student 210 self.outfile = os.path.join(self.workdir, 'myoutput.csv') 211 self.cat = getUtility(ICatalog, name='students_catalog') 212 self.intids = getUtility(IIntIds) 213 #self.study_level = StudyLevel() 214 return 215 216 def test_ifaces(self): 217 # make sure we fullfill interface contracts 218 obj = StudentStudyLevelExporter() 219 verifyObject(ICSVExporter, obj) 220 verifyClass(ICSVExporter, StudentStudyLevelExporter) 221 return 222 223 def test_get_as_utility(self): 224 # we can get an student exporter as utility 225 result = queryUtility(ICSVExporter, name="studentstudylevels") 226 self.assertTrue(result is not None) 227 return 228 229 def setup_student(self, student): 230 # set predictable values for `student` 231 student.matric_number = u'M123456' 232 student.adm_code = u'my adm code' 233 student.clearance_locked = False 234 student.clr_code = u'my clr code' 235 student.perm_address = u'Studentroad 21\nLagos 123456\n' 236 student.reg_number = u'123456' 237 student.student_id = u'A111111' 238 student.firstname = u'Anna' 239 student.lastname = u'Tester' 240 student.middlename = u'M.' 241 student.date_of_birth = datetime.date(1981, 2, 4) 242 student.sex = 'f' 243 student.email = 'anna@sample.com' 244 student.phone = u'+234-123-12345' 245 student.notice = u'Some notice\nin lines.' 246 247 study_course = student.get('studycourse') 248 student['studycourse'].certificate = self.certificate 249 student['studycourse'].entry_mode = 'ug_ft' 250 student['studycourse'].entry_session = 2010 251 student['studycourse'].current_session = 2012 252 student['studycourse'].current_level = int(self.certificate.start_level) 253 254 study_level = StudentStudyLevel() 255 study_level.level_session = 2012 256 study_level.level_verdict = "A" 257 study_level.level = 100 258 student['studycourse'].addStudentStudyLevel( 259 self.certificate, study_level) 260 return student 261 262 def test_export_empty(self): 263 # we can export a nearly empty study level 264 study_level = StudentStudyLevel() 265 exporter = StudentStudyLevelExporter() 266 exporter.export([study_level], self.outfile) 267 result = open(self.outfile, 'rb').read() 268 self.assertEqual( 269 result, 270 'level,level_session,level_verdict,matric_number,reg_number\r\n' 271 ',,0,,\r\n' 272 ) 273 return 274 275 def test_export(self): 276 # we can really export study levels. 277 # set values we can expect in export file 278 self.setup_student(self.student) 279 study_course = self.student.get('studycourse') 280 study_level = study_course[study_course.keys()[0]] 281 exporter = StudentStudyLevelExporter() 282 exporter.export([study_level], self.outfile) 283 result = open(self.outfile, 'rb').read() 284 self.assertEqual( 285 result, 286 'level,level_session,level_verdict,matric_number,reg_number\r\n' 287 '100,2012,A,M123456,123456\r\n' 288 ) 289 return 290 291 def test_export_all(self): 292 # we can really export study levels 293 # set values we can expect in export file 294 self.setup_student(self.student) 295 exporter = StudentStudyLevelExporter() 296 exporter.export_all(self.app, self.outfile) 297 result = open(self.outfile, 'rb').read() 298 print [x for x in self.student['studycourse'].keys()] 299 self.assertEqual( 300 result, 301 'level,level_session,level_verdict,matric_number,reg_number\r\n' 302 '100,2012,A,M123456,123456\r\n' 303 ) 304 return
Note: See TracChangeset for help on using the changeset viewer.