[7191] | 1 | ## $Id: batching.py 8349 2012-05-04 22:49:57Z uli $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[7433] | 18 | """Batch processing components for student objects. |
---|
[6821] | 19 | |
---|
| 20 | Batch processors eat CSV files to add, update or remove large numbers |
---|
| 21 | of certain kinds of objects at once. |
---|
| 22 | |
---|
[7261] | 23 | Here we define the processors for students specific objects like |
---|
| 24 | students, studycourses, payment tickets and accommodation tickets. |
---|
[6821] | 25 | """ |
---|
| 26 | import grok |
---|
[6849] | 27 | import csv |
---|
[6821] | 28 | from zope.interface import Interface |
---|
[6825] | 29 | from zope.schema import getFields |
---|
[7548] | 30 | from zope.component import queryUtility, getUtility |
---|
[7429] | 31 | from zope.event import notify |
---|
[6825] | 32 | from zope.catalog.interfaces import ICatalog |
---|
[7951] | 33 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo |
---|
[7811] | 34 | from waeup.kofa.interfaces import ( |
---|
[7522] | 35 | IBatchProcessor, FatalCSVError, IObjectConverter, IUserAccount, |
---|
[8300] | 36 | IObjectHistory, VALIDATED, IGNORE_MARKER) |
---|
[7959] | 37 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7811] | 38 | from waeup.kofa.students.interfaces import ( |
---|
[7532] | 39 | IStudent, IStudentStudyCourse, |
---|
[7536] | 40 | IStudentUpdateByRegNo, IStudentUpdateByMatricNo, |
---|
[7623] | 41 | IStudentStudyLevel, ICourseTicket, |
---|
[8174] | 42 | IStudentOnlinePayment, IStudentVerdictUpdate) |
---|
[8309] | 43 | from waeup.kofa.students.workflow import ( |
---|
| 44 | IMPORTABLE_STATES, IMPORTABLE_TRANSITIONS) |
---|
[7811] | 45 | from waeup.kofa.utils.batching import BatchProcessor |
---|
[6821] | 46 | |
---|
| 47 | class StudentProcessor(BatchProcessor): |
---|
| 48 | """A batch processor for IStudent objects. |
---|
| 49 | """ |
---|
| 50 | grok.implements(IBatchProcessor) |
---|
| 51 | grok.provides(IBatchProcessor) |
---|
| 52 | grok.context(Interface) |
---|
[7933] | 53 | util_name = 'studentprocessor' |
---|
[6821] | 54 | grok.name(util_name) |
---|
| 55 | |
---|
[7933] | 56 | name = u'Student Processor' |
---|
[6821] | 57 | iface = IStudent |
---|
| 58 | |
---|
[6849] | 59 | location_fields = [] |
---|
[6821] | 60 | factory_name = 'waeup.Student' |
---|
| 61 | |
---|
| 62 | @property |
---|
[6849] | 63 | def available_fields(self): |
---|
[8176] | 64 | fields = getFields(self.iface) |
---|
[6849] | 65 | return sorted(list(set( |
---|
[7513] | 66 | ['student_id','reg_number','matric_number', |
---|
[8309] | 67 | 'password', 'state', 'transition'] + fields.keys()))) |
---|
[6821] | 68 | |
---|
[6849] | 69 | def checkHeaders(self, headerfields, mode='create'): |
---|
[8309] | 70 | if 'state' in headerfields and 'transition' in headerfields: |
---|
| 71 | raise FatalCSVError( |
---|
| 72 | "State and transition can't be imported at the same time!") |
---|
[6854] | 73 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 74 | in headerfields and not 'matric_number' in headerfields: |
---|
[6849] | 75 | raise FatalCSVError( |
---|
[6854] | 76 | "Need at least columns student_id or reg_number " + |
---|
| 77 | "or matric_number for import!") |
---|
[6849] | 78 | if mode == 'create': |
---|
| 79 | for field in self.required_fields: |
---|
| 80 | if not field in headerfields: |
---|
| 81 | raise FatalCSVError( |
---|
| 82 | "Need at least columns %s for import!" % |
---|
| 83 | ', '.join(["'%s'" % x for x in self.required_fields])) |
---|
| 84 | # Check for fields to be ignored... |
---|
| 85 | not_ignored_fields = [x for x in headerfields |
---|
| 86 | if not x.startswith('--')] |
---|
| 87 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 88 | raise FatalCSVError( |
---|
| 89 | "Double headers: each column name may only appear once.") |
---|
| 90 | return True |
---|
| 91 | |
---|
[6821] | 92 | def parentsExist(self, row, site): |
---|
| 93 | return 'students' in site.keys() |
---|
| 94 | |
---|
[6849] | 95 | def getLocator(self, row): |
---|
[8232] | 96 | if row.get('student_id',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 97 | return 'student_id' |
---|
[8232] | 98 | elif row.get('reg_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 99 | return 'reg_number' |
---|
[8232] | 100 | elif row.get('matric_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 101 | return 'matric_number' |
---|
| 102 | else: |
---|
| 103 | return None |
---|
| 104 | |
---|
[6821] | 105 | # The entry never exists in create mode. |
---|
| 106 | def entryExists(self, row, site): |
---|
[7267] | 107 | return self.getEntry(row, site) is not None |
---|
| 108 | |
---|
| 109 | def getParent(self, row, site): |
---|
| 110 | return site['students'] |
---|
| 111 | |
---|
| 112 | def getEntry(self, row, site): |
---|
[6846] | 113 | if not 'students' in site.keys(): |
---|
[6849] | 114 | return None |
---|
| 115 | if self.getLocator(row) == 'student_id': |
---|
[6846] | 116 | if row['student_id'] in site['students']: |
---|
| 117 | student = site['students'][row['student_id']] |
---|
| 118 | return student |
---|
[6849] | 119 | elif self.getLocator(row) == 'reg_number': |
---|
[6846] | 120 | reg_number = row['reg_number'] |
---|
| 121 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 122 | results = list( |
---|
| 123 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 124 | if results: |
---|
| 125 | return results[0] |
---|
[6849] | 126 | elif self.getLocator(row) == 'matric_number': |
---|
[6846] | 127 | matric_number = row['matric_number'] |
---|
| 128 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 129 | results = list( |
---|
| 130 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 131 | if results: |
---|
| 132 | return results[0] |
---|
[6849] | 133 | return None |
---|
[6821] | 134 | |
---|
| 135 | def addEntry(self, obj, row, site): |
---|
| 136 | parent = self.getParent(row, site) |
---|
| 137 | parent.addStudent(obj) |
---|
[8287] | 138 | # We have to log this if state is provided. If not, |
---|
[7959] | 139 | # logging is done by the event handler handle_student_added |
---|
[8287] | 140 | if row.has_key('state'): |
---|
[7959] | 141 | parent.logger.info('%s - Student record created' % obj.student_id) |
---|
[7656] | 142 | history = IObjectHistory(obj) |
---|
[7959] | 143 | history.addMessage(_('Student record created')) |
---|
[6821] | 144 | return |
---|
| 145 | |
---|
| 146 | def delEntry(self, row, site): |
---|
[7267] | 147 | student = self.getEntry(row, site) |
---|
[7263] | 148 | if student is not None: |
---|
[6846] | 149 | parent = self.getParent(row, site) |
---|
[7656] | 150 | parent.logger.info('%s - Student removed' % student.student_id) |
---|
[6846] | 151 | del parent[student.student_id] |
---|
[6821] | 152 | pass |
---|
[6825] | 153 | |
---|
[8309] | 154 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 155 | """Checks requirements the object must fulfill when being updated. |
---|
| 156 | |
---|
| 157 | This method is not used in case of deleting or adding objects. |
---|
| 158 | |
---|
| 159 | Returns error messages as strings in case of requirement |
---|
| 160 | problems. |
---|
| 161 | """ |
---|
| 162 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 163 | if transition not in (IGNORE_MARKER, ''): |
---|
| 164 | allowed_transitions = IWorkflowInfo(obj).getManualTransitionIds() |
---|
| 165 | if transition not in allowed_transitions: |
---|
| 166 | return 'Transition not allowed.' |
---|
| 167 | return None |
---|
| 168 | |
---|
[7497] | 169 | def updateEntry(self, obj, row, site): |
---|
| 170 | """Update obj to the values given in row. |
---|
| 171 | """ |
---|
[8221] | 172 | items_changed = '' |
---|
| 173 | |
---|
[7643] | 174 | # Remove student_id from row if empty |
---|
[8232] | 175 | if row.has_key('student_id') and row['student_id'] in ( |
---|
| 176 | None, IGNORE_MARKER): |
---|
[7643] | 177 | row.pop('student_id') |
---|
[8221] | 178 | |
---|
| 179 | # Update password |
---|
[8232] | 180 | passwd = row.get('password', IGNORE_MARKER) |
---|
| 181 | if passwd not in ('', IGNORE_MARKER): |
---|
[8347] | 182 | if passwd.startswith('{SSHA}'): |
---|
| 183 | # already encrypted password |
---|
| 184 | obj.password = passwd |
---|
| 185 | else: |
---|
| 186 | # not yet encrypted password |
---|
| 187 | IUserAccount(obj).setPassword(passwd) |
---|
[8221] | 188 | row.pop('password') |
---|
| 189 | |
---|
| 190 | # Update registration state |
---|
[8287] | 191 | state = row.get('state', IGNORE_MARKER) |
---|
| 192 | if state not in (IGNORE_MARKER, ''): |
---|
| 193 | value = row['state'] |
---|
[8221] | 194 | IWorkflowState(obj).setState(value) |
---|
| 195 | msg = _("State '${a}' set", mapping = {'a':value}) |
---|
| 196 | history = IObjectHistory(obj) |
---|
| 197 | history.addMessage(msg) |
---|
[8287] | 198 | row.pop('state') |
---|
[8309] | 199 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 200 | if transition not in (IGNORE_MARKER, ''): |
---|
| 201 | value = row['transition'] |
---|
| 202 | IWorkflowInfo(obj).fireTransition(value) |
---|
| 203 | row.pop('transition') |
---|
[8221] | 204 | |
---|
| 205 | # apply other values... |
---|
| 206 | items_changed = super(StudentProcessor, self).updateEntry( |
---|
| 207 | obj, row, site) |
---|
| 208 | |
---|
| 209 | # Log actions... |
---|
[7656] | 210 | parent = self.getParent(row, site) |
---|
| 211 | if hasattr(obj,'student_id'): |
---|
| 212 | # Update mode: the student exists and we can get the student_id |
---|
| 213 | parent.logger.info( |
---|
| 214 | '%s - Student record updated: %s' |
---|
| 215 | % (obj.student_id, items_changed)) |
---|
| 216 | else: |
---|
| 217 | # Create mode: the student does not yet exist |
---|
| 218 | parent.logger.info('Student data imported: %s' % items_changed) |
---|
[8221] | 219 | return items_changed |
---|
[7497] | 220 | |
---|
[6849] | 221 | def getMapping(self, path, headerfields, mode): |
---|
| 222 | """Get a mapping from CSV file headerfields to actually used fieldnames. |
---|
| 223 | """ |
---|
| 224 | result = dict() |
---|
| 225 | reader = csv.reader(open(path, 'rb')) |
---|
| 226 | raw_header = reader.next() |
---|
| 227 | for num, field in enumerate(headerfields): |
---|
[8221] | 228 | if field not in ['student_id', 'reg_number', 'matric_number' |
---|
| 229 | ] and mode == 'remove': |
---|
[6849] | 230 | continue |
---|
| 231 | if field == u'--IGNORE--': |
---|
| 232 | # Skip ignored columns in failed and finished data files. |
---|
| 233 | continue |
---|
| 234 | result[raw_header[num]] = field |
---|
| 235 | return result |
---|
| 236 | |
---|
| 237 | def checkConversion(self, row, mode='create'): |
---|
| 238 | """Validates all values in row. |
---|
| 239 | """ |
---|
[7643] | 240 | iface = self.iface |
---|
[6849] | 241 | if mode in ['update', 'remove']: |
---|
| 242 | if self.getLocator(row) == 'reg_number': |
---|
| 243 | iface = IStudentUpdateByRegNo |
---|
| 244 | elif self.getLocator(row) == 'matric_number': |
---|
| 245 | iface = IStudentUpdateByMatricNo |
---|
| 246 | converter = IObjectConverter(iface) |
---|
| 247 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
[8214] | 248 | row, self.factory_name, mode=mode) |
---|
[8309] | 249 | if row.has_key('transition') and \ |
---|
| 250 | not row['transition'] in IMPORTABLE_TRANSITIONS: |
---|
| 251 | if row['transition'] not in (IGNORE_MARKER, ''): |
---|
| 252 | errs.append(('transition','not allowed')) |
---|
[8287] | 253 | if row.has_key('state') and \ |
---|
| 254 | not row['state'] in IMPORTABLE_STATES: |
---|
| 255 | if row['state'] not in (IGNORE_MARKER, ''): |
---|
| 256 | errs.append(('state','not allowed')) |
---|
| 257 | else: |
---|
| 258 | # state is an attribute of Student and must not |
---|
| 259 | # be changed if empty |
---|
| 260 | conv_dict['state'] = IGNORE_MARKER |
---|
[6849] | 261 | return errs, inv_errs, conv_dict |
---|
| 262 | |
---|
[8232] | 263 | |
---|
| 264 | class StudentProcessorBase(BatchProcessor): |
---|
| 265 | """A base for student subitem processor. |
---|
| 266 | |
---|
| 267 | Helps reducing redundancy. |
---|
[6825] | 268 | """ |
---|
[8232] | 269 | grok.baseclass() |
---|
[6825] | 270 | |
---|
[8232] | 271 | #: required fields beside 'student_id', 'reg_number' and 'matric_number' |
---|
| 272 | additional_fields = [] |
---|
[6825] | 273 | |
---|
[8232] | 274 | #: header fields additional required |
---|
| 275 | additional_headers = [] |
---|
[6849] | 276 | |
---|
[6825] | 277 | @property |
---|
| 278 | def available_fields(self): |
---|
[8232] | 279 | fields = ['student_id','reg_number','matric_number' |
---|
| 280 | ] + self.additional_fields |
---|
| 281 | return sorted(list(set(fields + getFields( |
---|
[6843] | 282 | self.iface).keys()))) |
---|
[6825] | 283 | |
---|
[6837] | 284 | def checkHeaders(self, headerfields, mode='ignore'): |
---|
[6854] | 285 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 286 | in headerfields and not 'matric_number' in headerfields: |
---|
[6825] | 287 | raise FatalCSVError( |
---|
[6854] | 288 | "Need at least columns student_id " + |
---|
| 289 | "or reg_number or matric_number for import!") |
---|
[8232] | 290 | for name in self.additional_headers: |
---|
| 291 | if not name in headerfields: |
---|
| 292 | raise FatalCSVError( |
---|
| 293 | "Need %s for import!" % name) |
---|
| 294 | |
---|
[6834] | 295 | # Check for fields to be ignored... |
---|
[6825] | 296 | not_ignored_fields = [x for x in headerfields |
---|
| 297 | if not x.startswith('--')] |
---|
| 298 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 299 | raise FatalCSVError( |
---|
| 300 | "Double headers: each column name may only appear once.") |
---|
| 301 | return True |
---|
| 302 | |
---|
[8232] | 303 | def _getStudent(self, row, site): |
---|
[8225] | 304 | NON_VALUES = ['', IGNORE_MARKER] |
---|
[6846] | 305 | if not 'students' in site.keys(): |
---|
[6849] | 306 | return None |
---|
[8225] | 307 | if row.get('student_id', '') not in NON_VALUES: |
---|
[6825] | 308 | if row['student_id'] in site['students']: |
---|
| 309 | student = site['students'][row['student_id']] |
---|
| 310 | return student |
---|
[8225] | 311 | elif row.get('reg_number', '') not in NON_VALUES: |
---|
[6825] | 312 | reg_number = row['reg_number'] |
---|
| 313 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 314 | results = list( |
---|
| 315 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 316 | if results: |
---|
| 317 | return results[0] |
---|
[8225] | 318 | elif row.get('matric_number', '') not in NON_VALUES: |
---|
[6843] | 319 | matric_number = row['matric_number'] |
---|
| 320 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 321 | results = list( |
---|
| 322 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 323 | if results: |
---|
| 324 | return results[0] |
---|
[6849] | 325 | return None |
---|
[6825] | 326 | |
---|
[7267] | 327 | def parentsExist(self, row, site): |
---|
| 328 | return self.getParent(row, site) is not None |
---|
| 329 | |
---|
[6825] | 330 | def entryExists(self, row, site): |
---|
[7534] | 331 | return self.getEntry(row, site) is not None |
---|
[6825] | 332 | |
---|
[8232] | 333 | def checkConversion(self, row, mode='ignore'): |
---|
| 334 | """Validates all values in row. |
---|
| 335 | """ |
---|
| 336 | converter = IObjectConverter(self.iface) |
---|
| 337 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
| 338 | row, self.factory_name, mode=mode) |
---|
| 339 | return errs, inv_errs, conv_dict |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | class StudentStudyCourseProcessor(StudentProcessorBase): |
---|
| 343 | """A batch processor for IStudentStudyCourse objects. |
---|
| 344 | """ |
---|
| 345 | grok.implements(IBatchProcessor) |
---|
| 346 | grok.provides(IBatchProcessor) |
---|
| 347 | grok.context(Interface) |
---|
| 348 | util_name = 'studycourseupdater' |
---|
| 349 | grok.name(util_name) |
---|
| 350 | |
---|
| 351 | name = u'StudentStudyCourse Processor (update only)' |
---|
| 352 | iface = IStudentStudyCourse |
---|
| 353 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 354 | |
---|
| 355 | location_fields = [] |
---|
| 356 | additional_fields = [] |
---|
| 357 | |
---|
| 358 | def getParent(self, row, site): |
---|
| 359 | return self._getStudent(row, site) |
---|
| 360 | |
---|
[6825] | 361 | def getEntry(self, row, site): |
---|
[7534] | 362 | student = self.getParent(row, site) |
---|
[7536] | 363 | if student is None: |
---|
[6825] | 364 | return None |
---|
| 365 | return student.get('studycourse') |
---|
[7429] | 366 | |
---|
| 367 | def updateEntry(self, obj, row, site): |
---|
| 368 | """Update obj to the values given in row. |
---|
| 369 | """ |
---|
[8221] | 370 | items_changed = super(StudentStudyCourseProcessor, self).updateEntry( |
---|
| 371 | obj, row, site) |
---|
[7656] | 372 | parent = self.getParent(row, site) |
---|
| 373 | parent.__parent__.logger.info( |
---|
| 374 | '%s - Study course updated: %s' |
---|
| 375 | % (parent.student_id, items_changed)) |
---|
[7429] | 376 | # Update the students_catalog |
---|
| 377 | notify(grok.ObjectModifiedEvent(obj.__parent__)) |
---|
| 378 | return |
---|
| 379 | |
---|
[7532] | 380 | def checkConversion(self, row, mode='ignore'): |
---|
| 381 | """Validates all values in row. |
---|
| 382 | """ |
---|
[8232] | 383 | errs, inv_errs, conv_dict = super( |
---|
| 384 | StudentStudyCourseProcessor, self).checkConversion(row, mode=mode) |
---|
[7532] | 385 | # We have to check if current_level is in range of certificate. |
---|
[7534] | 386 | if conv_dict.has_key('certificate'): |
---|
[8221] | 387 | cert = conv_dict['certificate'] |
---|
| 388 | if conv_dict['current_level'] < cert.start_level or \ |
---|
| 389 | conv_dict['current_level'] > cert.end_level+120: |
---|
[7534] | 390 | errs.append(('current_level','not in range')) |
---|
[7532] | 391 | return errs, inv_errs, conv_dict |
---|
| 392 | |
---|
[8232] | 393 | class StudentStudyLevelProcessor(StudentProcessorBase): |
---|
[7536] | 394 | """A batch processor for IStudentStudyLevel objects. |
---|
| 395 | """ |
---|
| 396 | grok.implements(IBatchProcessor) |
---|
| 397 | grok.provides(IBatchProcessor) |
---|
| 398 | grok.context(Interface) |
---|
[7933] | 399 | util_name = 'studylevelprocessor' |
---|
[7536] | 400 | grok.name(util_name) |
---|
| 401 | |
---|
[7933] | 402 | name = u'StudentStudyLevel Processor' |
---|
[7536] | 403 | iface = IStudentStudyLevel |
---|
| 404 | factory_name = 'waeup.StudentStudyLevel' |
---|
| 405 | |
---|
| 406 | location_fields = [] |
---|
[8232] | 407 | additional_fields = ['level'] |
---|
| 408 | additional_headers = ['level'] |
---|
[7536] | 409 | |
---|
| 410 | def getParent(self, row, site): |
---|
[8232] | 411 | student = self._getStudent(row, site) |
---|
| 412 | if student is None: |
---|
[7536] | 413 | return None |
---|
[8232] | 414 | return student['studycourse'] |
---|
[7536] | 415 | |
---|
| 416 | def getEntry(self, row, site): |
---|
| 417 | studycourse = self.getParent(row, site) |
---|
| 418 | if studycourse is None: |
---|
| 419 | return None |
---|
| 420 | return studycourse.get(row['level']) |
---|
| 421 | |
---|
| 422 | def addEntry(self, obj, row, site): |
---|
| 423 | parent = self.getParent(row, site) |
---|
| 424 | obj.level = int(row['level']) |
---|
| 425 | parent[row['level']] = obj |
---|
| 426 | return |
---|
| 427 | |
---|
| 428 | def checkConversion(self, row, mode='ignore'): |
---|
| 429 | """Validates all values in row. |
---|
| 430 | """ |
---|
[8232] | 431 | errs, inv_errs, conv_dict = super( |
---|
| 432 | StudentStudyLevelProcessor, self).checkConversion(row, mode=mode) |
---|
| 433 | |
---|
[7536] | 434 | # We have to check if level is a valid integer. |
---|
[7548] | 435 | # This is not done by the converter. |
---|
[7536] | 436 | try: |
---|
| 437 | level = int(row['level']) |
---|
[7612] | 438 | if level not in range(0,700,10): |
---|
[7536] | 439 | errs.append(('level','no valid integer')) |
---|
| 440 | except ValueError: |
---|
| 441 | errs.append(('level','no integer')) |
---|
| 442 | return errs, inv_errs, conv_dict |
---|
[7548] | 443 | |
---|
[8232] | 444 | class CourseTicketProcessor(StudentProcessorBase): |
---|
[7548] | 445 | """A batch processor for ICourseTicket objects. |
---|
| 446 | """ |
---|
| 447 | grok.implements(IBatchProcessor) |
---|
| 448 | grok.provides(IBatchProcessor) |
---|
| 449 | grok.context(Interface) |
---|
[7933] | 450 | util_name = 'courseticketprocessor' |
---|
[7548] | 451 | grok.name(util_name) |
---|
| 452 | |
---|
[7933] | 453 | name = u'CourseTicket Processor' |
---|
[7548] | 454 | iface = ICourseTicket |
---|
| 455 | factory_name = 'waeup.CourseTicket' |
---|
| 456 | |
---|
| 457 | location_fields = [] |
---|
[8232] | 458 | additional_fields = ['level', 'code'] |
---|
| 459 | additional_headers = ['level', 'code'] |
---|
[7548] | 460 | |
---|
| 461 | def getParent(self, row, site): |
---|
[8232] | 462 | student = self._getStudent(row, site) |
---|
| 463 | if student is None: |
---|
[7548] | 464 | return None |
---|
[8232] | 465 | return student['studycourse'].get(row['level']) |
---|
[7548] | 466 | |
---|
| 467 | def getEntry(self, row, site): |
---|
| 468 | level = self.getParent(row, site) |
---|
| 469 | if level is None: |
---|
| 470 | return None |
---|
| 471 | return level.get(row['code']) |
---|
| 472 | |
---|
| 473 | def addEntry(self, obj, row, site): |
---|
| 474 | parent = self.getParent(row, site) |
---|
| 475 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 476 | entries = list(catalog.searchResults(code=(row['code'],row['code']))) |
---|
| 477 | obj.fcode = entries[0].__parent__.__parent__.__parent__.code |
---|
| 478 | obj.dcode = entries[0].__parent__.__parent__.code |
---|
| 479 | obj.title = entries[0].title |
---|
| 480 | obj.credits = entries[0].credits |
---|
| 481 | obj.passmark = entries[0].passmark |
---|
| 482 | obj.semester = entries[0].semester |
---|
| 483 | parent[row['code']] = obj |
---|
| 484 | return |
---|
| 485 | |
---|
| 486 | def checkConversion(self, row, mode='ignore'): |
---|
| 487 | """Validates all values in row. |
---|
| 488 | """ |
---|
[8232] | 489 | errs, inv_errs, conv_dict = super( |
---|
| 490 | CourseTicketProcessor, self).checkConversion(row, mode=mode) |
---|
| 491 | |
---|
[7548] | 492 | # We have to check if course really exists. |
---|
| 493 | # This is not done by the converter. |
---|
| 494 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 495 | entries = catalog.searchResults(code=(row['code'],row['code'])) |
---|
| 496 | if len(entries) == 0: |
---|
| 497 | errs.append(('code','non-existent')) |
---|
| 498 | return errs, inv_errs, conv_dict |
---|
[7623] | 499 | return errs, inv_errs, conv_dict |
---|
| 500 | |
---|
[8232] | 501 | class StudentOnlinePaymentProcessor(StudentProcessorBase): |
---|
[7623] | 502 | """A batch processor for IStudentOnlinePayment objects. |
---|
| 503 | """ |
---|
| 504 | grok.implements(IBatchProcessor) |
---|
| 505 | grok.provides(IBatchProcessor) |
---|
| 506 | grok.context(Interface) |
---|
[7933] | 507 | util_name = 'paymentprocessor' |
---|
[7623] | 508 | grok.name(util_name) |
---|
| 509 | |
---|
[7933] | 510 | name = u'Payment Processor' |
---|
[8174] | 511 | iface = IStudentOnlinePayment |
---|
[7623] | 512 | factory_name = 'waeup.StudentOnlinePayment' |
---|
| 513 | |
---|
| 514 | location_fields = [] |
---|
[8232] | 515 | additional_fields = ['p_id'] |
---|
| 516 | additional_headers = ['p_id'] |
---|
[7623] | 517 | |
---|
[8232] | 518 | def parentsExist(self, row, site): |
---|
| 519 | return self.getParent(row, site) is not None |
---|
[7623] | 520 | |
---|
| 521 | def getParent(self, row, site): |
---|
[8232] | 522 | student = self._getStudent(row, site) |
---|
| 523 | if student is None: |
---|
[7623] | 524 | return None |
---|
[8232] | 525 | return student['payments'] |
---|
[7623] | 526 | |
---|
| 527 | def getEntry(self, row, site): |
---|
| 528 | payments = self.getParent(row, site) |
---|
| 529 | if payments is None: |
---|
| 530 | return None |
---|
[7626] | 531 | # We can use the hash symbol at the end of p_id in import files |
---|
| 532 | # to avoid annoying automatic number transformation |
---|
| 533 | # by Excel or Calc |
---|
| 534 | p_id = row['p_id'].strip('#') |
---|
| 535 | if p_id.startswith('p'): |
---|
| 536 | entry = payments.get(p_id) |
---|
[7623] | 537 | else: |
---|
| 538 | # For data migration from old SRP |
---|
[7626] | 539 | entry = payments.get('p' + p_id[6:]) |
---|
[7623] | 540 | return entry |
---|
| 541 | |
---|
| 542 | def addEntry(self, obj, row, site): |
---|
| 543 | parent = self.getParent(row, site) |
---|
[7626] | 544 | p_id = row['p_id'].strip('#') |
---|
| 545 | if not p_id.startswith('p'): |
---|
[7623] | 546 | # For data migration from old SRP |
---|
[7626] | 547 | obj.p_id = 'p' + p_id[6:] |
---|
[7623] | 548 | parent[obj.p_id] = obj |
---|
| 549 | else: |
---|
[7626] | 550 | parent[p_id] = obj |
---|
[7623] | 551 | return |
---|
| 552 | |
---|
| 553 | def checkConversion(self, row, mode='ignore'): |
---|
| 554 | """Validates all values in row. |
---|
| 555 | """ |
---|
[8232] | 556 | errs, inv_errs, conv_dict = super( |
---|
| 557 | StudentOnlinePaymentProcessor, self).checkConversion(row, mode=mode) |
---|
| 558 | |
---|
[7623] | 559 | # We have to check p_id. |
---|
[7626] | 560 | p_id = row['p_id'].strip('#') |
---|
| 561 | if p_id.startswith('p'): |
---|
| 562 | if not len(p_id) == 14: |
---|
[7623] | 563 | errs.append(('p_id','invalid length')) |
---|
| 564 | return errs, inv_errs, conv_dict |
---|
| 565 | else: |
---|
[7626] | 566 | if not len(p_id) == 19: |
---|
[7623] | 567 | errs.append(('p_id','invalid length')) |
---|
| 568 | return errs, inv_errs, conv_dict |
---|
| 569 | return errs, inv_errs, conv_dict |
---|
[7951] | 570 | |
---|
| 571 | class StudentVerdictProcessor(StudentStudyCourseProcessor): |
---|
| 572 | """A batch processor for verdicts. |
---|
| 573 | |
---|
| 574 | Import verdicts and perform workflow transitions. |
---|
| 575 | """ |
---|
| 576 | |
---|
| 577 | util_name = 'verdictupdater' |
---|
| 578 | grok.name(util_name) |
---|
| 579 | |
---|
| 580 | name = u'Verdict Processor (update only)' |
---|
| 581 | iface = IStudentVerdictUpdate |
---|
| 582 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 583 | |
---|
[8232] | 584 | additional_fields = ['current_session', 'current_level'] |
---|
[7951] | 585 | |
---|
| 586 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 587 | """Checks requirements the studycourse and the student must fulfill |
---|
| 588 | before being updated. |
---|
| 589 | """ |
---|
| 590 | # Check if current_levels correspond |
---|
| 591 | if obj.current_level != row['current_level']: |
---|
| 592 | return 'Current level does not correspond.' |
---|
| 593 | # Check if current_sessions correspond |
---|
| 594 | if obj.current_session != row['current_session']: |
---|
| 595 | return 'Current session does not correspond.' |
---|
| 596 | # Check if student is in state REGISTERED |
---|
| 597 | if obj.getStudent().state != VALIDATED: |
---|
| 598 | return 'Student in wrong state.' |
---|
| 599 | return None |
---|
| 600 | |
---|
| 601 | def updateEntry(self, obj, row, site): |
---|
| 602 | """Update obj to the values given in row. |
---|
| 603 | """ |
---|
[8221] | 604 | # Don't set current_session, current_level |
---|
| 605 | vals_to_set = dict((key, val) for key, val in row.items() |
---|
| 606 | if key not in ('current_session','current_level')) |
---|
| 607 | items_changed = super(StudentVerdictProcessor, self).updateEntry( |
---|
| 608 | obj, vals_to_set, site) |
---|
[7951] | 609 | parent = self.getParent(row, site) |
---|
| 610 | parent.__parent__.logger.info( |
---|
| 611 | '%s - Verdict updated: %s' |
---|
| 612 | % (parent.student_id, items_changed)) |
---|
| 613 | # Fire transition |
---|
| 614 | IWorkflowInfo(obj.__parent__).fireTransition('return') |
---|
| 615 | # Update the students_catalog |
---|
| 616 | notify(grok.ObjectModifiedEvent(obj.__parent__)) |
---|
| 617 | return |
---|