[7191] | 1 | ## $Id: batching.py 9421 2012-10-25 22:45:08Z 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 |
---|
[8884] | 28 | from time import time |
---|
[9295] | 29 | from datetime import datetime |
---|
[9296] | 30 | from zope.i18n import translate |
---|
[6821] | 31 | from zope.interface import Interface |
---|
[6825] | 32 | from zope.schema import getFields |
---|
[7548] | 33 | from zope.component import queryUtility, getUtility |
---|
[7429] | 34 | from zope.event import notify |
---|
[6825] | 35 | from zope.catalog.interfaces import ICatalog |
---|
[7951] | 36 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo |
---|
[7811] | 37 | from waeup.kofa.interfaces import ( |
---|
[7522] | 38 | IBatchProcessor, FatalCSVError, IObjectConverter, IUserAccount, |
---|
[9284] | 39 | IObjectHistory, VALIDATED, REGISTERED, IGNORE_MARKER) |
---|
[9296] | 40 | from waeup.kofa.interfaces import IKofaUtils |
---|
[7959] | 41 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7811] | 42 | from waeup.kofa.students.interfaces import ( |
---|
[7532] | 43 | IStudent, IStudentStudyCourse, |
---|
[7536] | 44 | IStudentUpdateByRegNo, IStudentUpdateByMatricNo, |
---|
[9420] | 45 | IStudentStudyLevel, ICourseTicketImport, |
---|
[8174] | 46 | IStudentOnlinePayment, IStudentVerdictUpdate) |
---|
[8309] | 47 | from waeup.kofa.students.workflow import ( |
---|
[9028] | 48 | IMPORTABLE_STATES, IMPORTABLE_TRANSITIONS, |
---|
| 49 | FORBIDDEN_POSTGRAD_TRANS, FORBIDDEN_POSTGRAD_STATES) |
---|
[7811] | 50 | from waeup.kofa.utils.batching import BatchProcessor |
---|
[6821] | 51 | |
---|
| 52 | class StudentProcessor(BatchProcessor): |
---|
| 53 | """A batch processor for IStudent objects. |
---|
| 54 | """ |
---|
| 55 | grok.implements(IBatchProcessor) |
---|
| 56 | grok.provides(IBatchProcessor) |
---|
| 57 | grok.context(Interface) |
---|
[7933] | 58 | util_name = 'studentprocessor' |
---|
[6821] | 59 | grok.name(util_name) |
---|
| 60 | |
---|
[7933] | 61 | name = u'Student Processor' |
---|
[6821] | 62 | iface = IStudent |
---|
[8581] | 63 | iface_byregnumber = IStudentUpdateByRegNo |
---|
| 64 | iface_bymatricnumber = IStudentUpdateByMatricNo |
---|
[6821] | 65 | |
---|
[6849] | 66 | location_fields = [] |
---|
[6821] | 67 | factory_name = 'waeup.Student' |
---|
| 68 | |
---|
| 69 | @property |
---|
[6849] | 70 | def available_fields(self): |
---|
[8176] | 71 | fields = getFields(self.iface) |
---|
[6849] | 72 | return sorted(list(set( |
---|
[7513] | 73 | ['student_id','reg_number','matric_number', |
---|
[8309] | 74 | 'password', 'state', 'transition'] + fields.keys()))) |
---|
[6821] | 75 | |
---|
[6849] | 76 | def checkHeaders(self, headerfields, mode='create'): |
---|
[8309] | 77 | if 'state' in headerfields and 'transition' in headerfields: |
---|
| 78 | raise FatalCSVError( |
---|
| 79 | "State and transition can't be imported at the same time!") |
---|
[6854] | 80 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 81 | in headerfields and not 'matric_number' in headerfields: |
---|
[6849] | 82 | raise FatalCSVError( |
---|
[6854] | 83 | "Need at least columns student_id or reg_number " + |
---|
| 84 | "or matric_number for import!") |
---|
[6849] | 85 | if mode == 'create': |
---|
| 86 | for field in self.required_fields: |
---|
| 87 | if not field in headerfields: |
---|
| 88 | raise FatalCSVError( |
---|
| 89 | "Need at least columns %s for import!" % |
---|
| 90 | ', '.join(["'%s'" % x for x in self.required_fields])) |
---|
| 91 | # Check for fields to be ignored... |
---|
| 92 | not_ignored_fields = [x for x in headerfields |
---|
| 93 | if not x.startswith('--')] |
---|
| 94 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 95 | raise FatalCSVError( |
---|
| 96 | "Double headers: each column name may only appear once.") |
---|
| 97 | return True |
---|
| 98 | |
---|
[6821] | 99 | def parentsExist(self, row, site): |
---|
| 100 | return 'students' in site.keys() |
---|
| 101 | |
---|
[6849] | 102 | def getLocator(self, row): |
---|
[8232] | 103 | if row.get('student_id',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 104 | return 'student_id' |
---|
[8232] | 105 | elif row.get('reg_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 106 | return 'reg_number' |
---|
[8232] | 107 | elif row.get('matric_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 108 | return 'matric_number' |
---|
| 109 | else: |
---|
| 110 | return None |
---|
| 111 | |
---|
[6821] | 112 | # The entry never exists in create mode. |
---|
| 113 | def entryExists(self, row, site): |
---|
[7267] | 114 | return self.getEntry(row, site) is not None |
---|
| 115 | |
---|
| 116 | def getParent(self, row, site): |
---|
| 117 | return site['students'] |
---|
| 118 | |
---|
| 119 | def getEntry(self, row, site): |
---|
[6846] | 120 | if not 'students' in site.keys(): |
---|
[6849] | 121 | return None |
---|
| 122 | if self.getLocator(row) == 'student_id': |
---|
[6846] | 123 | if row['student_id'] in site['students']: |
---|
| 124 | student = site['students'][row['student_id']] |
---|
| 125 | return student |
---|
[6849] | 126 | elif self.getLocator(row) == 'reg_number': |
---|
[6846] | 127 | reg_number = row['reg_number'] |
---|
| 128 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 129 | results = list( |
---|
| 130 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 131 | if results: |
---|
| 132 | return results[0] |
---|
[6849] | 133 | elif self.getLocator(row) == 'matric_number': |
---|
[6846] | 134 | matric_number = row['matric_number'] |
---|
| 135 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 136 | results = list( |
---|
| 137 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 138 | if results: |
---|
| 139 | return results[0] |
---|
[6849] | 140 | return None |
---|
[6821] | 141 | |
---|
| 142 | def addEntry(self, obj, row, site): |
---|
| 143 | parent = self.getParent(row, site) |
---|
| 144 | parent.addStudent(obj) |
---|
[8491] | 145 | # Reset _curr_stud_id if student_id has been imported |
---|
| 146 | if self.getLocator(row) == 'student_id': |
---|
| 147 | parent._curr_stud_id -= 1 |
---|
[8287] | 148 | # We have to log this if state is provided. If not, |
---|
[7959] | 149 | # logging is done by the event handler handle_student_added |
---|
[8287] | 150 | if row.has_key('state'): |
---|
[7959] | 151 | parent.logger.info('%s - Student record created' % obj.student_id) |
---|
[7656] | 152 | history = IObjectHistory(obj) |
---|
[7959] | 153 | history.addMessage(_('Student record created')) |
---|
[6821] | 154 | return |
---|
| 155 | |
---|
| 156 | def delEntry(self, row, site): |
---|
[7267] | 157 | student = self.getEntry(row, site) |
---|
[7263] | 158 | if student is not None: |
---|
[6846] | 159 | parent = self.getParent(row, site) |
---|
[7656] | 160 | parent.logger.info('%s - Student removed' % student.student_id) |
---|
[6846] | 161 | del parent[student.student_id] |
---|
[6821] | 162 | pass |
---|
[6825] | 163 | |
---|
[8309] | 164 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 165 | """Checks requirements the object must fulfill when being updated. |
---|
| 166 | |
---|
| 167 | This method is not used in case of deleting or adding objects. |
---|
| 168 | |
---|
| 169 | Returns error messages as strings in case of requirement |
---|
| 170 | problems. |
---|
| 171 | """ |
---|
| 172 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 173 | if transition not in (IGNORE_MARKER, ''): |
---|
| 174 | allowed_transitions = IWorkflowInfo(obj).getManualTransitionIds() |
---|
| 175 | if transition not in allowed_transitions: |
---|
| 176 | return 'Transition not allowed.' |
---|
[9028] | 177 | if transition in FORBIDDEN_POSTGRAD_TRANS and \ |
---|
| 178 | obj.is_postgrad: |
---|
| 179 | return 'Transition not allowed (pg student).' |
---|
| 180 | state = row.get('state', IGNORE_MARKER) |
---|
| 181 | if state not in (IGNORE_MARKER, ''): |
---|
| 182 | if state in FORBIDDEN_POSTGRAD_STATES and \ |
---|
| 183 | obj.is_postgrad: |
---|
| 184 | return 'State not allowed (pg student).' |
---|
[8309] | 185 | return None |
---|
| 186 | |
---|
[7497] | 187 | def updateEntry(self, obj, row, site): |
---|
| 188 | """Update obj to the values given in row. |
---|
| 189 | """ |
---|
[8221] | 190 | items_changed = '' |
---|
| 191 | |
---|
[7643] | 192 | # Remove student_id from row if empty |
---|
[8232] | 193 | if row.has_key('student_id') and row['student_id'] in ( |
---|
| 194 | None, IGNORE_MARKER): |
---|
[7643] | 195 | row.pop('student_id') |
---|
[8221] | 196 | |
---|
| 197 | # Update password |
---|
[9001] | 198 | # XXX: Tale DELETION_MARKER into consideration |
---|
[8498] | 199 | if row.has_key('password'): |
---|
| 200 | passwd = row.get('password', IGNORE_MARKER) |
---|
| 201 | if passwd not in ('', IGNORE_MARKER): |
---|
| 202 | if passwd.startswith('{SSHA}'): |
---|
| 203 | # already encrypted password |
---|
| 204 | obj.password = passwd |
---|
| 205 | else: |
---|
| 206 | # not yet encrypted password |
---|
| 207 | IUserAccount(obj).setPassword(passwd) |
---|
| 208 | items_changed += ('%s=%s, ' % ('password', passwd)) |
---|
[8221] | 209 | row.pop('password') |
---|
| 210 | |
---|
| 211 | # Update registration state |
---|
[8498] | 212 | if row.has_key('state'): |
---|
| 213 | state = row.get('state', IGNORE_MARKER) |
---|
| 214 | if state not in (IGNORE_MARKER, ''): |
---|
| 215 | value = row['state'] |
---|
| 216 | IWorkflowState(obj).setState(value) |
---|
| 217 | msg = _("State '${a}' set", mapping = {'a':value}) |
---|
| 218 | history = IObjectHistory(obj) |
---|
| 219 | history.addMessage(msg) |
---|
| 220 | items_changed += ('%s=%s, ' % ('state', state)) |
---|
[8287] | 221 | row.pop('state') |
---|
[8498] | 222 | |
---|
| 223 | if row.has_key('transition'): |
---|
| 224 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 225 | if transition not in (IGNORE_MARKER, ''): |
---|
| 226 | value = row['transition'] |
---|
| 227 | IWorkflowInfo(obj).fireTransition(value) |
---|
| 228 | items_changed += ('%s=%s, ' % ('transition', transition)) |
---|
[8309] | 229 | row.pop('transition') |
---|
[8221] | 230 | |
---|
| 231 | # apply other values... |
---|
[8498] | 232 | items_changed += super(StudentProcessor, self).updateEntry( |
---|
[8221] | 233 | obj, row, site) |
---|
| 234 | |
---|
| 235 | # Log actions... |
---|
[7656] | 236 | parent = self.getParent(row, site) |
---|
| 237 | if hasattr(obj,'student_id'): |
---|
[8995] | 238 | # Update mode: the student exists and we can get the student_id. |
---|
| 239 | # Create mode: the record contains the student_id |
---|
[7656] | 240 | parent.logger.info( |
---|
| 241 | '%s - Student record updated: %s' |
---|
| 242 | % (obj.student_id, items_changed)) |
---|
| 243 | else: |
---|
| 244 | # Create mode: the student does not yet exist |
---|
| 245 | parent.logger.info('Student data imported: %s' % items_changed) |
---|
[8221] | 246 | return items_changed |
---|
[7497] | 247 | |
---|
[6849] | 248 | def getMapping(self, path, headerfields, mode): |
---|
| 249 | """Get a mapping from CSV file headerfields to actually used fieldnames. |
---|
| 250 | """ |
---|
| 251 | result = dict() |
---|
| 252 | reader = csv.reader(open(path, 'rb')) |
---|
| 253 | raw_header = reader.next() |
---|
| 254 | for num, field in enumerate(headerfields): |
---|
[8221] | 255 | if field not in ['student_id', 'reg_number', 'matric_number' |
---|
| 256 | ] and mode == 'remove': |
---|
[6849] | 257 | continue |
---|
| 258 | if field == u'--IGNORE--': |
---|
| 259 | # Skip ignored columns in failed and finished data files. |
---|
| 260 | continue |
---|
| 261 | result[raw_header[num]] = field |
---|
| 262 | return result |
---|
| 263 | |
---|
| 264 | def checkConversion(self, row, mode='create'): |
---|
| 265 | """Validates all values in row. |
---|
| 266 | """ |
---|
[7643] | 267 | iface = self.iface |
---|
[6849] | 268 | if mode in ['update', 'remove']: |
---|
| 269 | if self.getLocator(row) == 'reg_number': |
---|
[8581] | 270 | iface = self.iface_byregnumber |
---|
[6849] | 271 | elif self.getLocator(row) == 'matric_number': |
---|
[8581] | 272 | iface = self.iface_bymatricnumber |
---|
[6849] | 273 | converter = IObjectConverter(iface) |
---|
| 274 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
[8214] | 275 | row, self.factory_name, mode=mode) |
---|
[9028] | 276 | if row.has_key('transition'): |
---|
| 277 | if row['transition'] not in IMPORTABLE_TRANSITIONS: |
---|
| 278 | if row['transition'] not in (IGNORE_MARKER, ''): |
---|
| 279 | errs.append(('transition','not allowed')) |
---|
| 280 | if row.has_key('state'): |
---|
| 281 | if row['state'] not in IMPORTABLE_STATES: |
---|
| 282 | if row['state'] not in (IGNORE_MARKER, ''): |
---|
| 283 | errs.append(('state','not allowed')) |
---|
| 284 | else: |
---|
| 285 | # State is an attribute of Student and must not |
---|
| 286 | # be changed if empty. |
---|
| 287 | conv_dict['state'] = IGNORE_MARKER |
---|
[8490] | 288 | try: |
---|
| 289 | # Correct stud_id counter. As the IConverter for students |
---|
| 290 | # creates student objects that are not used afterwards, we |
---|
| 291 | # have to fix the site-wide student_id counter. |
---|
| 292 | site = grok.getSite() |
---|
| 293 | students = site['students'] |
---|
| 294 | students._curr_stud_id -= 1 |
---|
| 295 | except (KeyError, TypeError, AttributeError): |
---|
| 296 | pass |
---|
[6849] | 297 | return errs, inv_errs, conv_dict |
---|
| 298 | |
---|
[8232] | 299 | |
---|
| 300 | class StudentProcessorBase(BatchProcessor): |
---|
| 301 | """A base for student subitem processor. |
---|
| 302 | |
---|
| 303 | Helps reducing redundancy. |
---|
[6825] | 304 | """ |
---|
[8232] | 305 | grok.baseclass() |
---|
[6825] | 306 | |
---|
[9420] | 307 | # additional available fields |
---|
[8884] | 308 | # beside 'student_id', 'reg_number' and 'matric_number' |
---|
[8232] | 309 | additional_fields = [] |
---|
[6825] | 310 | |
---|
[8884] | 311 | #: header fields additionally required |
---|
[8232] | 312 | additional_headers = [] |
---|
[6849] | 313 | |
---|
[6825] | 314 | @property |
---|
| 315 | def available_fields(self): |
---|
[8232] | 316 | fields = ['student_id','reg_number','matric_number' |
---|
| 317 | ] + self.additional_fields |
---|
| 318 | return sorted(list(set(fields + getFields( |
---|
[6843] | 319 | self.iface).keys()))) |
---|
[6825] | 320 | |
---|
[6837] | 321 | def checkHeaders(self, headerfields, mode='ignore'): |
---|
[6854] | 322 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 323 | in headerfields and not 'matric_number' in headerfields: |
---|
[6825] | 324 | raise FatalCSVError( |
---|
[6854] | 325 | "Need at least columns student_id " + |
---|
| 326 | "or reg_number or matric_number for import!") |
---|
[8232] | 327 | for name in self.additional_headers: |
---|
| 328 | if not name in headerfields: |
---|
| 329 | raise FatalCSVError( |
---|
| 330 | "Need %s for import!" % name) |
---|
| 331 | |
---|
[6834] | 332 | # Check for fields to be ignored... |
---|
[6825] | 333 | not_ignored_fields = [x for x in headerfields |
---|
| 334 | if not x.startswith('--')] |
---|
| 335 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 336 | raise FatalCSVError( |
---|
| 337 | "Double headers: each column name may only appear once.") |
---|
| 338 | return True |
---|
| 339 | |
---|
[8232] | 340 | def _getStudent(self, row, site): |
---|
[8225] | 341 | NON_VALUES = ['', IGNORE_MARKER] |
---|
[6846] | 342 | if not 'students' in site.keys(): |
---|
[6849] | 343 | return None |
---|
[8225] | 344 | if row.get('student_id', '') not in NON_VALUES: |
---|
[6825] | 345 | if row['student_id'] in site['students']: |
---|
| 346 | student = site['students'][row['student_id']] |
---|
| 347 | return student |
---|
[8225] | 348 | elif row.get('reg_number', '') not in NON_VALUES: |
---|
[6825] | 349 | reg_number = row['reg_number'] |
---|
| 350 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 351 | results = list( |
---|
| 352 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 353 | if results: |
---|
| 354 | return results[0] |
---|
[8225] | 355 | elif row.get('matric_number', '') not in NON_VALUES: |
---|
[6843] | 356 | matric_number = row['matric_number'] |
---|
| 357 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 358 | results = list( |
---|
| 359 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 360 | if results: |
---|
| 361 | return results[0] |
---|
[6849] | 362 | return None |
---|
[6825] | 363 | |
---|
[7267] | 364 | def parentsExist(self, row, site): |
---|
| 365 | return self.getParent(row, site) is not None |
---|
| 366 | |
---|
[6825] | 367 | def entryExists(self, row, site): |
---|
[7534] | 368 | return self.getEntry(row, site) is not None |
---|
[6825] | 369 | |
---|
[8232] | 370 | def checkConversion(self, row, mode='ignore'): |
---|
| 371 | """Validates all values in row. |
---|
| 372 | """ |
---|
| 373 | converter = IObjectConverter(self.iface) |
---|
| 374 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
| 375 | row, self.factory_name, mode=mode) |
---|
| 376 | return errs, inv_errs, conv_dict |
---|
| 377 | |
---|
[8885] | 378 | def getMapping(self, path, headerfields, mode): |
---|
| 379 | """Get a mapping from CSV file headerfields to actually used fieldnames. |
---|
| 380 | """ |
---|
| 381 | result = dict() |
---|
| 382 | reader = csv.reader(open(path, 'rb')) |
---|
| 383 | raw_header = reader.next() |
---|
| 384 | for num, field in enumerate(headerfields): |
---|
[8888] | 385 | if field not in ['student_id', 'reg_number', 'matric_number', |
---|
| 386 | 'p_id', 'code', 'level' |
---|
[8885] | 387 | ] and mode == 'remove': |
---|
| 388 | continue |
---|
| 389 | if field == u'--IGNORE--': |
---|
| 390 | # Skip ignored columns in failed and finished data files. |
---|
| 391 | continue |
---|
| 392 | result[raw_header[num]] = field |
---|
| 393 | return result |
---|
[8232] | 394 | |
---|
[8885] | 395 | |
---|
[8232] | 396 | class StudentStudyCourseProcessor(StudentProcessorBase): |
---|
| 397 | """A batch processor for IStudentStudyCourse objects. |
---|
| 398 | """ |
---|
| 399 | grok.implements(IBatchProcessor) |
---|
| 400 | grok.provides(IBatchProcessor) |
---|
| 401 | grok.context(Interface) |
---|
| 402 | util_name = 'studycourseupdater' |
---|
| 403 | grok.name(util_name) |
---|
| 404 | |
---|
| 405 | name = u'StudentStudyCourse Processor (update only)' |
---|
| 406 | iface = IStudentStudyCourse |
---|
| 407 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 408 | |
---|
| 409 | location_fields = [] |
---|
| 410 | additional_fields = [] |
---|
| 411 | |
---|
| 412 | def getParent(self, row, site): |
---|
| 413 | return self._getStudent(row, site) |
---|
| 414 | |
---|
[6825] | 415 | def getEntry(self, row, site): |
---|
[7534] | 416 | student = self.getParent(row, site) |
---|
[7536] | 417 | if student is None: |
---|
[6825] | 418 | return None |
---|
| 419 | return student.get('studycourse') |
---|
[7429] | 420 | |
---|
| 421 | def updateEntry(self, obj, row, site): |
---|
| 422 | """Update obj to the values given in row. |
---|
| 423 | """ |
---|
[8221] | 424 | items_changed = super(StudentStudyCourseProcessor, self).updateEntry( |
---|
| 425 | obj, row, site) |
---|
[7656] | 426 | parent = self.getParent(row, site) |
---|
| 427 | parent.__parent__.logger.info( |
---|
| 428 | '%s - Study course updated: %s' |
---|
| 429 | % (parent.student_id, items_changed)) |
---|
[7429] | 430 | # Update the students_catalog |
---|
| 431 | notify(grok.ObjectModifiedEvent(obj.__parent__)) |
---|
| 432 | return |
---|
| 433 | |
---|
[7532] | 434 | def checkConversion(self, row, mode='ignore'): |
---|
| 435 | """Validates all values in row. |
---|
| 436 | """ |
---|
[8232] | 437 | errs, inv_errs, conv_dict = super( |
---|
| 438 | StudentStudyCourseProcessor, self).checkConversion(row, mode=mode) |
---|
[7532] | 439 | # We have to check if current_level is in range of certificate. |
---|
[8940] | 440 | if conv_dict.has_key('certificate') and \ |
---|
| 441 | conv_dict.has_key('current_level'): |
---|
| 442 | cert = conv_dict['certificate'] |
---|
| 443 | level = conv_dict['current_level'] |
---|
| 444 | if level < cert.start_level or level > cert.end_level+120: |
---|
| 445 | errs.append(('current_level','not in range')) |
---|
[7532] | 446 | return errs, inv_errs, conv_dict |
---|
| 447 | |
---|
[9028] | 448 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 449 | """Checks requirements the object must fulfill when being updated. |
---|
| 450 | |
---|
| 451 | Returns error messages as strings in case of requirement |
---|
| 452 | problems. |
---|
| 453 | """ |
---|
| 454 | current_level = row.get('current_level', None) |
---|
[9037] | 455 | if current_level == 999 and \ |
---|
[9029] | 456 | obj.__parent__.state in FORBIDDEN_POSTGRAD_STATES: |
---|
[9028] | 457 | return 'Not a pg student.' |
---|
| 458 | return None |
---|
| 459 | |
---|
[8232] | 460 | class StudentStudyLevelProcessor(StudentProcessorBase): |
---|
[7536] | 461 | """A batch processor for IStudentStudyLevel objects. |
---|
| 462 | """ |
---|
| 463 | grok.implements(IBatchProcessor) |
---|
| 464 | grok.provides(IBatchProcessor) |
---|
| 465 | grok.context(Interface) |
---|
[7933] | 466 | util_name = 'studylevelprocessor' |
---|
[7536] | 467 | grok.name(util_name) |
---|
| 468 | |
---|
[7933] | 469 | name = u'StudentStudyLevel Processor' |
---|
[7536] | 470 | iface = IStudentStudyLevel |
---|
| 471 | factory_name = 'waeup.StudentStudyLevel' |
---|
| 472 | |
---|
| 473 | location_fields = [] |
---|
[9160] | 474 | |
---|
[9161] | 475 | additional_fields = ['level'] |
---|
[8232] | 476 | additional_headers = ['level'] |
---|
[7536] | 477 | |
---|
| 478 | def getParent(self, row, site): |
---|
[8232] | 479 | student = self._getStudent(row, site) |
---|
| 480 | if student is None: |
---|
[7536] | 481 | return None |
---|
[8232] | 482 | return student['studycourse'] |
---|
[7536] | 483 | |
---|
| 484 | def getEntry(self, row, site): |
---|
| 485 | studycourse = self.getParent(row, site) |
---|
| 486 | if studycourse is None: |
---|
| 487 | return None |
---|
| 488 | return studycourse.get(row['level']) |
---|
| 489 | |
---|
[9301] | 490 | def delEntry(self, row, site): |
---|
| 491 | studylevel = self.getEntry(row, site) |
---|
| 492 | parent = self.getParent(row, site) |
---|
| 493 | if studylevel is not None: |
---|
| 494 | student = self._getStudent(row, site) |
---|
| 495 | student.__parent__.logger.info('%s - Level removed: %s' |
---|
| 496 | % (student.student_id, studylevel.__name__)) |
---|
| 497 | del parent[studylevel.__name__] |
---|
| 498 | return |
---|
| 499 | |
---|
[8626] | 500 | def updateEntry(self, obj, row, site): |
---|
| 501 | """Update obj to the values given in row. |
---|
| 502 | """ |
---|
| 503 | items_changed = super(StudentStudyLevelProcessor, self).updateEntry( |
---|
| 504 | obj, row, site) |
---|
| 505 | student = self.getParent(row, site).__parent__ |
---|
| 506 | student.__parent__.logger.info( |
---|
| 507 | '%s - Study level updated: %s' |
---|
| 508 | % (student.student_id, items_changed)) |
---|
| 509 | return |
---|
| 510 | |
---|
[7536] | 511 | def addEntry(self, obj, row, site): |
---|
| 512 | parent = self.getParent(row, site) |
---|
| 513 | obj.level = int(row['level']) |
---|
| 514 | parent[row['level']] = obj |
---|
| 515 | return |
---|
| 516 | |
---|
| 517 | def checkConversion(self, row, mode='ignore'): |
---|
| 518 | """Validates all values in row. |
---|
| 519 | """ |
---|
[8232] | 520 | errs, inv_errs, conv_dict = super( |
---|
| 521 | StudentStudyLevelProcessor, self).checkConversion(row, mode=mode) |
---|
| 522 | |
---|
[7536] | 523 | # We have to check if level is a valid integer. |
---|
[7548] | 524 | # This is not done by the converter. |
---|
[7536] | 525 | try: |
---|
| 526 | level = int(row['level']) |
---|
[9301] | 527 | if level not in range(0,1000,10) + [999]: |
---|
[7536] | 528 | errs.append(('level','no valid integer')) |
---|
| 529 | except ValueError: |
---|
| 530 | errs.append(('level','no integer')) |
---|
| 531 | return errs, inv_errs, conv_dict |
---|
[7548] | 532 | |
---|
[8232] | 533 | class CourseTicketProcessor(StudentProcessorBase): |
---|
[7548] | 534 | """A batch processor for ICourseTicket objects. |
---|
| 535 | """ |
---|
| 536 | grok.implements(IBatchProcessor) |
---|
| 537 | grok.provides(IBatchProcessor) |
---|
| 538 | grok.context(Interface) |
---|
[7933] | 539 | util_name = 'courseticketprocessor' |
---|
[7548] | 540 | grok.name(util_name) |
---|
| 541 | |
---|
[7933] | 542 | name = u'CourseTicket Processor' |
---|
[9420] | 543 | iface = ICourseTicketImport |
---|
[7548] | 544 | factory_name = 'waeup.CourseTicket' |
---|
| 545 | |
---|
| 546 | location_fields = [] |
---|
[9316] | 547 | additional_fields = ['level', 'code'] |
---|
[8232] | 548 | additional_headers = ['level', 'code'] |
---|
[7548] | 549 | |
---|
[9420] | 550 | @property |
---|
| 551 | def available_fields(self): |
---|
| 552 | fields = [ |
---|
| 553 | 'student_id','reg_number','matric_number', |
---|
| 554 | 'mandatory', 'score', 'carry_over', 'automatic', |
---|
| 555 | 'level_session' |
---|
| 556 | ] + self.additional_fields |
---|
| 557 | return sorted(fields) |
---|
| 558 | |
---|
[7548] | 559 | def getParent(self, row, site): |
---|
[8232] | 560 | student = self._getStudent(row, site) |
---|
| 561 | if student is None: |
---|
[7548] | 562 | return None |
---|
[8232] | 563 | return student['studycourse'].get(row['level']) |
---|
[7548] | 564 | |
---|
| 565 | def getEntry(self, row, site): |
---|
| 566 | level = self.getParent(row, site) |
---|
| 567 | if level is None: |
---|
| 568 | return None |
---|
| 569 | return level.get(row['code']) |
---|
| 570 | |
---|
[8626] | 571 | def updateEntry(self, obj, row, site): |
---|
| 572 | """Update obj to the values given in row. |
---|
| 573 | """ |
---|
| 574 | items_changed = super(CourseTicketProcessor, self).updateEntry( |
---|
| 575 | obj, row, site) |
---|
[8888] | 576 | parent = self.getParent(row, site) |
---|
[8626] | 577 | student = self.getParent(row, site).__parent__.__parent__ |
---|
| 578 | student.__parent__.logger.info( |
---|
[8888] | 579 | '%s - Course ticket in %s updated: %s' |
---|
| 580 | % (student.student_id, parent.level, items_changed)) |
---|
[8626] | 581 | return |
---|
| 582 | |
---|
[7548] | 583 | def addEntry(self, obj, row, site): |
---|
| 584 | parent = self.getParent(row, site) |
---|
| 585 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 586 | entries = list(catalog.searchResults(code=(row['code'],row['code']))) |
---|
| 587 | obj.fcode = entries[0].__parent__.__parent__.__parent__.code |
---|
| 588 | obj.dcode = entries[0].__parent__.__parent__.code |
---|
| 589 | obj.title = entries[0].title |
---|
[9420] | 590 | #if getattr(obj, 'credits', None) is None: |
---|
| 591 | obj.credits = entries[0].credits |
---|
| 592 | #if getattr(obj, 'passmark', None) is None: |
---|
| 593 | obj.passmark = entries[0].passmark |
---|
[7548] | 594 | obj.semester = entries[0].semester |
---|
| 595 | parent[row['code']] = obj |
---|
| 596 | return |
---|
| 597 | |
---|
[8888] | 598 | def delEntry(self, row, site): |
---|
| 599 | ticket = self.getEntry(row, site) |
---|
| 600 | parent = self.getParent(row, site) |
---|
| 601 | if ticket is not None: |
---|
| 602 | student = self._getStudent(row, site) |
---|
| 603 | student.__parent__.logger.info('%s - Course ticket in %s removed: %s' |
---|
| 604 | % (student.student_id, parent.level, ticket.code)) |
---|
| 605 | del parent[ticket.code] |
---|
| 606 | return |
---|
| 607 | |
---|
[7548] | 608 | def checkConversion(self, row, mode='ignore'): |
---|
| 609 | """Validates all values in row. |
---|
| 610 | """ |
---|
[8232] | 611 | errs, inv_errs, conv_dict = super( |
---|
| 612 | CourseTicketProcessor, self).checkConversion(row, mode=mode) |
---|
[9420] | 613 | if mode == 'remove': |
---|
| 614 | return errs, inv_errs, conv_dict |
---|
| 615 | # In update and create mode we have to check if course really exists. |
---|
[7548] | 616 | # This is not done by the converter. |
---|
| 617 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 618 | entries = catalog.searchResults(code=(row['code'],row['code'])) |
---|
| 619 | if len(entries) == 0: |
---|
| 620 | errs.append(('code','non-existent')) |
---|
| 621 | return errs, inv_errs, conv_dict |
---|
[9420] | 622 | # If level_session is provided in row we have to check if |
---|
| 623 | # the parent studylevel exists and if its level_session |
---|
| 624 | # attribute corresponds with the expected value in row. |
---|
[9421] | 625 | level_session = conv_dict.get('level_session', IGNORE_MARKER) |
---|
| 626 | if level_session not in (IGNORE_MARKER, None): |
---|
[9420] | 627 | site = grok.getSite() |
---|
| 628 | studylevel = self.getParent(row, site) |
---|
| 629 | if studylevel is not None: |
---|
| 630 | if studylevel.level_session != level_session: |
---|
| 631 | errs.append(('level_session','does not match %s' |
---|
| 632 | % studylevel.level_session)) |
---|
| 633 | else: |
---|
| 634 | errs.append(('level','does not exist')) |
---|
[7623] | 635 | return errs, inv_errs, conv_dict |
---|
| 636 | |
---|
[8232] | 637 | class StudentOnlinePaymentProcessor(StudentProcessorBase): |
---|
[7623] | 638 | """A batch processor for IStudentOnlinePayment objects. |
---|
| 639 | """ |
---|
| 640 | grok.implements(IBatchProcessor) |
---|
| 641 | grok.provides(IBatchProcessor) |
---|
| 642 | grok.context(Interface) |
---|
[7933] | 643 | util_name = 'paymentprocessor' |
---|
[7623] | 644 | grok.name(util_name) |
---|
| 645 | |
---|
[8942] | 646 | name = u'Student Payment Processor' |
---|
[8174] | 647 | iface = IStudentOnlinePayment |
---|
[7623] | 648 | factory_name = 'waeup.StudentOnlinePayment' |
---|
| 649 | |
---|
| 650 | location_fields = [] |
---|
[8232] | 651 | additional_fields = ['p_id'] |
---|
[8884] | 652 | additional_headers = [] |
---|
[7623] | 653 | |
---|
[8884] | 654 | def checkHeaders(self, headerfields, mode='ignore'): |
---|
| 655 | super(StudentOnlinePaymentProcessor, self).checkHeaders(headerfields) |
---|
[8885] | 656 | if mode in ('update', 'remove') and not 'p_id' in headerfields: |
---|
[8884] | 657 | raise FatalCSVError( |
---|
[8885] | 658 | "Need p_id for import in update and remove modes!") |
---|
[8884] | 659 | return True |
---|
| 660 | |
---|
[8232] | 661 | def parentsExist(self, row, site): |
---|
| 662 | return self.getParent(row, site) is not None |
---|
[7623] | 663 | |
---|
| 664 | def getParent(self, row, site): |
---|
[8232] | 665 | student = self._getStudent(row, site) |
---|
| 666 | if student is None: |
---|
[7623] | 667 | return None |
---|
[8232] | 668 | return student['payments'] |
---|
[7623] | 669 | |
---|
| 670 | def getEntry(self, row, site): |
---|
| 671 | payments = self.getParent(row, site) |
---|
| 672 | if payments is None: |
---|
| 673 | return None |
---|
[8884] | 674 | p_id = row.get('p_id', None) |
---|
| 675 | if p_id is None: |
---|
| 676 | return None |
---|
[7626] | 677 | # We can use the hash symbol at the end of p_id in import files |
---|
| 678 | # to avoid annoying automatic number transformation |
---|
| 679 | # by Excel or Calc |
---|
[8884] | 680 | p_id = p_id.strip('#') |
---|
| 681 | if not p_id.startswith('p'): |
---|
| 682 | # For data migration from old SRP only |
---|
| 683 | p_id = 'p' + p_id[7:] + '0' |
---|
| 684 | entry = payments.get(p_id) |
---|
[7623] | 685 | return entry |
---|
| 686 | |
---|
[8626] | 687 | def updateEntry(self, obj, row, site): |
---|
| 688 | """Update obj to the values given in row. |
---|
| 689 | """ |
---|
| 690 | items_changed = super(StudentOnlinePaymentProcessor, self).updateEntry( |
---|
| 691 | obj, row, site) |
---|
| 692 | student = self.getParent(row, site).__parent__ |
---|
| 693 | student.__parent__.logger.info( |
---|
[8886] | 694 | '%s - Payment ticket updated: %s' |
---|
| 695 | % (student.student_id, items_changed)) |
---|
[8626] | 696 | return |
---|
| 697 | |
---|
[7623] | 698 | def addEntry(self, obj, row, site): |
---|
| 699 | parent = self.getParent(row, site) |
---|
[7626] | 700 | p_id = row['p_id'].strip('#') |
---|
| 701 | if not p_id.startswith('p'): |
---|
[7623] | 702 | # For data migration from old SRP |
---|
[8884] | 703 | obj.p_id = 'p' + p_id[7:] + '0' |
---|
[7623] | 704 | parent[obj.p_id] = obj |
---|
| 705 | else: |
---|
[7626] | 706 | parent[p_id] = obj |
---|
[7623] | 707 | return |
---|
| 708 | |
---|
[8885] | 709 | def delEntry(self, row, site): |
---|
| 710 | payment = self.getEntry(row, site) |
---|
| 711 | parent = self.getParent(row, site) |
---|
| 712 | if payment is not None: |
---|
| 713 | student = self._getStudent(row, site) |
---|
[8886] | 714 | student.__parent__.logger.info('%s - Payment ticket removed: %s' |
---|
[8885] | 715 | % (student.student_id, payment.p_id)) |
---|
| 716 | del parent[payment.p_id] |
---|
[8886] | 717 | return |
---|
[8885] | 718 | |
---|
[7623] | 719 | def checkConversion(self, row, mode='ignore'): |
---|
| 720 | """Validates all values in row. |
---|
| 721 | """ |
---|
[8232] | 722 | errs, inv_errs, conv_dict = super( |
---|
| 723 | StudentOnlinePaymentProcessor, self).checkConversion(row, mode=mode) |
---|
| 724 | |
---|
[7623] | 725 | # We have to check p_id. |
---|
[8884] | 726 | p_id = row.get('p_id', None) |
---|
[8942] | 727 | if not p_id: |
---|
[8884] | 728 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
| 729 | p_id = "p%s" % timestamp |
---|
| 730 | conv_dict['p_id'] = p_id |
---|
| 731 | return errs, inv_errs, conv_dict |
---|
| 732 | else: |
---|
| 733 | p_id = p_id.strip('#') |
---|
[7626] | 734 | if p_id.startswith('p'): |
---|
| 735 | if not len(p_id) == 14: |
---|
[7623] | 736 | errs.append(('p_id','invalid length')) |
---|
| 737 | return errs, inv_errs, conv_dict |
---|
| 738 | else: |
---|
[7626] | 739 | if not len(p_id) == 19: |
---|
[7623] | 740 | errs.append(('p_id','invalid length')) |
---|
| 741 | return errs, inv_errs, conv_dict |
---|
| 742 | return errs, inv_errs, conv_dict |
---|
[7951] | 743 | |
---|
| 744 | class StudentVerdictProcessor(StudentStudyCourseProcessor): |
---|
[9418] | 745 | """A special batch processor for verdicts. |
---|
[7951] | 746 | |
---|
| 747 | Import verdicts and perform workflow transitions. |
---|
| 748 | """ |
---|
| 749 | |
---|
| 750 | util_name = 'verdictupdater' |
---|
| 751 | grok.name(util_name) |
---|
| 752 | |
---|
[9418] | 753 | name = u'Verdict Processor (special processor, update only)' |
---|
[7951] | 754 | iface = IStudentVerdictUpdate |
---|
| 755 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 756 | |
---|
[9284] | 757 | additional_fields = [ |
---|
| 758 | 'current_session', |
---|
| 759 | 'current_level', |
---|
[9296] | 760 | 'bypass_validation', |
---|
| 761 | 'validated_by'] |
---|
[7951] | 762 | |
---|
| 763 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 764 | """Checks requirements the studycourse and the student must fulfill |
---|
| 765 | before being updated. |
---|
| 766 | """ |
---|
| 767 | # Check if current_levels correspond |
---|
| 768 | if obj.current_level != row['current_level']: |
---|
| 769 | return 'Current level does not correspond.' |
---|
| 770 | # Check if current_sessions correspond |
---|
| 771 | if obj.current_session != row['current_session']: |
---|
| 772 | return 'Current session does not correspond.' |
---|
[9282] | 773 | # Check if new verdict is provided |
---|
| 774 | if row['current_verdict'] in (IGNORE_MARKER, ''): |
---|
[9293] | 775 | return 'No verdict in import file.' |
---|
| 776 | # Check if studylevel exists# |
---|
| 777 | level_string = str(obj.current_level) |
---|
| 778 | if obj.get(level_string) is None: |
---|
| 779 | return 'Study level object is missing.' |
---|
[9284] | 780 | # Check if student is in state REGISTERED or VALIDATED |
---|
[9296] | 781 | if row.get('bypass_validation'): |
---|
[9284] | 782 | if obj.student.state not in (VALIDATED, REGISTERED): |
---|
| 783 | return 'Student in wrong state.' |
---|
| 784 | else: |
---|
| 785 | if obj.student.state != VALIDATED: |
---|
| 786 | return 'Student in wrong state.' |
---|
[7951] | 787 | return None |
---|
| 788 | |
---|
| 789 | def updateEntry(self, obj, row, site): |
---|
| 790 | """Update obj to the values given in row. |
---|
| 791 | """ |
---|
[8221] | 792 | # Don't set current_session, current_level |
---|
| 793 | vals_to_set = dict((key, val) for key, val in row.items() |
---|
| 794 | if key not in ('current_session','current_level')) |
---|
[9293] | 795 | super(StudentVerdictProcessor, self).updateEntry(obj, vals_to_set, site) |
---|
[7951] | 796 | parent = self.getParent(row, site) |
---|
[9293] | 797 | # Set current_vedict in corresponding studylevel |
---|
| 798 | level_string = str(obj.current_level) |
---|
| 799 | obj[level_string].level_verdict = row['current_verdict'] |
---|
[9296] | 800 | # Fire transition and set studylevel attributes |
---|
| 801 | # depending on student's state |
---|
[9284] | 802 | if obj.__parent__.state == REGISTERED: |
---|
[9296] | 803 | validated_by = row.get('validated_by', '') |
---|
| 804 | if validated_by in (IGNORE_MARKER, ''): |
---|
| 805 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 806 | system = translate(_('System'),'waeup.kofa', |
---|
| 807 | target_language=portal_language) |
---|
| 808 | obj[level_string].validated_by = system |
---|
| 809 | else: |
---|
| 810 | obj[level_string].validated_by = validated_by |
---|
| 811 | obj[level_string].validation_date = datetime.utcnow() |
---|
[9284] | 812 | IWorkflowInfo(obj.__parent__).fireTransition('bypass_validation') |
---|
| 813 | else: |
---|
| 814 | IWorkflowInfo(obj.__parent__).fireTransition('return') |
---|
[7951] | 815 | # Update the students_catalog |
---|
| 816 | notify(grok.ObjectModifiedEvent(obj.__parent__)) |
---|
| 817 | return |
---|