[7191] | 1 | ## $Id: batching.py 8626 2012-06-04 11:30:01Z henrik $ |
---|
| 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 |
---|
[8581] | 58 | iface_byregnumber = IStudentUpdateByRegNo |
---|
| 59 | iface_bymatricnumber = IStudentUpdateByMatricNo |
---|
[6821] | 60 | |
---|
[6849] | 61 | location_fields = [] |
---|
[6821] | 62 | factory_name = 'waeup.Student' |
---|
| 63 | |
---|
| 64 | @property |
---|
[6849] | 65 | def available_fields(self): |
---|
[8176] | 66 | fields = getFields(self.iface) |
---|
[6849] | 67 | return sorted(list(set( |
---|
[7513] | 68 | ['student_id','reg_number','matric_number', |
---|
[8309] | 69 | 'password', 'state', 'transition'] + fields.keys()))) |
---|
[6821] | 70 | |
---|
[6849] | 71 | def checkHeaders(self, headerfields, mode='create'): |
---|
[8309] | 72 | if 'state' in headerfields and 'transition' in headerfields: |
---|
| 73 | raise FatalCSVError( |
---|
| 74 | "State and transition can't be imported at the same time!") |
---|
[6854] | 75 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 76 | in headerfields and not 'matric_number' in headerfields: |
---|
[6849] | 77 | raise FatalCSVError( |
---|
[6854] | 78 | "Need at least columns student_id or reg_number " + |
---|
| 79 | "or matric_number for import!") |
---|
[6849] | 80 | if mode == 'create': |
---|
| 81 | for field in self.required_fields: |
---|
| 82 | if not field in headerfields: |
---|
| 83 | raise FatalCSVError( |
---|
| 84 | "Need at least columns %s for import!" % |
---|
| 85 | ', '.join(["'%s'" % x for x in self.required_fields])) |
---|
| 86 | # Check for fields to be ignored... |
---|
| 87 | not_ignored_fields = [x for x in headerfields |
---|
| 88 | if not x.startswith('--')] |
---|
| 89 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 90 | raise FatalCSVError( |
---|
| 91 | "Double headers: each column name may only appear once.") |
---|
| 92 | return True |
---|
| 93 | |
---|
[6821] | 94 | def parentsExist(self, row, site): |
---|
| 95 | return 'students' in site.keys() |
---|
| 96 | |
---|
[6849] | 97 | def getLocator(self, row): |
---|
[8232] | 98 | if row.get('student_id',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 99 | return 'student_id' |
---|
[8232] | 100 | elif row.get('reg_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 101 | return 'reg_number' |
---|
[8232] | 102 | elif row.get('matric_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 103 | return 'matric_number' |
---|
| 104 | else: |
---|
| 105 | return None |
---|
| 106 | |
---|
[6821] | 107 | # The entry never exists in create mode. |
---|
| 108 | def entryExists(self, row, site): |
---|
[7267] | 109 | return self.getEntry(row, site) is not None |
---|
| 110 | |
---|
| 111 | def getParent(self, row, site): |
---|
| 112 | return site['students'] |
---|
| 113 | |
---|
| 114 | def getEntry(self, row, site): |
---|
[6846] | 115 | if not 'students' in site.keys(): |
---|
[6849] | 116 | return None |
---|
| 117 | if self.getLocator(row) == 'student_id': |
---|
[6846] | 118 | if row['student_id'] in site['students']: |
---|
| 119 | student = site['students'][row['student_id']] |
---|
| 120 | return student |
---|
[6849] | 121 | elif self.getLocator(row) == 'reg_number': |
---|
[6846] | 122 | reg_number = row['reg_number'] |
---|
| 123 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 124 | results = list( |
---|
| 125 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 126 | if results: |
---|
| 127 | return results[0] |
---|
[6849] | 128 | elif self.getLocator(row) == 'matric_number': |
---|
[6846] | 129 | matric_number = row['matric_number'] |
---|
| 130 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 131 | results = list( |
---|
| 132 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 133 | if results: |
---|
| 134 | return results[0] |
---|
[6849] | 135 | return None |
---|
[6821] | 136 | |
---|
| 137 | def addEntry(self, obj, row, site): |
---|
| 138 | parent = self.getParent(row, site) |
---|
| 139 | parent.addStudent(obj) |
---|
[8491] | 140 | # Reset _curr_stud_id if student_id has been imported |
---|
| 141 | if self.getLocator(row) == 'student_id': |
---|
| 142 | parent._curr_stud_id -= 1 |
---|
[8287] | 143 | # We have to log this if state is provided. If not, |
---|
[7959] | 144 | # logging is done by the event handler handle_student_added |
---|
[8287] | 145 | if row.has_key('state'): |
---|
[7959] | 146 | parent.logger.info('%s - Student record created' % obj.student_id) |
---|
[7656] | 147 | history = IObjectHistory(obj) |
---|
[7959] | 148 | history.addMessage(_('Student record created')) |
---|
[6821] | 149 | return |
---|
| 150 | |
---|
| 151 | def delEntry(self, row, site): |
---|
[7267] | 152 | student = self.getEntry(row, site) |
---|
[7263] | 153 | if student is not None: |
---|
[6846] | 154 | parent = self.getParent(row, site) |
---|
[7656] | 155 | parent.logger.info('%s - Student removed' % student.student_id) |
---|
[6846] | 156 | del parent[student.student_id] |
---|
[6821] | 157 | pass |
---|
[6825] | 158 | |
---|
[8309] | 159 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 160 | """Checks requirements the object must fulfill when being updated. |
---|
| 161 | |
---|
| 162 | This method is not used in case of deleting or adding objects. |
---|
| 163 | |
---|
| 164 | Returns error messages as strings in case of requirement |
---|
| 165 | problems. |
---|
| 166 | """ |
---|
| 167 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 168 | if transition not in (IGNORE_MARKER, ''): |
---|
| 169 | allowed_transitions = IWorkflowInfo(obj).getManualTransitionIds() |
---|
| 170 | if transition not in allowed_transitions: |
---|
| 171 | return 'Transition not allowed.' |
---|
| 172 | return None |
---|
| 173 | |
---|
[7497] | 174 | def updateEntry(self, obj, row, site): |
---|
| 175 | """Update obj to the values given in row. |
---|
| 176 | """ |
---|
[8221] | 177 | items_changed = '' |
---|
| 178 | |
---|
[7643] | 179 | # Remove student_id from row if empty |
---|
[8232] | 180 | if row.has_key('student_id') and row['student_id'] in ( |
---|
| 181 | None, IGNORE_MARKER): |
---|
[7643] | 182 | row.pop('student_id') |
---|
[8221] | 183 | |
---|
| 184 | # Update password |
---|
[8498] | 185 | if row.has_key('password'): |
---|
| 186 | passwd = row.get('password', IGNORE_MARKER) |
---|
| 187 | if passwd not in ('', IGNORE_MARKER): |
---|
| 188 | if passwd.startswith('{SSHA}'): |
---|
| 189 | # already encrypted password |
---|
| 190 | obj.password = passwd |
---|
| 191 | else: |
---|
| 192 | # not yet encrypted password |
---|
| 193 | IUserAccount(obj).setPassword(passwd) |
---|
| 194 | items_changed += ('%s=%s, ' % ('password', passwd)) |
---|
[8221] | 195 | row.pop('password') |
---|
| 196 | |
---|
| 197 | # Update registration state |
---|
[8498] | 198 | if row.has_key('state'): |
---|
| 199 | state = row.get('state', IGNORE_MARKER) |
---|
| 200 | if state not in (IGNORE_MARKER, ''): |
---|
| 201 | value = row['state'] |
---|
| 202 | IWorkflowState(obj).setState(value) |
---|
| 203 | msg = _("State '${a}' set", mapping = {'a':value}) |
---|
| 204 | history = IObjectHistory(obj) |
---|
| 205 | history.addMessage(msg) |
---|
| 206 | items_changed += ('%s=%s, ' % ('state', state)) |
---|
[8287] | 207 | row.pop('state') |
---|
[8498] | 208 | |
---|
| 209 | if row.has_key('transition'): |
---|
| 210 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 211 | if transition not in (IGNORE_MARKER, ''): |
---|
| 212 | value = row['transition'] |
---|
| 213 | IWorkflowInfo(obj).fireTransition(value) |
---|
| 214 | items_changed += ('%s=%s, ' % ('transition', transition)) |
---|
[8309] | 215 | row.pop('transition') |
---|
[8221] | 216 | |
---|
| 217 | # apply other values... |
---|
[8498] | 218 | items_changed += super(StudentProcessor, self).updateEntry( |
---|
[8221] | 219 | obj, row, site) |
---|
| 220 | |
---|
| 221 | # Log actions... |
---|
[7656] | 222 | parent = self.getParent(row, site) |
---|
| 223 | if hasattr(obj,'student_id'): |
---|
| 224 | # Update mode: the student exists and we can get the student_id |
---|
| 225 | parent.logger.info( |
---|
| 226 | '%s - Student record updated: %s' |
---|
| 227 | % (obj.student_id, items_changed)) |
---|
| 228 | else: |
---|
| 229 | # Create mode: the student does not yet exist |
---|
| 230 | parent.logger.info('Student data imported: %s' % items_changed) |
---|
[8221] | 231 | return items_changed |
---|
[7497] | 232 | |
---|
[6849] | 233 | def getMapping(self, path, headerfields, mode): |
---|
| 234 | """Get a mapping from CSV file headerfields to actually used fieldnames. |
---|
| 235 | """ |
---|
| 236 | result = dict() |
---|
| 237 | reader = csv.reader(open(path, 'rb')) |
---|
| 238 | raw_header = reader.next() |
---|
| 239 | for num, field in enumerate(headerfields): |
---|
[8221] | 240 | if field not in ['student_id', 'reg_number', 'matric_number' |
---|
| 241 | ] and mode == 'remove': |
---|
[6849] | 242 | continue |
---|
| 243 | if field == u'--IGNORE--': |
---|
| 244 | # Skip ignored columns in failed and finished data files. |
---|
| 245 | continue |
---|
| 246 | result[raw_header[num]] = field |
---|
| 247 | return result |
---|
| 248 | |
---|
| 249 | def checkConversion(self, row, mode='create'): |
---|
| 250 | """Validates all values in row. |
---|
| 251 | """ |
---|
[7643] | 252 | iface = self.iface |
---|
[6849] | 253 | if mode in ['update', 'remove']: |
---|
| 254 | if self.getLocator(row) == 'reg_number': |
---|
[8581] | 255 | iface = self.iface_byregnumber |
---|
[6849] | 256 | elif self.getLocator(row) == 'matric_number': |
---|
[8581] | 257 | iface = self.iface_bymatricnumber |
---|
[6849] | 258 | converter = IObjectConverter(iface) |
---|
| 259 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
[8214] | 260 | row, self.factory_name, mode=mode) |
---|
[8309] | 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')) |
---|
[8287] | 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 |
---|
[8490] | 273 | |
---|
| 274 | try: |
---|
| 275 | # Correct stud_id counter. As the IConverter for students |
---|
| 276 | # creates student objects that are not used afterwards, we |
---|
| 277 | # have to fix the site-wide student_id counter. |
---|
| 278 | site = grok.getSite() |
---|
| 279 | students = site['students'] |
---|
| 280 | students._curr_stud_id -= 1 |
---|
| 281 | except (KeyError, TypeError, AttributeError): |
---|
| 282 | pass |
---|
[6849] | 283 | return errs, inv_errs, conv_dict |
---|
| 284 | |
---|
[8232] | 285 | |
---|
| 286 | class StudentProcessorBase(BatchProcessor): |
---|
| 287 | """A base for student subitem processor. |
---|
| 288 | |
---|
| 289 | Helps reducing redundancy. |
---|
[6825] | 290 | """ |
---|
[8232] | 291 | grok.baseclass() |
---|
[6825] | 292 | |
---|
[8232] | 293 | #: required fields beside 'student_id', 'reg_number' and 'matric_number' |
---|
| 294 | additional_fields = [] |
---|
[6825] | 295 | |
---|
[8232] | 296 | #: header fields additional required |
---|
| 297 | additional_headers = [] |
---|
[6849] | 298 | |
---|
[6825] | 299 | @property |
---|
| 300 | def available_fields(self): |
---|
[8232] | 301 | fields = ['student_id','reg_number','matric_number' |
---|
| 302 | ] + self.additional_fields |
---|
| 303 | return sorted(list(set(fields + getFields( |
---|
[6843] | 304 | self.iface).keys()))) |
---|
[6825] | 305 | |
---|
[6837] | 306 | def checkHeaders(self, headerfields, mode='ignore'): |
---|
[6854] | 307 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 308 | in headerfields and not 'matric_number' in headerfields: |
---|
[6825] | 309 | raise FatalCSVError( |
---|
[6854] | 310 | "Need at least columns student_id " + |
---|
| 311 | "or reg_number or matric_number for import!") |
---|
[8232] | 312 | for name in self.additional_headers: |
---|
| 313 | if not name in headerfields: |
---|
| 314 | raise FatalCSVError( |
---|
| 315 | "Need %s for import!" % name) |
---|
| 316 | |
---|
[6834] | 317 | # Check for fields to be ignored... |
---|
[6825] | 318 | not_ignored_fields = [x for x in headerfields |
---|
| 319 | if not x.startswith('--')] |
---|
| 320 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 321 | raise FatalCSVError( |
---|
| 322 | "Double headers: each column name may only appear once.") |
---|
| 323 | return True |
---|
| 324 | |
---|
[8232] | 325 | def _getStudent(self, row, site): |
---|
[8225] | 326 | NON_VALUES = ['', IGNORE_MARKER] |
---|
[6846] | 327 | if not 'students' in site.keys(): |
---|
[6849] | 328 | return None |
---|
[8225] | 329 | if row.get('student_id', '') not in NON_VALUES: |
---|
[6825] | 330 | if row['student_id'] in site['students']: |
---|
| 331 | student = site['students'][row['student_id']] |
---|
| 332 | return student |
---|
[8225] | 333 | elif row.get('reg_number', '') not in NON_VALUES: |
---|
[6825] | 334 | reg_number = row['reg_number'] |
---|
| 335 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 336 | results = list( |
---|
| 337 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 338 | if results: |
---|
| 339 | return results[0] |
---|
[8225] | 340 | elif row.get('matric_number', '') not in NON_VALUES: |
---|
[6843] | 341 | matric_number = row['matric_number'] |
---|
| 342 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 343 | results = list( |
---|
| 344 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 345 | if results: |
---|
| 346 | return results[0] |
---|
[6849] | 347 | return None |
---|
[6825] | 348 | |
---|
[7267] | 349 | def parentsExist(self, row, site): |
---|
| 350 | return self.getParent(row, site) is not None |
---|
| 351 | |
---|
[6825] | 352 | def entryExists(self, row, site): |
---|
[7534] | 353 | return self.getEntry(row, site) is not None |
---|
[6825] | 354 | |
---|
[8232] | 355 | def checkConversion(self, row, mode='ignore'): |
---|
| 356 | """Validates all values in row. |
---|
| 357 | """ |
---|
| 358 | converter = IObjectConverter(self.iface) |
---|
| 359 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
| 360 | row, self.factory_name, mode=mode) |
---|
| 361 | return errs, inv_errs, conv_dict |
---|
| 362 | |
---|
| 363 | |
---|
| 364 | class StudentStudyCourseProcessor(StudentProcessorBase): |
---|
| 365 | """A batch processor for IStudentStudyCourse objects. |
---|
| 366 | """ |
---|
| 367 | grok.implements(IBatchProcessor) |
---|
| 368 | grok.provides(IBatchProcessor) |
---|
| 369 | grok.context(Interface) |
---|
| 370 | util_name = 'studycourseupdater' |
---|
| 371 | grok.name(util_name) |
---|
| 372 | |
---|
| 373 | name = u'StudentStudyCourse Processor (update only)' |
---|
| 374 | iface = IStudentStudyCourse |
---|
| 375 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 376 | |
---|
| 377 | location_fields = [] |
---|
| 378 | additional_fields = [] |
---|
| 379 | |
---|
| 380 | def getParent(self, row, site): |
---|
| 381 | return self._getStudent(row, site) |
---|
| 382 | |
---|
[6825] | 383 | def getEntry(self, row, site): |
---|
[7534] | 384 | student = self.getParent(row, site) |
---|
[7536] | 385 | if student is None: |
---|
[6825] | 386 | return None |
---|
| 387 | return student.get('studycourse') |
---|
[7429] | 388 | |
---|
| 389 | def updateEntry(self, obj, row, site): |
---|
| 390 | """Update obj to the values given in row. |
---|
| 391 | """ |
---|
[8221] | 392 | items_changed = super(StudentStudyCourseProcessor, self).updateEntry( |
---|
| 393 | obj, row, site) |
---|
[7656] | 394 | parent = self.getParent(row, site) |
---|
| 395 | parent.__parent__.logger.info( |
---|
| 396 | '%s - Study course updated: %s' |
---|
| 397 | % (parent.student_id, items_changed)) |
---|
[7429] | 398 | # Update the students_catalog |
---|
| 399 | notify(grok.ObjectModifiedEvent(obj.__parent__)) |
---|
| 400 | return |
---|
| 401 | |
---|
[7532] | 402 | def checkConversion(self, row, mode='ignore'): |
---|
| 403 | """Validates all values in row. |
---|
| 404 | """ |
---|
[8232] | 405 | errs, inv_errs, conv_dict = super( |
---|
| 406 | StudentStudyCourseProcessor, self).checkConversion(row, mode=mode) |
---|
[7532] | 407 | # We have to check if current_level is in range of certificate. |
---|
[7534] | 408 | if conv_dict.has_key('certificate'): |
---|
[8221] | 409 | cert = conv_dict['certificate'] |
---|
| 410 | if conv_dict['current_level'] < cert.start_level or \ |
---|
| 411 | conv_dict['current_level'] > cert.end_level+120: |
---|
[7534] | 412 | errs.append(('current_level','not in range')) |
---|
[7532] | 413 | return errs, inv_errs, conv_dict |
---|
| 414 | |
---|
[8232] | 415 | class StudentStudyLevelProcessor(StudentProcessorBase): |
---|
[7536] | 416 | """A batch processor for IStudentStudyLevel objects. |
---|
| 417 | """ |
---|
| 418 | grok.implements(IBatchProcessor) |
---|
| 419 | grok.provides(IBatchProcessor) |
---|
| 420 | grok.context(Interface) |
---|
[7933] | 421 | util_name = 'studylevelprocessor' |
---|
[7536] | 422 | grok.name(util_name) |
---|
| 423 | |
---|
[7933] | 424 | name = u'StudentStudyLevel Processor' |
---|
[7536] | 425 | iface = IStudentStudyLevel |
---|
| 426 | factory_name = 'waeup.StudentStudyLevel' |
---|
| 427 | |
---|
| 428 | location_fields = [] |
---|
[8232] | 429 | additional_fields = ['level'] |
---|
| 430 | additional_headers = ['level'] |
---|
[7536] | 431 | |
---|
| 432 | def getParent(self, row, site): |
---|
[8232] | 433 | student = self._getStudent(row, site) |
---|
| 434 | if student is None: |
---|
[7536] | 435 | return None |
---|
[8232] | 436 | return student['studycourse'] |
---|
[7536] | 437 | |
---|
| 438 | def getEntry(self, row, site): |
---|
| 439 | studycourse = self.getParent(row, site) |
---|
| 440 | if studycourse is None: |
---|
| 441 | return None |
---|
| 442 | return studycourse.get(row['level']) |
---|
| 443 | |
---|
[8626] | 444 | def updateEntry(self, obj, row, site): |
---|
| 445 | """Update obj to the values given in row. |
---|
| 446 | """ |
---|
| 447 | items_changed = super(StudentStudyLevelProcessor, self).updateEntry( |
---|
| 448 | obj, row, site) |
---|
| 449 | student = self.getParent(row, site).__parent__ |
---|
| 450 | student.__parent__.logger.info( |
---|
| 451 | '%s - Study level updated: %s' |
---|
| 452 | % (student.student_id, items_changed)) |
---|
| 453 | return |
---|
| 454 | |
---|
[7536] | 455 | def addEntry(self, obj, row, site): |
---|
| 456 | parent = self.getParent(row, site) |
---|
| 457 | obj.level = int(row['level']) |
---|
| 458 | parent[row['level']] = obj |
---|
| 459 | return |
---|
| 460 | |
---|
| 461 | def checkConversion(self, row, mode='ignore'): |
---|
| 462 | """Validates all values in row. |
---|
| 463 | """ |
---|
[8232] | 464 | errs, inv_errs, conv_dict = super( |
---|
| 465 | StudentStudyLevelProcessor, self).checkConversion(row, mode=mode) |
---|
| 466 | |
---|
[7536] | 467 | # We have to check if level is a valid integer. |
---|
[7548] | 468 | # This is not done by the converter. |
---|
[7536] | 469 | try: |
---|
| 470 | level = int(row['level']) |
---|
[7612] | 471 | if level not in range(0,700,10): |
---|
[7536] | 472 | errs.append(('level','no valid integer')) |
---|
| 473 | except ValueError: |
---|
| 474 | errs.append(('level','no integer')) |
---|
| 475 | return errs, inv_errs, conv_dict |
---|
[7548] | 476 | |
---|
[8232] | 477 | class CourseTicketProcessor(StudentProcessorBase): |
---|
[7548] | 478 | """A batch processor for ICourseTicket objects. |
---|
| 479 | """ |
---|
| 480 | grok.implements(IBatchProcessor) |
---|
| 481 | grok.provides(IBatchProcessor) |
---|
| 482 | grok.context(Interface) |
---|
[7933] | 483 | util_name = 'courseticketprocessor' |
---|
[7548] | 484 | grok.name(util_name) |
---|
| 485 | |
---|
[7933] | 486 | name = u'CourseTicket Processor' |
---|
[7548] | 487 | iface = ICourseTicket |
---|
| 488 | factory_name = 'waeup.CourseTicket' |
---|
| 489 | |
---|
| 490 | location_fields = [] |
---|
[8232] | 491 | additional_fields = ['level', 'code'] |
---|
| 492 | additional_headers = ['level', 'code'] |
---|
[7548] | 493 | |
---|
| 494 | def getParent(self, row, site): |
---|
[8232] | 495 | student = self._getStudent(row, site) |
---|
| 496 | if student is None: |
---|
[7548] | 497 | return None |
---|
[8232] | 498 | return student['studycourse'].get(row['level']) |
---|
[7548] | 499 | |
---|
| 500 | def getEntry(self, row, site): |
---|
| 501 | level = self.getParent(row, site) |
---|
| 502 | if level is None: |
---|
| 503 | return None |
---|
| 504 | return level.get(row['code']) |
---|
| 505 | |
---|
[8626] | 506 | def updateEntry(self, obj, row, site): |
---|
| 507 | """Update obj to the values given in row. |
---|
| 508 | """ |
---|
| 509 | items_changed = super(CourseTicketProcessor, self).updateEntry( |
---|
| 510 | obj, row, site) |
---|
| 511 | student = self.getParent(row, site).__parent__.__parent__ |
---|
| 512 | student.__parent__.logger.info( |
---|
| 513 | '%s - Course ticket updated: %s' |
---|
| 514 | % (student.student_id, items_changed)) |
---|
| 515 | return |
---|
| 516 | |
---|
[7548] | 517 | def addEntry(self, obj, row, site): |
---|
| 518 | parent = self.getParent(row, site) |
---|
| 519 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 520 | entries = list(catalog.searchResults(code=(row['code'],row['code']))) |
---|
| 521 | obj.fcode = entries[0].__parent__.__parent__.__parent__.code |
---|
| 522 | obj.dcode = entries[0].__parent__.__parent__.code |
---|
| 523 | obj.title = entries[0].title |
---|
| 524 | obj.credits = entries[0].credits |
---|
| 525 | obj.passmark = entries[0].passmark |
---|
| 526 | obj.semester = entries[0].semester |
---|
| 527 | parent[row['code']] = obj |
---|
| 528 | return |
---|
| 529 | |
---|
| 530 | def checkConversion(self, row, mode='ignore'): |
---|
| 531 | """Validates all values in row. |
---|
| 532 | """ |
---|
[8232] | 533 | errs, inv_errs, conv_dict = super( |
---|
| 534 | CourseTicketProcessor, self).checkConversion(row, mode=mode) |
---|
| 535 | |
---|
[7548] | 536 | # We have to check if course really exists. |
---|
| 537 | # This is not done by the converter. |
---|
| 538 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 539 | entries = catalog.searchResults(code=(row['code'],row['code'])) |
---|
| 540 | if len(entries) == 0: |
---|
| 541 | errs.append(('code','non-existent')) |
---|
| 542 | return errs, inv_errs, conv_dict |
---|
[7623] | 543 | return errs, inv_errs, conv_dict |
---|
| 544 | |
---|
[8232] | 545 | class StudentOnlinePaymentProcessor(StudentProcessorBase): |
---|
[7623] | 546 | """A batch processor for IStudentOnlinePayment objects. |
---|
| 547 | """ |
---|
| 548 | grok.implements(IBatchProcessor) |
---|
| 549 | grok.provides(IBatchProcessor) |
---|
| 550 | grok.context(Interface) |
---|
[7933] | 551 | util_name = 'paymentprocessor' |
---|
[7623] | 552 | grok.name(util_name) |
---|
| 553 | |
---|
[7933] | 554 | name = u'Payment Processor' |
---|
[8174] | 555 | iface = IStudentOnlinePayment |
---|
[7623] | 556 | factory_name = 'waeup.StudentOnlinePayment' |
---|
| 557 | |
---|
| 558 | location_fields = [] |
---|
[8232] | 559 | additional_fields = ['p_id'] |
---|
| 560 | additional_headers = ['p_id'] |
---|
[7623] | 561 | |
---|
[8232] | 562 | def parentsExist(self, row, site): |
---|
| 563 | return self.getParent(row, site) is not None |
---|
[7623] | 564 | |
---|
| 565 | def getParent(self, row, site): |
---|
[8232] | 566 | student = self._getStudent(row, site) |
---|
| 567 | if student is None: |
---|
[7623] | 568 | return None |
---|
[8232] | 569 | return student['payments'] |
---|
[7623] | 570 | |
---|
| 571 | def getEntry(self, row, site): |
---|
| 572 | payments = self.getParent(row, site) |
---|
| 573 | if payments is None: |
---|
| 574 | return None |
---|
[7626] | 575 | # We can use the hash symbol at the end of p_id in import files |
---|
| 576 | # to avoid annoying automatic number transformation |
---|
| 577 | # by Excel or Calc |
---|
| 578 | p_id = row['p_id'].strip('#') |
---|
| 579 | if p_id.startswith('p'): |
---|
| 580 | entry = payments.get(p_id) |
---|
[7623] | 581 | else: |
---|
| 582 | # For data migration from old SRP |
---|
[7626] | 583 | entry = payments.get('p' + p_id[6:]) |
---|
[7623] | 584 | return entry |
---|
| 585 | |
---|
[8626] | 586 | def updateEntry(self, obj, row, site): |
---|
| 587 | """Update obj to the values given in row. |
---|
| 588 | """ |
---|
| 589 | items_changed = super(StudentOnlinePaymentProcessor, self).updateEntry( |
---|
| 590 | obj, row, site) |
---|
| 591 | student = self.getParent(row, site).__parent__ |
---|
| 592 | student.__parent__.logger.info( |
---|
| 593 | '%s - Payment ticket updated: %s' |
---|
| 594 | % (student.student_id, items_changed)) |
---|
| 595 | return |
---|
| 596 | |
---|
[7623] | 597 | def addEntry(self, obj, row, site): |
---|
| 598 | parent = self.getParent(row, site) |
---|
[7626] | 599 | p_id = row['p_id'].strip('#') |
---|
| 600 | if not p_id.startswith('p'): |
---|
[7623] | 601 | # For data migration from old SRP |
---|
[7626] | 602 | obj.p_id = 'p' + p_id[6:] |
---|
[7623] | 603 | parent[obj.p_id] = obj |
---|
| 604 | else: |
---|
[7626] | 605 | parent[p_id] = obj |
---|
[7623] | 606 | return |
---|
| 607 | |
---|
| 608 | def checkConversion(self, row, mode='ignore'): |
---|
| 609 | """Validates all values in row. |
---|
| 610 | """ |
---|
[8232] | 611 | errs, inv_errs, conv_dict = super( |
---|
| 612 | StudentOnlinePaymentProcessor, self).checkConversion(row, mode=mode) |
---|
| 613 | |
---|
[7623] | 614 | # We have to check p_id. |
---|
[7626] | 615 | p_id = row['p_id'].strip('#') |
---|
| 616 | if p_id.startswith('p'): |
---|
| 617 | if not len(p_id) == 14: |
---|
[7623] | 618 | errs.append(('p_id','invalid length')) |
---|
| 619 | return errs, inv_errs, conv_dict |
---|
| 620 | else: |
---|
[7626] | 621 | if not len(p_id) == 19: |
---|
[7623] | 622 | errs.append(('p_id','invalid length')) |
---|
| 623 | return errs, inv_errs, conv_dict |
---|
| 624 | return errs, inv_errs, conv_dict |
---|
[7951] | 625 | |
---|
| 626 | class StudentVerdictProcessor(StudentStudyCourseProcessor): |
---|
| 627 | """A batch processor for verdicts. |
---|
| 628 | |
---|
| 629 | Import verdicts and perform workflow transitions. |
---|
| 630 | """ |
---|
| 631 | |
---|
| 632 | util_name = 'verdictupdater' |
---|
| 633 | grok.name(util_name) |
---|
| 634 | |
---|
| 635 | name = u'Verdict Processor (update only)' |
---|
| 636 | iface = IStudentVerdictUpdate |
---|
| 637 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 638 | |
---|
[8232] | 639 | additional_fields = ['current_session', 'current_level'] |
---|
[7951] | 640 | |
---|
| 641 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 642 | """Checks requirements the studycourse and the student must fulfill |
---|
| 643 | before being updated. |
---|
| 644 | """ |
---|
| 645 | # Check if current_levels correspond |
---|
| 646 | if obj.current_level != row['current_level']: |
---|
| 647 | return 'Current level does not correspond.' |
---|
| 648 | # Check if current_sessions correspond |
---|
| 649 | if obj.current_session != row['current_session']: |
---|
| 650 | return 'Current session does not correspond.' |
---|
| 651 | # Check if student is in state REGISTERED |
---|
| 652 | if obj.getStudent().state != VALIDATED: |
---|
| 653 | return 'Student in wrong state.' |
---|
| 654 | return None |
---|
| 655 | |
---|
| 656 | def updateEntry(self, obj, row, site): |
---|
| 657 | """Update obj to the values given in row. |
---|
| 658 | """ |
---|
[8221] | 659 | # Don't set current_session, current_level |
---|
| 660 | vals_to_set = dict((key, val) for key, val in row.items() |
---|
| 661 | if key not in ('current_session','current_level')) |
---|
| 662 | items_changed = super(StudentVerdictProcessor, self).updateEntry( |
---|
| 663 | obj, vals_to_set, site) |
---|
[7951] | 664 | parent = self.getParent(row, site) |
---|
| 665 | parent.__parent__.logger.info( |
---|
| 666 | '%s - Verdict updated: %s' |
---|
| 667 | % (parent.student_id, items_changed)) |
---|
| 668 | # Fire transition |
---|
| 669 | IWorkflowInfo(obj.__parent__).fireTransition('return') |
---|
| 670 | # Update the students_catalog |
---|
| 671 | notify(grok.ObjectModifiedEvent(obj.__parent__)) |
---|
| 672 | return |
---|