- Timestamp:
- 3 May 2012, 07:28:57 (13 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/applicants
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/applicants/batching.py
r8290 r8331 77 77 class ApplicantProcessor(BatchProcessor): 78 78 """A batch processor for IApplicant objects. 79 80 In create mode container_code is required. If application_number is given 81 an applicant with this number is created in the designated container. 82 If application_number is not given a random application_number is assigned. 83 applicant_id is being determined by the system and can't be imported. 84 85 In update or remove mode container_code and application_number columns 86 must not exist. The applicant object is solely searched by its applicant_id 87 or reg_number. 79 88 """ 80 89 grok.implements(IBatchProcessor) … … 85 94 name = u'Applicant Processor' 86 95 iface = IApplicant 87 location_fields = [' container_code']96 location_fields = [''] 88 97 factory_name = 'waeup.Applicant' 89 98 … … 93 102 def available_fields(self): 94 103 return sorted(list(set( 95 ['application_number', 'reg_number',104 ['application_number', 96 105 'container_code','state','password'] + getFields( 97 106 self.iface).keys()))) 98 107 99 108 def checkHeaders(self, headerfields, mode='create'): 100 if not 'reg_number' in headerfields and not 'application_number' \101 in headerfields:102 raise FatalCSVError(103 "Need at least columns application_number or reg_number ")109 cond1 = 'container_code' in headerfields 110 cond2 = 'application_number' in headerfields 111 cond3 = 'applicant_id' in headerfields 112 cond4 = 'reg_number' in headerfields 104 113 if mode == 'create': 114 if not cond1: 115 raise FatalCSVError( 116 "Need at least container_code column!") 117 if cond3: 118 raise FatalCSVError( 119 "applicant_id can't be imported in create mode!") 105 120 for field in self.required_fields: 106 121 if not field in headerfields: … … 108 123 "Need at least columns %s for import!" % 109 124 ', '.join(["'%s'" % x for x in self.required_fields])) 125 if mode in ('update', 'remove'): 126 if not cond3 and not cond4: 127 raise FatalCSVError( 128 "Need at least column reg_number or applicant_id!") 129 if cond1 or cond2: 130 raise FatalCSVError( 131 "container_code or application_number can't be imported " + 132 "in update or remove mode!") 110 133 # Check for fields to be ignored... 111 134 not_ignored_fields = [x for x in headerfields … … 117 140 118 141 def getLocator(self, row): 119 if row.get('application_number', None) not in (IGNORE_MARKER, None): 120 return 'application_number' 142 if row.get('container_code', None) not in (IGNORE_MARKER, None): 143 # create, update or remove 144 return 'container_code' 145 elif row.get('applicant_id', None) not in (IGNORE_MARKER, None): 146 # update or remove 147 return 'applicant_id' 121 148 elif row.get('reg_number', None) not in (IGNORE_MARKER, None): 149 # update or remove 122 150 return 'reg_number' 123 151 else: … … 125 153 126 154 def getParent(self, row, site): 127 if not 'applicants' in site.keys(): 128 return False 129 return site['applicants'][row['container_code']] 155 if self.getLocator(row) == 'container_code': 156 return site['applicants'].get(row['container_code'], None) 157 if self.getLocator(row) == 'reg_number': 158 reg_number = row['reg_number'] 159 cat = queryUtility(ICatalog, name='applicants_catalog') 160 results = list( 161 cat.searchResults(reg_number=(reg_number, reg_number))) 162 if results: 163 return results[0].__parent__ 164 if self.getLocator(row) == 'applicant_id': 165 applicant_id = row['applicant_id'] 166 cat = queryUtility(ICatalog, name='applicants_catalog') 167 results = list( 168 cat.searchResults(applicant_id=(applicant_id, applicant_id))) 169 if results: 170 return results[0].__parent__ 171 return None 130 172 131 173 def parentsExist(self, row, site): … … 133 175 134 176 def getEntry(self, row, site): 135 if not self.parentsExist(row, site): 177 if self.getLocator(row) == 'container_code': 178 if row.get('application_number', None) not in (IGNORE_MARKER, None): 179 if not self.parentsExist(row, site): 180 return None 181 parent = self.getParent(row, site) 182 return parent.get(row['application_number']) 136 183 return None 137 parent = self.getParent(row, site) 138 if self.getLocator(row) == 'application_number': 139 if row['application_number'] in parent: 140 applicant = parent[row['application_number']] 141 return applicant 142 elif self.getLocator(row) == 'reg_number': 184 if self.getLocator(row) == 'applicant_id': 185 applicant_id = row['applicant_id'] 186 cat = queryUtility(ICatalog, name='applicants_catalog') 187 results = list( 188 cat.searchResults(applicant_id=(applicant_id, applicant_id))) 189 if results: 190 return results[0] 191 if self.getLocator(row) == 'reg_number': 143 192 reg_number = row['reg_number'] 144 193 cat = queryUtility(ICatalog, name='applicants_catalog') … … 167 216 applicant = self.getEntry(row, site) 168 217 if applicant is not None: 169 parent = self.getParent(row, site)218 parent = applicant.__parent__ 170 219 del parent[applicant.application_number] 171 220 pass … … 227 276 raw_header = reader.next() 228 277 for num, field in enumerate(headerfields): 229 if field not in ['container_code', 230 'application_number', 'reg_number'] and mode == 'remove': 278 if field not in ['applicant_id', 'reg_number'] and mode == 'remove': 231 279 continue 232 280 if field == u'--IGNORE--': … … 239 287 """Validates all values in row. 240 288 """ 241 if mode in ['update', 'remove']: 242 if self.getLocator(row) == 'reg_number': 243 iface = IApplicantUpdateByRegNo 244 else: 245 iface = self.iface 289 iface = self.iface 290 if self.getLocator(row) == 'reg_number' or mode == 'remove': 291 iface = IApplicantUpdateByRegNo 246 292 converter = IObjectConverter(iface) 247 293 errs, inv_errs, conv_dict = converter.fromStringDict( … … 255 301 # be changed if empty 256 302 conv_dict['state'] = IGNORE_MARKER 257 application_number = row.get('application_number', IGNORE_MARKER)303 application_number = row.get('application_number', None) 258 304 if application_number in (IGNORE_MARKER, ''): 259 305 conv_dict['application_number'] = IGNORE_MARKER -
main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/sample_applicant_data_update.csv
r8290 r8331 1 container_code,reg_number,firstname,middlename,state2 dp2011,1001,Aaren,,3 dp2011 ,1002,Alfons,,admitted4 dp2011,1003,Abraham,,1 applicant_id,reg_number,firstname,middlename,state 2 ,1001,Aaren,, 3 dp2011_2345,6666,Alfons,,admitted 4 ,1003,Abraham,, -
main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/sample_applicant_data_update2.csv
r8290 r8331 1 container_code,reg_number,firstname,middlename,application_number 2 dp2011 ,1001,Aaren,XXX,3 dp2011,1002,Alfons,XXX, 4 dp2011,1003,Abraham,XXX, 1 applicant_id,reg_number,firstname,middlename 2 dp2011_1234,,Aaren,XXX 3 ,6666,Alfons,XXX 4 ,1003,Abraham,XXX -
main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_batching.py
r8311 r8331 24 24 import tempfile 25 25 import unittest 26 import grok 26 27 from zope.component.hooks import setSite, clearSite 27 28 from zope.component import createObject 28 29 from zope.interface.verify import verifyClass, verifyObject 30 from zope.event import notify 29 31 30 32 from waeup.kofa.app import University … … 68 70 69 71 APPLICANT_HEADER_FIELDS_UPDATE = APPLICANT_SAMPLE_DATA_UPDATE.split( 72 '\n')[0].split(',') 73 74 APPLICANT_HEADER_FIELDS_UPDATE2 = APPLICANT_SAMPLE_DATA_UPDATE2.split( 70 75 '\n')[0].split(',') 71 76 … … 310 315 num, num_warns, fin_file, fail_file = self.processor.doImport( 311 316 self.csv_file_update, APPLICANT_HEADER_FIELDS_UPDATE, 'update') 312 #content = open(fail_file).read()313 317 self.assertEqual(num_warns,0) 314 318 # The middlename import value was None. … … 324 328 # password of Finau is still unset 325 329 self.assertEqual(IUserAccount(container['2345']).password,'') 326 shutil.rmtree(os.path.dirname(fin_file)) 327 # Now we import another file which clears all middlename attributes. 328 num, num_warns, fin_file, fail_file = self.processor.doImport( 329 self.csv_file_update2, APPLICANT_HEADER_FIELDS_UPDATE, 'update') 330 # reg_number of Finau has changed 331 self.assertEqual(container['2345'].reg_number, '6666') 332 shutil.rmtree(os.path.dirname(fin_file)) 333 # We have to inform the catalog that the reg_number has changed 334 notify(grok.ObjectModifiedEvent(container['2345'])) 335 # Now we import another file which clears all middlename attributes 336 # and uses the new reg_number as locator 337 num, num_warns, fin_file, fail_file = self.processor.doImport( 338 self.csv_file_update2, APPLICANT_HEADER_FIELDS_UPDATE2, 'update') 339 #content = open(fail_file).read() 340 #print content 330 341 self.assertEqual(num_warns,0) 331 342 assert container['1234'].middlename is None
Note: See TracChangeset for help on using the changeset viewer.