- Timestamp:
- 3 May 2007, 14:01:40 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
WAeUP_SRP/trunk/WAeUPTool.py
r1728 r1730 264 264 d = {'Title': f2t[pt]['title']} 265 265 for field in f2t[pt]['fields']: 266 d[field] = dict.get(field, None)266 d[field] = dict.get(field,'') 267 267 if pt == 'StudentPersonal': 268 268 d['sex'] = dict.get('sex') == 'F' … … 757 757 ###) 758 758 759 security.declareProtected(ModifyPortalContent,"importData")###( 760 def importData(self,name): 761 """load data from CSV values""" 762 import transaction 763 import random 764 current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") 765 students_folder = self.portal_url.getPortalObject().campus.students 766 start = True 767 tr_count = 1 768 total = 0 769 from pdb import set_trace;set_trace() 770 iname = "import_%s" % name 771 schema = getattr(self.portal_schemas,iname ,None) 772 if schema is None: 773 logger.error('No such schema %s' % iname) 774 return 775 layout = getattr(self.portal_layouts,iname,None) 776 if layout is None: 777 logger.error('No such layout %s' % iname) 778 return 779 validators = {} 780 for widget in layout.keys(): 781 validators[widget] = layout[widget].validate 782 no_import = [] 783 imported = [] 784 logger = logging.getLogger('WAeUPTool.importData') 785 try: 786 items = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) 787 except: 788 logger.error('Error reading %s.csv' % name) 789 return 790 for item in items: 791 tr_count += 1 792 if start: 793 start = False 794 logger.info('Start importing from %s.csv' % name) 795 import_keys = item.keys() 796 diff_set = set(import_keys).difference(set(schema.keys())) 797 if diff_set: 798 logger.error('%s not in schema.' % diff_set) 799 return 800 diff_set = set(import_keys).difference(set(layout.keys())) 801 if diff_set: 802 logger.error('%s not in layout.' % diff_set) 803 return 804 s = ','.join(['"%s"' % fn for fn in import_keys]) 805 imported.append(s) 806 no_import.append('%s,"Error"' % s) 807 format = ','.join(['"%%(%s)s"' % fn for fn in import_keys]) 808 format_error = format + ',"%(Error)s"' 809 for k in import_keys: 810 if validators[k](item): 811 pass 812 if tr_count > 1000: 813 if len(no_import) > 0: 814 open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( 815 '\n'.join(no_import) + '\n') 816 no_import = [] 817 em = '%d transactions committed\n' % (tr_count) 818 transaction.commit() 819 regs = [] 820 logger.info(em) 821 total += tr_count 822 tr_count = 0 823 open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( 824 '\n'.join(no_import)) 825 return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) 826 ###) 827 759 828 InitializeClass(WAeUPTool)
Note: See TracChangeset for help on using the changeset viewer.