Changeset 2185
- Timestamp:
- 7 Sep 2007, 12:15:45 (17 years ago)
- Location:
- WAeUP_SRP/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
WAeUP_SRP/trunk/WAeUPTables.py
r2184 r2185 167 167 ###) 168 168 169 security.declarePrivate("_import ") ###(170 def _import (self,filename,schema,layout, mode,logger):169 security.declarePrivate("_import_old") ###( 170 def _import_old(self,filename,schema,layout, mode,logger): 171 171 "import data from csv" 172 172 import transaction … … 219 219 # importer = None 220 220 try: 221 items = csv.DictReader(open(import_fn,"rb")) 221 items = csv.DictReader(open(import_fn,"rb"), 222 dialect="excel", 223 skipinitialspace=True) 222 224 except: 223 225 em = 'Error reading %s.csv' % filename 224 226 logger.error(em) 225 227 return d 228 #import pdb;pdb.set_trace() 226 229 for item in items: 227 230 if start: … … 229 232 logger.info('%s starts import from %s.csv' % (member,filename)) 230 233 #import_keys = [k for k in item.keys() if not k.startswith('ignore')] 231 attrs = csv.reader(open("%s/import/%s.csv" % (i_home,filename),"rb")).next() 234 attrs = csv.reader(open("%s/import/%s.csv" % (i_home,filename),"rb"), 235 dialect="excel", 236 skipinitialspace=True).next() 232 237 import_keys = [k for k in attrs if not (k.startswith('ignore') or k.isupper())] 233 238 diff2schema = set(import_keys).difference(set(schema.keys())) … … 269 274 total_imported += 1 270 275 total += 1 271 # if importer and total_imported and not total_imported % 1000:272 # transaction.commit()273 # if len(not_imported) > 0:274 # open(not_imported_fn,"a").write('\n'.join(not_imported) + '\n')275 # not_imported = []276 # if len(imported) > 0:277 # open(imported_fn,"a").write('\n'.join(imported) + '\n')278 # imported = []279 # em = '%d transactions committed\n' % (tr_count)280 # regs = []281 # logger.info(em)282 # tr_count = 0283 276 if len(imported) > 0: 284 277 open(imported_fn,"a").write('\n'.join(imported)) … … 295 288 return d 296 289 ###) 290 291 security.declarePrivate("_import") ###( 292 def _import_new(self,csv_items,schema, layout, mode,logger): 293 "import data from csv.Dictreader Instance" 294 start = True 295 tr_count = 1 296 total_imported = 0 297 total_not_imported = 0 298 total = 0 299 iname = "%s" % filename 300 not_imported = [] 301 valid_records = [] 302 invalid_records = [] 303 duplicate_records = [] 304 d = {} 305 d['mode'] = mode 306 d['valid_records'] = valid_records 307 d['invalid_records'] = invalid_records 308 d['invalid_records'] = duplicate_records 309 # d['import_fn'] = import_fn 310 # d['imported_fn'] = imported_fn 311 # d['not_imported_fn'] = not_imported_fn 312 validators = {} 313 for widget in layout.keys(): 314 try: 315 validators[widget] = layout[widget].validate 316 except AttributeError: 317 logger.info('%s has no validate attribute' % widget) 318 return d 319 for item in csv_items: 320 if start: 321 start = False 322 logger.info('%s starts import from %s.csv' % (member,filename)) 323 #import_keys = [k for k in item.keys() if not k.startswith('ignore')] 324 attrs = csv.reader(open("%s/import/%s.csv" % (i_home,filename),"rb")).next() 325 import_keys = [k for k in attrs if not (k.startswith('ignore') or k.isupper())] 326 diff2schema = set(import_keys).difference(set(schema.keys())) 327 diff2layout = set(import_keys).difference(set(layout.keys())) 328 if diff2layout: 329 em = "not ignorable key(s) %s found in heading" % diff2layout 330 logger.info(em) 331 return d 332 # s = ','.join(['"%s"' % fn for fn in import_keys]) 333 # open(not_imported_fn,"a").write(s + ',"Error"'+ '\n') 334 # #s = '"id",' + s 335 # open(imported_fn,"a").write(s + '\n') 336 # format = ','.join(['"%%(%s)s"' % fn for fn in import_keys]) 337 # format_error = format + ',"%(Error)s"' 338 # #format = '"%(id)s",'+ format 339 adapters = [MappingStorageAdapter(schema, item)] 340 dm = DataModel(item, adapters,context=self) 341 ds = DataStructure(data=item,datamodel=dm) 342 error_string = "" 343 for k in import_keys: 344 if not validators[k](ds,mode=mode): 345 error_string += " %s : %s" % (k,ds.getError(k)) 346 if error_string: 347 item['Error'] = error_string 348 #invalid_records.append(dm) 349 invalid_records.append(item) 350 total_not_imported += 1 351 else: 352 em = format % item 353 valid_records.append(dm) 354 #logger.info("%(total_imported)d of %(total)d %(em)s" % vars()) 355 tr_count += 1 356 total_imported += 1 357 total += 1 358 # if len(imported) > 0: 359 # open(imported_fn,"a").write('\n'.join(imported)) 360 # if len(not_imported) > 0: 361 # open(not_imported_fn,"a").write('\n'.join(not_imported)) 362 #em = "Imported: %d, not imported: %d of total %d" % (total_imported,total_not_imported,total) 363 d['imported'] = total_imported 364 d['not_imported'] = total_not_imported 365 d['valid_records'] = valid_records 366 d['invalid_records'] = invalid_records 367 return d 368 ###) 369 297 370 ###) 298 371 … … 406 479 WAeUPTable.__init__(self, name) 407 480 481 security.declareProtected(ModifyPortalContent,"new_importCSV")###( 482 def new_importCSV(self,filename="JAMB_data", 483 schema_id="application", 484 layout_id="application_import", 485 mode='add'): 486 """ import JAMB data """ 487 current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") 488 pm = self.portal_membership 489 member = pm.getAuthenticatedMember() 490 logger = logging.getLogger('WAeUPTables.ApplicantsCatalog.importCSV') 491 lock_fn = "%s/import/%s_import_lock" % (i_home,filename) 492 import_fn = "%s/import/%s.csv" % (i_home,filename) 493 if mode not in ('add','edit'): 494 logger.info("invalid mode: %s" % mode) 495 if os.path.exists(lock_fn): 496 logger.info("import of %(import_fn)s already in progress" % vars()) 497 return 498 lock_file = open(lock_fn,"w") 499 lock_file.write("%(current)s \n" % vars()) 500 lock_file.close() 501 invalid_fn = "%s/import/%s_not_imported%s.csv" % (i_home,filename,current) 502 duplicate_fn = "%s/import/%s_not_imported%s.csv" % (i_home,filename,current) 503 stool = getToolByName(self, 'portal_schemas') 504 ltool = getToolByName(self, 'portal_layouts') 505 schema = stool._getOb(schema_id) 506 if schema is None: 507 em = 'No such schema %s' % schema_id 508 logger.error(em) 509 return 510 for postfix in ('_import',''): 511 layout_name = "%(layout_id)s%(postfix)s" % vars() 512 if hasattr(ltool,layout_name): 513 break 514 layout = ltool._getOb(layout_name) 515 if layout is None: 516 em = 'No such layout %s' % layout_id 517 logger.error(em) 518 return 519 try: 520 csv_file = csv.DictReader(open(import_fn,"rb")) 521 except: 522 em = 'Error reading %s.csv' % filename 523 logger.error(em) 524 return 525 d = self._import_new(csv_items,schema,layout,mode,logger) 526 imported = [] 527 edited = [] 528 duplicates = [] 529 not_found = [] 530 if len(d['valid_records']) > 0: 531 for record in d['valid_records']: 532 #import pdb;pdb.set_trace() 533 if mode == "add": 534 try: 535 self.addRecord(**dict(record.items())) 536 imported.append(**dict(record.items())) 537 logger.info("added %s" % record.items()) 538 except ValueError: 539 dupplicate.append(**dict(record.items())) 540 logger.info("duplicate %s" % record.items()) 541 elif mode == "edit": 542 try: 543 self.modifyRecord(**dict(record.items())) 544 edited.append(**dict(record.items())) 545 logger.info("edited %s" % record.items()) 546 except KeyError: 547 not_found.append(**dict(record.items())) 548 logger.info("not found %s" % record.items()) 549 invalid = d['invalid_records'] 550 for itype in ("imported","edited","not_found","duplicate","invalid"): 551 outlist = locals[itype] 552 if len(outlist): 553 d = {} 554 for k in outlist[0].keys(): 555 d[k] = k 556 outlist[0] = d 557 outfile = open("file_name_%s" % itype,'w') 558 csv.DictWriter(outfile,outlist[0].keys()).writerows(outlist) 559 logger.info("wrote %(itype)s records to %(, written to %(not_imported_fn)s" % d) 560 ###) 561 408 562 security.declareProtected(ModifyPortalContent,"importCSV")###( 409 563 def importCSV(self,filename="JAMB_data", … … 425 579 return 426 580 logger = logging.getLogger('WAeUPTables.ApplicantsCatalog.importCSV') 427 d = self._import (filename,schema,layout,mode,logger)581 d = self._import_old(filename,schema,layout,mode,logger) 428 582 if len(d['valid_records']) > 0: 429 583 for record in d['valid_records']: -
WAeUP_SRP/trunk/Widgets.py
r2144 r2185 516 516 combined,states,lgas = self._getLGAs() 517 517 one_field = False 518 if not self.state_field and not self.lga_field: 518 if len(v) == 0: 519 pass 520 elif not self.state_field and not self.lga_field: 519 521 one_field = True 520 522 if v not in combined.keys():
Note: See TracChangeset for help on using the changeset viewer.