- Timestamp:
- 20 Sep 2012, 08:49:37 (12 years ago)
- Location:
- main/waeup.kofa/branches/uli-zc-async
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/branches/uli-zc-async
- Property svn:mergeinfo changed
-
main/waeup.kofa/branches/uli-zc-async/src/waeup/kofa/students/batching.py
r8736 r9209 26 26 import grok 27 27 import csv 28 from time import time 28 29 from zope.interface import Interface 29 30 from zope.schema import getFields … … 42 43 IStudentOnlinePayment, IStudentVerdictUpdate) 43 44 from waeup.kofa.students.workflow import ( 44 IMPORTABLE_STATES, IMPORTABLE_TRANSITIONS) 45 IMPORTABLE_STATES, IMPORTABLE_TRANSITIONS, 46 FORBIDDEN_POSTGRAD_TRANS, FORBIDDEN_POSTGRAD_STATES) 45 47 from waeup.kofa.utils.batching import BatchProcessor 46 48 … … 170 172 if transition not in allowed_transitions: 171 173 return 'Transition not allowed.' 174 if transition in FORBIDDEN_POSTGRAD_TRANS and \ 175 obj.is_postgrad: 176 return 'Transition not allowed (pg student).' 177 state = row.get('state', IGNORE_MARKER) 178 if state not in (IGNORE_MARKER, ''): 179 if state in FORBIDDEN_POSTGRAD_STATES and \ 180 obj.is_postgrad: 181 return 'State not allowed (pg student).' 172 182 return None 173 183 … … 183 193 184 194 # Update password 195 # XXX: Tale DELETION_MARKER into consideration 185 196 if row.has_key('password'): 186 197 passwd = row.get('password', IGNORE_MARKER) … … 222 233 parent = self.getParent(row, site) 223 234 if hasattr(obj,'student_id'): 224 # Update mode: the student exists and we can get the student_id 235 # Update mode: the student exists and we can get the student_id. 236 # Create mode: the record contains the student_id 225 237 parent.logger.info( 226 238 '%s - Student record updated: %s' … … 259 271 errs, inv_errs, conv_dict = converter.fromStringDict( 260 272 row, self.factory_name, mode=mode) 261 if row.has_key('transition') and \ 262 not row['transition'] in IMPORTABLE_TRANSITIONS: 263 if row['transition'] not in (IGNORE_MARKER, ''): 264 errs.append(('transition','not allowed')) 265 if row.has_key('state') and \ 266 not row['state'] in IMPORTABLE_STATES: 267 if row['state'] not in (IGNORE_MARKER, ''): 268 errs.append(('state','not allowed')) 269 else: 270 # state is an attribute of Student and must not 271 # be changed if empty 272 conv_dict['state'] = IGNORE_MARKER 273 273 if row.has_key('transition'): 274 if row['transition'] not in IMPORTABLE_TRANSITIONS: 275 if row['transition'] not in (IGNORE_MARKER, ''): 276 errs.append(('transition','not allowed')) 277 if row.has_key('state'): 278 if row['state'] not in IMPORTABLE_STATES: 279 if row['state'] not in (IGNORE_MARKER, ''): 280 errs.append(('state','not allowed')) 281 else: 282 # State is an attribute of Student and must not 283 # be changed if empty. 284 conv_dict['state'] = IGNORE_MARKER 274 285 try: 275 286 # Correct stud_id counter. As the IConverter for students … … 291 302 grok.baseclass() 292 303 293 #: required fields beside 'student_id', 'reg_number' and 'matric_number' 304 # additional available fields 305 # beside 'student_id', 'reg_number' and 'matric_number' 294 306 additional_fields = [] 295 307 296 #: header fields additional required308 #: header fields additionally required 297 309 additional_headers = [] 298 310 … … 361 373 return errs, inv_errs, conv_dict 362 374 375 def getMapping(self, path, headerfields, mode): 376 """Get a mapping from CSV file headerfields to actually used fieldnames. 377 """ 378 result = dict() 379 reader = csv.reader(open(path, 'rb')) 380 raw_header = reader.next() 381 for num, field in enumerate(headerfields): 382 if field not in ['student_id', 'reg_number', 'matric_number', 383 'p_id', 'code', 'level' 384 ] and mode == 'remove': 385 continue 386 if field == u'--IGNORE--': 387 # Skip ignored columns in failed and finished data files. 388 continue 389 result[raw_header[num]] = field 390 return result 391 363 392 364 393 class StudentStudyCourseProcessor(StudentProcessorBase): … … 406 435 StudentStudyCourseProcessor, self).checkConversion(row, mode=mode) 407 436 # We have to check if current_level is in range of certificate. 408 if conv_dict.has_key('certificate'): 409 cert = conv_dict['certificate'] 410 if conv_dict['current_level'] < cert.start_level or \ 411 conv_dict['current_level'] > cert.end_level+120: 412 errs.append(('current_level','not in range')) 437 if conv_dict.has_key('certificate') and \ 438 conv_dict.has_key('current_level'): 439 cert = conv_dict['certificate'] 440 level = conv_dict['current_level'] 441 if level < cert.start_level or level > cert.end_level+120: 442 errs.append(('current_level','not in range')) 413 443 return errs, inv_errs, conv_dict 444 445 def checkUpdateRequirements(self, obj, row, site): 446 """Checks requirements the object must fulfill when being updated. 447 448 Returns error messages as strings in case of requirement 449 problems. 450 """ 451 current_level = row.get('current_level', None) 452 if current_level == 999 and \ 453 obj.__parent__.state in FORBIDDEN_POSTGRAD_STATES: 454 return 'Not a pg student.' 455 return None 414 456 415 457 class StudentStudyLevelProcessor(StudentProcessorBase): … … 427 469 428 470 location_fields = [] 471 429 472 additional_fields = ['level'] 430 473 additional_headers = ['level'] … … 509 552 items_changed = super(CourseTicketProcessor, self).updateEntry( 510 553 obj, row, site) 554 parent = self.getParent(row, site) 511 555 student = self.getParent(row, site).__parent__.__parent__ 512 556 student.__parent__.logger.info( 513 '%s - Course ticket updated: %s'514 % (student.student_id, items_changed))557 '%s - Course ticket in %s updated: %s' 558 % (student.student_id, parent.level, items_changed)) 515 559 return 516 560 … … 528 572 return 529 573 574 def delEntry(self, row, site): 575 ticket = self.getEntry(row, site) 576 parent = self.getParent(row, site) 577 if ticket is not None: 578 student = self._getStudent(row, site) 579 student.__parent__.logger.info('%s - Course ticket in %s removed: %s' 580 % (student.student_id, parent.level, ticket.code)) 581 del parent[ticket.code] 582 return 583 530 584 def checkConversion(self, row, mode='ignore'): 531 585 """Validates all values in row. … … 552 606 grok.name(util_name) 553 607 554 name = u' Payment Processor'608 name = u'Student Payment Processor' 555 609 iface = IStudentOnlinePayment 556 610 factory_name = 'waeup.StudentOnlinePayment' … … 558 612 location_fields = [] 559 613 additional_fields = ['p_id'] 560 additional_headers = ['p_id'] 614 additional_headers = [] 615 616 def checkHeaders(self, headerfields, mode='ignore'): 617 super(StudentOnlinePaymentProcessor, self).checkHeaders(headerfields) 618 if mode in ('update', 'remove') and not 'p_id' in headerfields: 619 raise FatalCSVError( 620 "Need p_id for import in update and remove modes!") 621 return True 561 622 562 623 def parentsExist(self, row, site): … … 573 634 if payments is None: 574 635 return None 636 p_id = row.get('p_id', None) 637 if p_id is None: 638 return None 575 639 # We can use the hash symbol at the end of p_id in import files 576 640 # to avoid annoying automatic number transformation 577 641 # by Excel or Calc 578 p_id = row['p_id'].strip('#') 579 if p_id.startswith('p'): 580 entry = payments.get(p_id) 581 else: 582 # For data migration from old SRP 583 entry = payments.get('p' + p_id[6:]) 642 p_id = p_id.strip('#') 643 if not p_id.startswith('p'): 644 # For data migration from old SRP only 645 p_id = 'p' + p_id[7:] + '0' 646 entry = payments.get(p_id) 584 647 return entry 585 648 … … 600 663 if not p_id.startswith('p'): 601 664 # For data migration from old SRP 602 obj.p_id = 'p' + p_id[ 6:]665 obj.p_id = 'p' + p_id[7:] + '0' 603 666 parent[obj.p_id] = obj 604 667 else: … … 606 669 return 607 670 671 def delEntry(self, row, site): 672 payment = self.getEntry(row, site) 673 parent = self.getParent(row, site) 674 if payment is not None: 675 student = self._getStudent(row, site) 676 student.__parent__.logger.info('%s - Payment ticket removed: %s' 677 % (student.student_id, payment.p_id)) 678 del parent[payment.p_id] 679 return 680 608 681 def checkConversion(self, row, mode='ignore'): 609 682 """Validates all values in row. … … 613 686 614 687 # We have to check p_id. 615 p_id = row['p_id'].strip('#') 688 p_id = row.get('p_id', None) 689 if not p_id: 690 timestamp = ("%d" % int(time()*10000))[1:] 691 p_id = "p%s" % timestamp 692 conv_dict['p_id'] = p_id 693 return errs, inv_errs, conv_dict 694 else: 695 p_id = p_id.strip('#') 616 696 if p_id.startswith('p'): 617 697 if not len(p_id) == 14:
Note: See TracChangeset for help on using the changeset viewer.