Changeset 2189 for WAeUP_SRP/trunk
- Timestamp:
- 11 Sep 2007, 09:02:49 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
WAeUP_SRP/trunk/WAeUPTables.py
r2185 r2189 90 90 ###) 91 91 92 def record2dict(self,fields,record): 93 d = {} 94 for key in fields: 95 v = getattr(record, key, None) 96 if v: 97 if key == 'lga': 98 v = self.portal_vocabularies.local_gov_areas.get(v) 99 d[key] = v 100 else: 101 d[key] = '' 102 return d 103 92 104 def addRecord(self, **data): ###( 93 105 # The uid is the same as "bed". … … 166 178 167 179 ###) 180 181 security.declareProtected(ModifyPortalContent,"dumpAll")###( 182 def dumpAll(self): 183 """dump all data in the table to a csv""" 184 member = self.portal_membership.getAuthenticatedMember() 185 logger = logging.getLogger('WAeUPTables.dump_%s' % self.__name__) 186 current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") 187 export_file = "%s/export/%s_%s.csv" % (i_home,self.__name__,current,) 188 print export_file 189 res_list = [] 190 lines = [] 191 if hasattr(self,"export_keys"): 192 fields = self.export_keys 193 else: 194 fields = [] 195 for f in self.schema(): 196 fields.append(f) 197 headline = ','.join(fields) 198 #open(export_file,"a").write(headline +'\n') 199 out = open(export_file,"wb") 200 out.write(headline +'\n') 201 out.close() 202 out = open(export_file,"a") 203 csv_writer = csv.DictWriter(out,fields,) 204 format = '"%(' + ')s","%('.join(fields) + ')s"' 205 records = self() 206 nr2export = len(records) 207 logger.info('%s starts dumping, %s records to export' % (member,nr2export)) 208 chunk = 2000 209 total = 0 210 start = DateTime.DateTime().timeTime() 211 start_chunk = DateTime.DateTime().timeTime() 212 for record in records: 213 not_all = False 214 d = self.record2dict(fields,record) 215 #d['state'],d['lga'] = formatLGA(d['lga'],voc = self.portal_vocabularies.local_gov_areas) 216 #lines.append(format % d) 217 lines.append(d) 218 total += 1 219 if total and not total % chunk or total == len(records): 220 #open(export_file,"a").write('\n'.join(lines) +'\n') 221 csv_writer.writerows(lines) 222 anz = len(lines) 223 logger.info("wrote %(anz)d total written %(total)d" % vars()) 224 end_chunk = DateTime.DateTime().timeTime() 225 duration = end_chunk-start_chunk 226 per_record = duration/anz 227 till_now = end_chunk - start 228 avarage_per_record = till_now/total 229 estimated_end = DateTime.DateTime(start + avarage_per_record * nr2export) 230 estimated_end = estimated_end.strftime("%H:%M:%S") 231 logger.info('%(duration)4.1f, %(per_record)4.3f,end %(estimated_end)s' % vars()) 232 start_chunk = DateTime.DateTime().timeTime() 233 lines = [] 234 end = DateTime.DateTime().timeTime() 235 logger.info('total time %6.2f m' % ((end-start)/60)) 236 import os 237 filename, extension = os.path.splitext(export_file) 238 from subprocess import call 239 msg = "wrote %(total)d records to %(export_file)s" % vars() 240 try: 241 retcode = call('gzip %s' % (export_file),shell=True) 242 if retcode == 0: 243 msg = "wrote %(total)d records to %(export_file)s.gz" % vars() 244 except OSError, e: 245 retcode = -99 246 logger.info("zip failed with %s" % e) 247 logger.info(msg) 248 args = {'portal_status_message': msg} 249 #url = self.REQUEST.get('URL1') + '?' + urlencode(args) 250 url = self.REQUEST.get('URL2') 251 return self.REQUEST.RESPONSE.redirect(url) 252 ###) 168 253 169 254 security.declarePrivate("_import_old") ###( … … 473 558 key = "reg_no" 474 559 security = ClassSecurityInfo() 475 560 export_keys = ( 561 "reg_no", 562 "status", 563 "lastname", 564 "sex", 565 "date_of_birth", 566 "lga", 567 "email", 568 "phone", 569 "passport", 570 "entry_mode", 571 "pin", 572 "screening_type", 573 "registration_date", 574 "testdate", 575 "application_date", 576 "screening_date", 577 "faculty", 578 "department", 579 "course1", 580 "course2", 581 "course3", 582 "eng_score", 583 "subj1", 584 "subj1score", 585 "subj2", 586 "subj2score", 587 "subj3", 588 "subj3score", 589 "aggregate", 590 "course_admitted", 591 ) 476 592 def __init__(self,name=None): 477 593 if name == None: 478 594 name = self.name 479 595 WAeUPTable.__init__(self, name) 596 597 480 598 481 599 security.declareProtected(ModifyPortalContent,"new_importCSV")###(
Note: See TracChangeset for help on using the changeset viewer.