Changeset 3453 for WAeUP_SRP/base
- Timestamp:
- 20 Apr 2008, 21:40:30 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
WAeUP_SRP/base/Students.py
r3447 r3453 284 284 ###) 285 285 286 security.declareProtected(ModifyPortalContent,"exportStudents")###( 287 def exportStudents(self): 288 """export Studentsdata to a file""" 289 member = self.portal_membership.getAuthenticatedMember() 290 logger = logging.getLogger('Students.exportStudents') 291 current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") 292 students_folder = self.portal_url.getPortalObject().campus.students 293 export_file = "%s/export/students_%s.csv" % (i_home,current) 294 295 from Products.AdvancedQuery import Eq, Between, Le,In,MatchRegexp 296 aq_students = self.students_catalog.evalAdvancedQuery 297 toexport = {'students_catalog': ("id", 298 "matric_no", 299 "jamb_reg_no", 300 "name", 301 #"review_state", 302 #"entry_session", 303 #"session", 304 #"entry_mode", 305 #"mode", 306 #"verdict", 307 #"sex", 308 #"email", 309 #"phone", 310 #"faculty", 311 #"department", 312 #"course", 313 #"level", 314 ), 315 #'personal': ('firstname', 316 # 'middlename', 317 # 'lastname', 318 # 'perm_address', 319 # ), 320 'clearance': (#'state', # is not an attribute of clearance but is needed for splitting lga 321 #'lga', 322 'birthday', 323 ) 324 } 325 res_list = [] 326 lines = [] 327 fields = [] 328 with_lga = False 329 for k in toexport.keys(): 330 for f in toexport[k]: 331 if f == 'lga' : 332 with_lga = True 333 fields.append(f) 334 headline = ','.join(fields).replace('level','current_level') 335 open(export_file,"a").write(headline +'\n') 336 format = '"%(' + ')s","%('.join(fields) + ')s"' 337 #query = In('review_state',('cleared_and_validated', 338 # 'clearance_requested', 339 # 'school_fee_paid', 340 # 'courses_registered', 341 # 'courses_validated')) 342 #query = In('review_state',('clearance_requested')) 343 #students = aq_students(query) 344 students = aq_students 345 nr2export = len(students) 346 logger.info('%s starts exportStudents, %s student records to export' % (member,nr2export)) 347 chunk = 1000 348 total = 0 349 start = DateTime.DateTime().timeTime() 350 start_chunk = DateTime.DateTime().timeTime() 351 for student in students: 352 not_all = False 353 d = self.getFormattedStudentEntry(student) 354 student_obj = getattr(students_folder,student.id) 355 for k in toexport.keys()[1:]: 356 try: 357 object = getattr(student_obj,k) 358 object_doc = object.getContent() 359 except: 360 logger.info('%s %s record not found' % (student.id,k)) 361 not_all = True 362 continue 363 for f in toexport[k]: 364 d[f] = getattr(object_doc,f,'') 365 if not_all: 366 continue 367 if with_lga: 368 d['state'],d['lga'] = formatLGA(d['lga'],voc = self.portal_vocabularies.local_gov_areas) 369 lines.append(format % d) 370 total += 1 371 if total and not total % chunk or total == len(students): 372 open(export_file,"a").write('\n'.join(lines) +'\n') 373 anz = len(lines) 374 logger.info("wrote %(anz)d total written %(total)d" % vars()) 375 end_chunk = DateTime.DateTime().timeTime() 376 duration = end_chunk-start_chunk 377 per_record = duration/anz 378 till_now = end_chunk - start 379 avarage_per_record = till_now/total 380 estimated_end = DateTime.DateTime(start + avarage_per_record * nr2export) 381 estimated_end = estimated_end.strftime("%H:%M:%S") 382 logger.info('%(duration)4.1f, %(per_record)4.3f,end %(estimated_end)s' % vars()) 383 start_chunk = DateTime.DateTime().timeTime() 384 lines = [] 385 end = DateTime.DateTime().timeTime() 386 logger.info('total time %6.2f m' % ((end-start)/60)) 387 filename, extension = os.path.splitext(export_file) 388 from subprocess import call 389 msg = "wrote %(total)d records to %(export_file)s" % vars() 390 try: 391 retcode = call('gzip %s' % (export_file),shell=True) 392 if retcode == 0: 393 msg = "wrote %(total)d records to %(export_file)s.gz" % vars() 394 except OSError, e: 395 retcode = -99 396 logger.info("zip failed with %s" % e) 397 logger.info(msg) 398 args = {'portal_status_message': msg} 399 url = self.REQUEST.get('URL2') 400 return self.REQUEST.RESPONSE.redirect(url) 401 ###) 286 402 287 403 security.declareProtected(ModifyPortalContent,"dumpStudentsCatalog")###(
Note: See TracChangeset for help on using the changeset viewer.