[7191] | 1 | ## $Id: batching.py 12883 2015-04-25 06:13:18Z 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 |
---|
[10028] | 27 | import unicodecsv as csv # XXX: csv ops should move to dedicated module. |
---|
[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 |
---|
[9960] | 33 | from zope.component import queryUtility, getUtility, createObject |
---|
[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) |
---|
[12623] | 40 | from waeup.kofa.interfaces import IKofaUtils, DuplicationError |
---|
[7959] | 41 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7811] | 42 | from waeup.kofa.students.interfaces import ( |
---|
[9960] | 43 | IStudent, IStudentStudyCourse, IStudentStudyCourseTransfer, |
---|
[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): |
---|
[12872] | 53 | """The Student Processor imports student base data. |
---|
| 54 | |
---|
| 55 | In create mode no locator is required. If no `student_id` is given, |
---|
| 56 | the portal automatically assigns a new student id. |
---|
| 57 | |
---|
| 58 | In update or remove mode the processor uses |
---|
| 59 | either the `student_id`, `reg_number` or `matric_number` to localize the |
---|
| 60 | student object, exactly in this order. If `student_id` is given and an |
---|
| 61 | object can be found, `reg_number` and `matric_number` will be overwritten |
---|
| 62 | by the values provided in the import file. If `student_id` is missing, |
---|
| 63 | `reg_number` is used to localize the object and only `matric_number` |
---|
| 64 | will be overwritten. `matric_number` is used as locator only if both |
---|
| 65 | `student_id` and `reg_number` are missing. `student_id` can't be changed |
---|
| 66 | by the batch processor. |
---|
| 67 | |
---|
| 68 | There are two ways to change the registration state of the student, |
---|
| 69 | an unsafe and a safe way. The safe way makes use of workflow transitions. |
---|
| 70 | Transitions are only possible between allowed workflow states. Only |
---|
| 71 | transitions ensure that the registration workflow is maintained. |
---|
| 72 | |
---|
| 73 | **Always prefer the safe way!** |
---|
[6821] | 74 | """ |
---|
| 75 | grok.implements(IBatchProcessor) |
---|
| 76 | grok.provides(IBatchProcessor) |
---|
| 77 | grok.context(Interface) |
---|
[7933] | 78 | util_name = 'studentprocessor' |
---|
[6821] | 79 | grok.name(util_name) |
---|
| 80 | |
---|
[11891] | 81 | name = _('Student Processor') |
---|
[6821] | 82 | iface = IStudent |
---|
[8581] | 83 | iface_byregnumber = IStudentUpdateByRegNo |
---|
| 84 | iface_bymatricnumber = IStudentUpdateByMatricNo |
---|
[6821] | 85 | |
---|
| 86 | factory_name = 'waeup.Student' |
---|
| 87 | |
---|
| 88 | @property |
---|
[6849] | 89 | def available_fields(self): |
---|
[8176] | 90 | fields = getFields(self.iface) |
---|
[6849] | 91 | return sorted(list(set( |
---|
[7513] | 92 | ['student_id','reg_number','matric_number', |
---|
[8309] | 93 | 'password', 'state', 'transition'] + fields.keys()))) |
---|
[6821] | 94 | |
---|
[6849] | 95 | def checkHeaders(self, headerfields, mode='create'): |
---|
[8309] | 96 | if 'state' in headerfields and 'transition' in headerfields: |
---|
| 97 | raise FatalCSVError( |
---|
[12180] | 98 | "State and transition can't be imported at the same time!") |
---|
[6854] | 99 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 100 | in headerfields and not 'matric_number' in headerfields: |
---|
[6849] | 101 | raise FatalCSVError( |
---|
[6854] | 102 | "Need at least columns student_id or reg_number " + |
---|
| 103 | "or matric_number for import!") |
---|
[6849] | 104 | if mode == 'create': |
---|
| 105 | for field in self.required_fields: |
---|
| 106 | if not field in headerfields: |
---|
| 107 | raise FatalCSVError( |
---|
| 108 | "Need at least columns %s for import!" % |
---|
| 109 | ', '.join(["'%s'" % x for x in self.required_fields])) |
---|
| 110 | # Check for fields to be ignored... |
---|
| 111 | not_ignored_fields = [x for x in headerfields |
---|
| 112 | if not x.startswith('--')] |
---|
| 113 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 114 | raise FatalCSVError( |
---|
| 115 | "Double headers: each column name may only appear once.") |
---|
| 116 | return True |
---|
| 117 | |
---|
[6821] | 118 | def parentsExist(self, row, site): |
---|
| 119 | return 'students' in site.keys() |
---|
| 120 | |
---|
[6849] | 121 | def getLocator(self, row): |
---|
[8232] | 122 | if row.get('student_id',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 123 | return 'student_id' |
---|
[8232] | 124 | elif row.get('reg_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 125 | return 'reg_number' |
---|
[8232] | 126 | elif row.get('matric_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 127 | return 'matric_number' |
---|
| 128 | else: |
---|
| 129 | return None |
---|
| 130 | |
---|
[6821] | 131 | # The entry never exists in create mode. |
---|
| 132 | def entryExists(self, row, site): |
---|
[7267] | 133 | return self.getEntry(row, site) is not None |
---|
| 134 | |
---|
| 135 | def getParent(self, row, site): |
---|
| 136 | return site['students'] |
---|
| 137 | |
---|
| 138 | def getEntry(self, row, site): |
---|
[6846] | 139 | if not 'students' in site.keys(): |
---|
[6849] | 140 | return None |
---|
| 141 | if self.getLocator(row) == 'student_id': |
---|
[6846] | 142 | if row['student_id'] in site['students']: |
---|
| 143 | student = site['students'][row['student_id']] |
---|
| 144 | return student |
---|
[6849] | 145 | elif self.getLocator(row) == 'reg_number': |
---|
[6846] | 146 | reg_number = row['reg_number'] |
---|
| 147 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 148 | results = list( |
---|
| 149 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 150 | if results: |
---|
| 151 | return results[0] |
---|
[6849] | 152 | elif self.getLocator(row) == 'matric_number': |
---|
[6846] | 153 | matric_number = row['matric_number'] |
---|
| 154 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 155 | results = list( |
---|
| 156 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 157 | if results: |
---|
| 158 | return results[0] |
---|
[6849] | 159 | return None |
---|
[6821] | 160 | |
---|
| 161 | def addEntry(self, obj, row, site): |
---|
| 162 | parent = self.getParent(row, site) |
---|
| 163 | parent.addStudent(obj) |
---|
[8491] | 164 | # Reset _curr_stud_id if student_id has been imported |
---|
| 165 | if self.getLocator(row) == 'student_id': |
---|
| 166 | parent._curr_stud_id -= 1 |
---|
[8287] | 167 | # We have to log this if state is provided. If not, |
---|
[7959] | 168 | # logging is done by the event handler handle_student_added |
---|
[9701] | 169 | if 'state' in row: |
---|
[7959] | 170 | parent.logger.info('%s - Student record created' % obj.student_id) |
---|
[7656] | 171 | history = IObjectHistory(obj) |
---|
[7959] | 172 | history.addMessage(_('Student record created')) |
---|
[6821] | 173 | return |
---|
| 174 | |
---|
| 175 | def delEntry(self, row, site): |
---|
[7267] | 176 | student = self.getEntry(row, site) |
---|
[7263] | 177 | if student is not None: |
---|
[6846] | 178 | parent = self.getParent(row, site) |
---|
[7656] | 179 | parent.logger.info('%s - Student removed' % student.student_id) |
---|
[6846] | 180 | del parent[student.student_id] |
---|
[6821] | 181 | pass |
---|
[6825] | 182 | |
---|
[8309] | 183 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 184 | """Checks requirements the object must fulfill when being updated. |
---|
| 185 | |
---|
| 186 | This method is not used in case of deleting or adding objects. |
---|
| 187 | |
---|
| 188 | Returns error messages as strings in case of requirement |
---|
| 189 | problems. |
---|
| 190 | """ |
---|
| 191 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 192 | if transition not in (IGNORE_MARKER, ''): |
---|
| 193 | allowed_transitions = IWorkflowInfo(obj).getManualTransitionIds() |
---|
| 194 | if transition not in allowed_transitions: |
---|
| 195 | return 'Transition not allowed.' |
---|
[9028] | 196 | if transition in FORBIDDEN_POSTGRAD_TRANS and \ |
---|
| 197 | obj.is_postgrad: |
---|
| 198 | return 'Transition not allowed (pg student).' |
---|
| 199 | state = row.get('state', IGNORE_MARKER) |
---|
| 200 | if state not in (IGNORE_MARKER, ''): |
---|
| 201 | if state in FORBIDDEN_POSTGRAD_STATES and \ |
---|
| 202 | obj.is_postgrad: |
---|
| 203 | return 'State not allowed (pg student).' |
---|
[8309] | 204 | return None |
---|
| 205 | |
---|
[9706] | 206 | def updateEntry(self, obj, row, site, filename): |
---|
[7497] | 207 | """Update obj to the values given in row. |
---|
| 208 | """ |
---|
[8221] | 209 | items_changed = '' |
---|
| 210 | |
---|
[7643] | 211 | # Remove student_id from row if empty |
---|
[9701] | 212 | if 'student_id' in row and row['student_id'] in (None, IGNORE_MARKER): |
---|
[7643] | 213 | row.pop('student_id') |
---|
[8221] | 214 | |
---|
| 215 | # Update password |
---|
[9701] | 216 | # XXX: Take DELETION_MARKER into consideration |
---|
| 217 | if 'password' in row: |
---|
[8498] | 218 | passwd = row.get('password', IGNORE_MARKER) |
---|
| 219 | if passwd not in ('', IGNORE_MARKER): |
---|
| 220 | if passwd.startswith('{SSHA}'): |
---|
| 221 | # already encrypted password |
---|
| 222 | obj.password = passwd |
---|
| 223 | else: |
---|
| 224 | # not yet encrypted password |
---|
| 225 | IUserAccount(obj).setPassword(passwd) |
---|
| 226 | items_changed += ('%s=%s, ' % ('password', passwd)) |
---|
[8221] | 227 | row.pop('password') |
---|
| 228 | |
---|
| 229 | # Update registration state |
---|
[9701] | 230 | if 'state' in row: |
---|
[8498] | 231 | state = row.get('state', IGNORE_MARKER) |
---|
| 232 | if state not in (IGNORE_MARKER, ''): |
---|
| 233 | value = row['state'] |
---|
| 234 | IWorkflowState(obj).setState(value) |
---|
| 235 | msg = _("State '${a}' set", mapping = {'a':value}) |
---|
| 236 | history = IObjectHistory(obj) |
---|
| 237 | history.addMessage(msg) |
---|
| 238 | items_changed += ('%s=%s, ' % ('state', state)) |
---|
[8287] | 239 | row.pop('state') |
---|
[8498] | 240 | |
---|
[9701] | 241 | if 'transition' in row: |
---|
[8498] | 242 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 243 | if transition not in (IGNORE_MARKER, ''): |
---|
| 244 | value = row['transition'] |
---|
| 245 | IWorkflowInfo(obj).fireTransition(value) |
---|
| 246 | items_changed += ('%s=%s, ' % ('transition', transition)) |
---|
[8309] | 247 | row.pop('transition') |
---|
[8221] | 248 | |
---|
| 249 | # apply other values... |
---|
[8498] | 250 | items_changed += super(StudentProcessor, self).updateEntry( |
---|
[9706] | 251 | obj, row, site, filename) |
---|
[8221] | 252 | |
---|
| 253 | # Log actions... |
---|
[7656] | 254 | parent = self.getParent(row, site) |
---|
| 255 | if hasattr(obj,'student_id'): |
---|
[8995] | 256 | # Update mode: the student exists and we can get the student_id. |
---|
| 257 | # Create mode: the record contains the student_id |
---|
[7656] | 258 | parent.logger.info( |
---|
[9706] | 259 | '%s - %s - %s - updated: %s' |
---|
| 260 | % (self.name, filename, obj.student_id, items_changed)) |
---|
[7656] | 261 | else: |
---|
| 262 | # Create mode: the student does not yet exist |
---|
[9706] | 263 | # XXX: It seems that this never happens because student_id |
---|
| 264 | # is always set. |
---|
| 265 | parent.logger.info( |
---|
| 266 | '%s - %s - %s - imported: %s' |
---|
| 267 | % (self.name, filename, obj.student_id, items_changed)) |
---|
[8221] | 268 | return items_changed |
---|
[7497] | 269 | |
---|
[6849] | 270 | def getMapping(self, path, headerfields, mode): |
---|
| 271 | """Get a mapping from CSV file headerfields to actually used fieldnames. |
---|
| 272 | """ |
---|
| 273 | result = dict() |
---|
| 274 | reader = csv.reader(open(path, 'rb')) |
---|
| 275 | raw_header = reader.next() |
---|
| 276 | for num, field in enumerate(headerfields): |
---|
[8221] | 277 | if field not in ['student_id', 'reg_number', 'matric_number' |
---|
| 278 | ] and mode == 'remove': |
---|
[6849] | 279 | continue |
---|
| 280 | if field == u'--IGNORE--': |
---|
| 281 | # Skip ignored columns in failed and finished data files. |
---|
| 282 | continue |
---|
| 283 | result[raw_header[num]] = field |
---|
| 284 | return result |
---|
| 285 | |
---|
| 286 | def checkConversion(self, row, mode='create'): |
---|
| 287 | """Validates all values in row. |
---|
| 288 | """ |
---|
[7643] | 289 | iface = self.iface |
---|
[6849] | 290 | if mode in ['update', 'remove']: |
---|
| 291 | if self.getLocator(row) == 'reg_number': |
---|
[8581] | 292 | iface = self.iface_byregnumber |
---|
[6849] | 293 | elif self.getLocator(row) == 'matric_number': |
---|
[8581] | 294 | iface = self.iface_bymatricnumber |
---|
[6849] | 295 | converter = IObjectConverter(iface) |
---|
| 296 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
[8214] | 297 | row, self.factory_name, mode=mode) |
---|
[12883] | 298 | # We cannot import both state and transition. |
---|
| 299 | if 'transition' in row and 'state' in row: |
---|
| 300 | if row['transition'] not in (IGNORE_MARKER, '') and \ |
---|
| 301 | row['state'] not in (IGNORE_MARKER, ''): |
---|
| 302 | errs.append(('workflow','not allowed')) |
---|
| 303 | return errs, inv_errs, conv_dict |
---|
[9701] | 304 | if 'transition' in row: |
---|
[9028] | 305 | if row['transition'] not in IMPORTABLE_TRANSITIONS: |
---|
| 306 | if row['transition'] not in (IGNORE_MARKER, ''): |
---|
| 307 | errs.append(('transition','not allowed')) |
---|
[9701] | 308 | if 'state' in row: |
---|
[9028] | 309 | if row['state'] not in IMPORTABLE_STATES: |
---|
| 310 | if row['state'] not in (IGNORE_MARKER, ''): |
---|
| 311 | errs.append(('state','not allowed')) |
---|
| 312 | else: |
---|
| 313 | # State is an attribute of Student and must not |
---|
| 314 | # be changed if empty. |
---|
| 315 | conv_dict['state'] = IGNORE_MARKER |
---|
[8490] | 316 | try: |
---|
| 317 | # Correct stud_id counter. As the IConverter for students |
---|
| 318 | # creates student objects that are not used afterwards, we |
---|
| 319 | # have to fix the site-wide student_id counter. |
---|
| 320 | site = grok.getSite() |
---|
| 321 | students = site['students'] |
---|
| 322 | students._curr_stud_id -= 1 |
---|
| 323 | except (KeyError, TypeError, AttributeError): |
---|
| 324 | pass |
---|
[6849] | 325 | return errs, inv_errs, conv_dict |
---|
| 326 | |
---|
[8232] | 327 | |
---|
| 328 | class StudentProcessorBase(BatchProcessor): |
---|
| 329 | """A base for student subitem processor. |
---|
| 330 | |
---|
| 331 | Helps reducing redundancy. |
---|
[6825] | 332 | """ |
---|
[8232] | 333 | grok.baseclass() |
---|
[6825] | 334 | |
---|
[9420] | 335 | # additional available fields |
---|
[8884] | 336 | # beside 'student_id', 'reg_number' and 'matric_number' |
---|
[8232] | 337 | additional_fields = [] |
---|
[6825] | 338 | |
---|
[12869] | 339 | # additional required fields (subset of additional_fields) |
---|
| 340 | additional_fields_required = [] |
---|
[6849] | 341 | |
---|
[6825] | 342 | @property |
---|
| 343 | def available_fields(self): |
---|
[8232] | 344 | fields = ['student_id','reg_number','matric_number' |
---|
| 345 | ] + self.additional_fields |
---|
| 346 | return sorted(list(set(fields + getFields( |
---|
[6843] | 347 | self.iface).keys()))) |
---|
[6825] | 348 | |
---|
[6837] | 349 | def checkHeaders(self, headerfields, mode='ignore'): |
---|
[6854] | 350 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 351 | in headerfields and not 'matric_number' in headerfields: |
---|
[6825] | 352 | raise FatalCSVError( |
---|
[6854] | 353 | "Need at least columns student_id " + |
---|
| 354 | "or reg_number or matric_number for import!") |
---|
[12869] | 355 | for name in self.additional_fields_required: |
---|
[8232] | 356 | if not name in headerfields: |
---|
| 357 | raise FatalCSVError( |
---|
| 358 | "Need %s for import!" % name) |
---|
| 359 | |
---|
[6834] | 360 | # Check for fields to be ignored... |
---|
[6825] | 361 | not_ignored_fields = [x for x in headerfields |
---|
| 362 | if not x.startswith('--')] |
---|
| 363 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 364 | raise FatalCSVError( |
---|
| 365 | "Double headers: each column name may only appear once.") |
---|
| 366 | return True |
---|
| 367 | |
---|
[8232] | 368 | def _getStudent(self, row, site): |
---|
[8225] | 369 | NON_VALUES = ['', IGNORE_MARKER] |
---|
[6846] | 370 | if not 'students' in site.keys(): |
---|
[6849] | 371 | return None |
---|
[8225] | 372 | if row.get('student_id', '') not in NON_VALUES: |
---|
[6825] | 373 | if row['student_id'] in site['students']: |
---|
| 374 | student = site['students'][row['student_id']] |
---|
| 375 | return student |
---|
[8225] | 376 | elif row.get('reg_number', '') not in NON_VALUES: |
---|
[6825] | 377 | reg_number = row['reg_number'] |
---|
| 378 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 379 | results = list( |
---|
| 380 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 381 | if results: |
---|
| 382 | return results[0] |
---|
[8225] | 383 | elif row.get('matric_number', '') not in NON_VALUES: |
---|
[6843] | 384 | matric_number = row['matric_number'] |
---|
| 385 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 386 | results = list( |
---|
| 387 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 388 | if results: |
---|
| 389 | return results[0] |
---|
[6849] | 390 | return None |
---|
[6825] | 391 | |
---|
[7267] | 392 | def parentsExist(self, row, site): |
---|
| 393 | return self.getParent(row, site) is not None |
---|
| 394 | |
---|
[6825] | 395 | def entryExists(self, row, site): |
---|
[7534] | 396 | return self.getEntry(row, site) is not None |
---|
[6825] | 397 | |
---|
[8232] | 398 | def checkConversion(self, row, mode='ignore'): |
---|
| 399 | """Validates all values in row. |
---|
| 400 | """ |
---|
| 401 | converter = IObjectConverter(self.iface) |
---|
| 402 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
| 403 | row, self.factory_name, mode=mode) |
---|
| 404 | return errs, inv_errs, conv_dict |
---|
| 405 | |
---|
[8885] | 406 | def getMapping(self, path, headerfields, mode): |
---|
| 407 | """Get a mapping from CSV file headerfields to actually used fieldnames. |
---|
| 408 | """ |
---|
| 409 | result = dict() |
---|
| 410 | reader = csv.reader(open(path, 'rb')) |
---|
| 411 | raw_header = reader.next() |
---|
| 412 | for num, field in enumerate(headerfields): |
---|
[8888] | 413 | if field not in ['student_id', 'reg_number', 'matric_number', |
---|
| 414 | 'p_id', 'code', 'level' |
---|
[8885] | 415 | ] and mode == 'remove': |
---|
| 416 | continue |
---|
| 417 | if field == u'--IGNORE--': |
---|
| 418 | # Skip ignored columns in failed and finished data files. |
---|
| 419 | continue |
---|
| 420 | result[raw_header[num]] = field |
---|
| 421 | return result |
---|
[8232] | 422 | |
---|
[8885] | 423 | |
---|
[8232] | 424 | class StudentStudyCourseProcessor(StudentProcessorBase): |
---|
[12872] | 425 | """The Student Study Course Processor imports data which refer |
---|
| 426 | to the student's course of study. The study course container data |
---|
| 427 | describe the current state of the course of study and it stores the |
---|
| 428 | entry conditions, i.e. when the student started the course. |
---|
| 429 | |
---|
| 430 | Most important is the `certificate` attribute which tells us which course |
---|
| 431 | the student is studying. The terms 'study course' and 'course of study' |
---|
| 432 | are used synonymously. The 'certificate' is the study programme described |
---|
| 433 | in the acadmic section. The study course object stores a referrer to a |
---|
| 434 | certificate in the acadmic section. |
---|
| 435 | |
---|
| 436 | When importing a new certificate code, `checkConversion` does not only |
---|
| 437 | check whether a certificate with the same code exists, it also |
---|
| 438 | proves if `current_level` is inside the level range of the certificate. |
---|
| 439 | For example, some study programmes start at level 200. The imported |
---|
| 440 | current level must thus be 200 or higher. |
---|
| 441 | |
---|
| 442 | `checkUpdateRequirements` looks up if the imported values match the |
---|
| 443 | certificate already stored with the study course object. The imported |
---|
| 444 | `current_level` must be in the range of the certificate already |
---|
| 445 | stored. |
---|
| 446 | |
---|
| 447 | .. note:: |
---|
| 448 | |
---|
| 449 | The processor does only offer an update mode. An 'empty' study course |
---|
| 450 | object is automatically created when the student object is added. So this |
---|
| 451 | object always exists. It can neither be added a second time nor |
---|
| 452 | be removed. |
---|
| 453 | |
---|
| 454 | Students can be transferred by import. A transfer is initialized if the |
---|
| 455 | `entry_mode` value is ``transfer``. In this case `checkConversion` uses a |
---|
| 456 | different interface for data validation and `checkUpdateRequirements` |
---|
| 457 | ensures that a student can only be transferred twice. The student transfer |
---|
| 458 | process is described elsewhere. |
---|
[8232] | 459 | """ |
---|
| 460 | grok.implements(IBatchProcessor) |
---|
| 461 | grok.provides(IBatchProcessor) |
---|
| 462 | grok.context(Interface) |
---|
| 463 | util_name = 'studycourseupdater' |
---|
| 464 | grok.name(util_name) |
---|
| 465 | |
---|
[11891] | 466 | name = _('StudentStudyCourse Processor (update only)') |
---|
[8232] | 467 | iface = IStudentStudyCourse |
---|
[9960] | 468 | iface_transfer = IStudentStudyCourseTransfer |
---|
[8232] | 469 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 470 | |
---|
| 471 | def getParent(self, row, site): |
---|
| 472 | return self._getStudent(row, site) |
---|
| 473 | |
---|
[6825] | 474 | def getEntry(self, row, site): |
---|
[7534] | 475 | student = self.getParent(row, site) |
---|
[7536] | 476 | if student is None: |
---|
[6825] | 477 | return None |
---|
| 478 | return student.get('studycourse') |
---|
[7429] | 479 | |
---|
[9706] | 480 | def updateEntry(self, obj, row, site, filename): |
---|
[7429] | 481 | """Update obj to the values given in row. |
---|
| 482 | """ |
---|
[9960] | 483 | entry_mode = row.get('entry_mode', None) |
---|
| 484 | certificate = row.get('certificate', None) |
---|
[9962] | 485 | current_session = row.get('current_session', None) |
---|
| 486 | student = self.getParent(row, site) |
---|
[9960] | 487 | if entry_mode == 'transfer': |
---|
[9962] | 488 | # We do not expect any error here since we |
---|
| 489 | # checked all constraints in checkConversion and |
---|
| 490 | # in checkUpdateRequirements |
---|
| 491 | student.transfer( |
---|
| 492 | certificate=certificate, current_session=current_session) |
---|
[9960] | 493 | obj = student['studycourse'] |
---|
[8221] | 494 | items_changed = super(StudentStudyCourseProcessor, self).updateEntry( |
---|
[9706] | 495 | obj, row, site, filename) |
---|
[9962] | 496 | student.__parent__.logger.info( |
---|
[9706] | 497 | '%s - %s - %s - updated: %s' |
---|
[9962] | 498 | % (self.name, filename, student.student_id, items_changed)) |
---|
[7429] | 499 | # Update the students_catalog |
---|
[9962] | 500 | notify(grok.ObjectModifiedEvent(student)) |
---|
[7429] | 501 | return |
---|
| 502 | |
---|
[7532] | 503 | def checkConversion(self, row, mode='ignore'): |
---|
| 504 | """Validates all values in row. |
---|
| 505 | """ |
---|
[9960] | 506 | # We have to use the correct interface. Transfer |
---|
| 507 | # updates have different constraints. |
---|
| 508 | entry_mode = row.get('entry_mode', None) |
---|
| 509 | if entry_mode == 'transfer': |
---|
| 510 | converter = IObjectConverter(self.iface_transfer) |
---|
| 511 | else: |
---|
| 512 | converter = IObjectConverter(self.iface) |
---|
| 513 | errs, inv_errs, conv_dict = converter.fromStringDict( |
---|
| 514 | row, self.factory_name, mode=mode) |
---|
| 515 | |
---|
[7532] | 516 | # We have to check if current_level is in range of certificate. |
---|
[9701] | 517 | if 'certificate' in conv_dict and 'current_level' in conv_dict: |
---|
[8940] | 518 | cert = conv_dict['certificate'] |
---|
| 519 | level = conv_dict['current_level'] |
---|
| 520 | if level < cert.start_level or level > cert.end_level+120: |
---|
| 521 | errs.append(('current_level','not in range')) |
---|
[7532] | 522 | return errs, inv_errs, conv_dict |
---|
| 523 | |
---|
[9028] | 524 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 525 | """Checks requirements the object must fulfill when being updated. |
---|
| 526 | Returns error messages as strings in case of requirement |
---|
| 527 | problems. |
---|
| 528 | """ |
---|
[9960] | 529 | certificate = getattr(obj, 'certificate', None) |
---|
| 530 | entry_session = getattr(obj, 'entry_session', None) |
---|
[9028] | 531 | current_level = row.get('current_level', None) |
---|
[9960] | 532 | entry_mode = row.get('entry_mode', None) |
---|
| 533 | # We have to ensure that the student can be transferred. |
---|
| 534 | if entry_mode == 'transfer': |
---|
| 535 | if certificate is None or entry_session is None: |
---|
| 536 | return 'Former study course record incomplete.' |
---|
| 537 | if 'studycourse_1' in obj.__parent__.keys() and \ |
---|
| 538 | 'studycourse_2' in obj.__parent__.keys(): |
---|
| 539 | return 'Maximum number of transfers exceeded.' |
---|
[9735] | 540 | if current_level: |
---|
| 541 | if current_level == 999 and \ |
---|
| 542 | obj.__parent__.state in FORBIDDEN_POSTGRAD_STATES: |
---|
| 543 | return 'Not a pg student.' |
---|
| 544 | cert = row.get('certificate', None) |
---|
| 545 | if certificate is None and cert is None: |
---|
| 546 | return 'No certificate to check level.' |
---|
[9960] | 547 | if certificate is not None and cert is None and ( |
---|
[9735] | 548 | current_level < certificate.start_level or \ |
---|
| 549 | current_level > certificate.end_level+120): |
---|
| 550 | return 'current_level not in range.' |
---|
[9028] | 551 | return None |
---|
| 552 | |
---|
[8232] | 553 | class StudentStudyLevelProcessor(StudentProcessorBase): |
---|
[12872] | 554 | """The Student Study Level Processor imports study level data. |
---|
| 555 | It overwrites the container attributes but not the content of the container, |
---|
| 556 | i.e. the course tickets stored inside the container. There is nothing |
---|
| 557 | special about this processor. |
---|
[7536] | 558 | """ |
---|
| 559 | grok.implements(IBatchProcessor) |
---|
| 560 | grok.provides(IBatchProcessor) |
---|
| 561 | grok.context(Interface) |
---|
[7933] | 562 | util_name = 'studylevelprocessor' |
---|
[7536] | 563 | grok.name(util_name) |
---|
| 564 | |
---|
[11891] | 565 | name = _('StudentStudyLevel Processor') |
---|
[7536] | 566 | iface = IStudentStudyLevel |
---|
| 567 | factory_name = 'waeup.StudentStudyLevel' |
---|
| 568 | |
---|
[9690] | 569 | @property |
---|
| 570 | def available_fields(self): |
---|
| 571 | fields = super(StudentStudyLevelProcessor, self).available_fields |
---|
| 572 | fields.remove('total_credits') |
---|
[10479] | 573 | fields.remove('gpa') |
---|
[9690] | 574 | return fields |
---|
| 575 | |
---|
[7536] | 576 | def getParent(self, row, site): |
---|
[8232] | 577 | student = self._getStudent(row, site) |
---|
| 578 | if student is None: |
---|
[7536] | 579 | return None |
---|
[8232] | 580 | return student['studycourse'] |
---|
[7536] | 581 | |
---|
| 582 | def getEntry(self, row, site): |
---|
| 583 | studycourse = self.getParent(row, site) |
---|
| 584 | if studycourse is None: |
---|
| 585 | return None |
---|
[12873] | 586 | return studycourse.get(str(row['level'])) |
---|
[7536] | 587 | |
---|
[9301] | 588 | def delEntry(self, row, site): |
---|
| 589 | studylevel = self.getEntry(row, site) |
---|
| 590 | parent = self.getParent(row, site) |
---|
| 591 | if studylevel is not None: |
---|
| 592 | student = self._getStudent(row, site) |
---|
| 593 | student.__parent__.logger.info('%s - Level removed: %s' |
---|
| 594 | % (student.student_id, studylevel.__name__)) |
---|
| 595 | del parent[studylevel.__name__] |
---|
| 596 | return |
---|
| 597 | |
---|
[9706] | 598 | def updateEntry(self, obj, row, site, filename): |
---|
[8626] | 599 | """Update obj to the values given in row. |
---|
| 600 | """ |
---|
| 601 | items_changed = super(StudentStudyLevelProcessor, self).updateEntry( |
---|
[9706] | 602 | obj, row, site, filename) |
---|
[8626] | 603 | student = self.getParent(row, site).__parent__ |
---|
| 604 | student.__parent__.logger.info( |
---|
[9706] | 605 | '%s - %s - %s - updated: %s' |
---|
| 606 | % (self.name, filename, student.student_id, items_changed)) |
---|
[8626] | 607 | return |
---|
| 608 | |
---|
[7536] | 609 | def addEntry(self, obj, row, site): |
---|
| 610 | parent = self.getParent(row, site) |
---|
[12873] | 611 | parent[str(row['level'])] = obj |
---|
[7536] | 612 | return |
---|
| 613 | |
---|
[8232] | 614 | class CourseTicketProcessor(StudentProcessorBase): |
---|
[12882] | 615 | """The Course Ticket Processor imports course tickets, the subobjects |
---|
| 616 | of student study levels (= course lists). |
---|
| 617 | |
---|
| 618 | An imported course ticket contains a copy of the original course data. |
---|
| 619 | During import only a few attributes can be set/overwritten. |
---|
| 620 | |
---|
| 621 | Like all other student data importers, this processor also requires |
---|
| 622 | either `student_id`, `reg_number` or `matric_number` to find the student. |
---|
| 623 | Then it needs `level` and `code` to localize the course ticket. |
---|
| 624 | |
---|
| 625 | `checkConversion` first searches the courses catalog for the imported |
---|
| 626 | `code` and ensures that a course with such a code really exists |
---|
| 627 | in the academic section. It furthermore checks if `level_session` in |
---|
| 628 | the row corresponds with the session of the parent student |
---|
| 629 | study level object. It fails if one of the conditions is not met. |
---|
| 630 | |
---|
| 631 | In create mode `fcode`, `dcode`, `title`, `credits`, `passmark` and |
---|
| 632 | `semester` are taken from the course found in the academic section. |
---|
| 633 | These attributes can nevermore be changed, neither via the user interface |
---|
| 634 | nor by import. |
---|
[7548] | 635 | """ |
---|
| 636 | grok.implements(IBatchProcessor) |
---|
| 637 | grok.provides(IBatchProcessor) |
---|
| 638 | grok.context(Interface) |
---|
[7933] | 639 | util_name = 'courseticketprocessor' |
---|
[7548] | 640 | grok.name(util_name) |
---|
| 641 | |
---|
[11891] | 642 | name = _('CourseTicket Processor') |
---|
[9420] | 643 | iface = ICourseTicketImport |
---|
[7548] | 644 | factory_name = 'waeup.CourseTicket' |
---|
| 645 | |
---|
[9316] | 646 | additional_fields = ['level', 'code'] |
---|
[12869] | 647 | additional_fields_required = additional_fields |
---|
[7548] | 648 | |
---|
[9420] | 649 | @property |
---|
| 650 | def available_fields(self): |
---|
| 651 | fields = [ |
---|
| 652 | 'student_id','reg_number','matric_number', |
---|
| 653 | 'mandatory', 'score', 'carry_over', 'automatic', |
---|
| 654 | 'level_session' |
---|
| 655 | ] + self.additional_fields |
---|
| 656 | return sorted(fields) |
---|
| 657 | |
---|
[7548] | 658 | def getParent(self, row, site): |
---|
[8232] | 659 | student = self._getStudent(row, site) |
---|
| 660 | if student is None: |
---|
[7548] | 661 | return None |
---|
[12873] | 662 | return student['studycourse'].get(str(row['level'])) |
---|
[7548] | 663 | |
---|
| 664 | def getEntry(self, row, site): |
---|
| 665 | level = self.getParent(row, site) |
---|
| 666 | if level is None: |
---|
| 667 | return None |
---|
| 668 | return level.get(row['code']) |
---|
| 669 | |
---|
[9706] | 670 | def updateEntry(self, obj, row, site, filename): |
---|
[8626] | 671 | """Update obj to the values given in row. |
---|
| 672 | """ |
---|
| 673 | items_changed = super(CourseTicketProcessor, self).updateEntry( |
---|
[9706] | 674 | obj, row, site, filename) |
---|
[8888] | 675 | parent = self.getParent(row, site) |
---|
[8626] | 676 | student = self.getParent(row, site).__parent__.__parent__ |
---|
| 677 | student.__parent__.logger.info( |
---|
[9706] | 678 | '%s - %s - %s - %s - updated: %s' |
---|
| 679 | % (self.name, filename, student.student_id, parent.level, items_changed)) |
---|
[8626] | 680 | return |
---|
| 681 | |
---|
[7548] | 682 | def addEntry(self, obj, row, site): |
---|
| 683 | parent = self.getParent(row, site) |
---|
| 684 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 685 | entries = list(catalog.searchResults(code=(row['code'],row['code']))) |
---|
| 686 | obj.fcode = entries[0].__parent__.__parent__.__parent__.code |
---|
| 687 | obj.dcode = entries[0].__parent__.__parent__.code |
---|
| 688 | obj.title = entries[0].title |
---|
[9420] | 689 | #if getattr(obj, 'credits', None) is None: |
---|
| 690 | obj.credits = entries[0].credits |
---|
| 691 | #if getattr(obj, 'passmark', None) is None: |
---|
| 692 | obj.passmark = entries[0].passmark |
---|
[7548] | 693 | obj.semester = entries[0].semester |
---|
| 694 | parent[row['code']] = obj |
---|
| 695 | return |
---|
| 696 | |
---|
[8888] | 697 | def delEntry(self, row, site): |
---|
| 698 | ticket = self.getEntry(row, site) |
---|
| 699 | parent = self.getParent(row, site) |
---|
| 700 | if ticket is not None: |
---|
| 701 | student = self._getStudent(row, site) |
---|
| 702 | student.__parent__.logger.info('%s - Course ticket in %s removed: %s' |
---|
| 703 | % (student.student_id, parent.level, ticket.code)) |
---|
| 704 | del parent[ticket.code] |
---|
| 705 | return |
---|
| 706 | |
---|
[7548] | 707 | def checkConversion(self, row, mode='ignore'): |
---|
| 708 | """Validates all values in row. |
---|
| 709 | """ |
---|
[8232] | 710 | errs, inv_errs, conv_dict = super( |
---|
| 711 | CourseTicketProcessor, self).checkConversion(row, mode=mode) |
---|
[9420] | 712 | if mode == 'remove': |
---|
| 713 | return errs, inv_errs, conv_dict |
---|
| 714 | # In update and create mode we have to check if course really exists. |
---|
[7548] | 715 | # This is not done by the converter. |
---|
| 716 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 717 | entries = catalog.searchResults(code=(row['code'],row['code'])) |
---|
| 718 | if len(entries) == 0: |
---|
| 719 | errs.append(('code','non-existent')) |
---|
| 720 | return errs, inv_errs, conv_dict |
---|
[9420] | 721 | # If level_session is provided in row we have to check if |
---|
| 722 | # the parent studylevel exists and if its level_session |
---|
| 723 | # attribute corresponds with the expected value in row. |
---|
[9421] | 724 | level_session = conv_dict.get('level_session', IGNORE_MARKER) |
---|
| 725 | if level_session not in (IGNORE_MARKER, None): |
---|
[9420] | 726 | site = grok.getSite() |
---|
| 727 | studylevel = self.getParent(row, site) |
---|
| 728 | if studylevel is not None: |
---|
| 729 | if studylevel.level_session != level_session: |
---|
| 730 | errs.append(('level_session','does not match %s' |
---|
| 731 | % studylevel.level_session)) |
---|
| 732 | else: |
---|
| 733 | errs.append(('level','does not exist')) |
---|
[7623] | 734 | return errs, inv_errs, conv_dict |
---|
| 735 | |
---|
[8232] | 736 | class StudentOnlinePaymentProcessor(StudentProcessorBase): |
---|
[12882] | 737 | """The Course Ticket Processor imports student payment tickets. The |
---|
| 738 | tickets are located in the ``payments`` subfolder of the student |
---|
| 739 | container. The only additional locator is `p_id`, the object id. |
---|
| 740 | |
---|
| 741 | The `checkConversion` method checks the format of the payment identifier. |
---|
| 742 | In create mode it does also ensures that same p_id does not exist |
---|
| 743 | elsewhere. It must be portal-wide unique. |
---|
| 744 | |
---|
| 745 | When adding a payment ticket, the `addEntry` method checks if the same |
---|
| 746 | payment has already been made. It compares `p_category` and `p_session` |
---|
| 747 | in the row with the corresponding attributes of existing payment |
---|
| 748 | tickets in state ``paid``. If they match, a `DuplicationError` is raised. |
---|
[7623] | 749 | """ |
---|
| 750 | grok.implements(IBatchProcessor) |
---|
| 751 | grok.provides(IBatchProcessor) |
---|
| 752 | grok.context(Interface) |
---|
[7933] | 753 | util_name = 'paymentprocessor' |
---|
[7623] | 754 | grok.name(util_name) |
---|
| 755 | |
---|
[11891] | 756 | name = _('StudentOnlinePayment Processor') |
---|
[8174] | 757 | iface = IStudentOnlinePayment |
---|
[7623] | 758 | factory_name = 'waeup.StudentOnlinePayment' |
---|
| 759 | |
---|
[8232] | 760 | additional_fields = ['p_id'] |
---|
[7623] | 761 | |
---|
[8884] | 762 | def checkHeaders(self, headerfields, mode='ignore'): |
---|
| 763 | super(StudentOnlinePaymentProcessor, self).checkHeaders(headerfields) |
---|
[8885] | 764 | if mode in ('update', 'remove') and not 'p_id' in headerfields: |
---|
[8884] | 765 | raise FatalCSVError( |
---|
[8885] | 766 | "Need p_id for import in update and remove modes!") |
---|
[8884] | 767 | return True |
---|
| 768 | |
---|
[8232] | 769 | def parentsExist(self, row, site): |
---|
| 770 | return self.getParent(row, site) is not None |
---|
[7623] | 771 | |
---|
| 772 | def getParent(self, row, site): |
---|
[8232] | 773 | student = self._getStudent(row, site) |
---|
| 774 | if student is None: |
---|
[7623] | 775 | return None |
---|
[8232] | 776 | return student['payments'] |
---|
[7623] | 777 | |
---|
| 778 | def getEntry(self, row, site): |
---|
| 779 | payments = self.getParent(row, site) |
---|
| 780 | if payments is None: |
---|
| 781 | return None |
---|
[8884] | 782 | p_id = row.get('p_id', None) |
---|
| 783 | if p_id is None: |
---|
| 784 | return None |
---|
[7626] | 785 | # We can use the hash symbol at the end of p_id in import files |
---|
| 786 | # to avoid annoying automatic number transformation |
---|
| 787 | # by Excel or Calc |
---|
[8884] | 788 | p_id = p_id.strip('#') |
---|
[9467] | 789 | if len(p_id.split('-')) != 3 and not p_id.startswith('p'): |
---|
[8884] | 790 | # For data migration from old SRP only |
---|
| 791 | p_id = 'p' + p_id[7:] + '0' |
---|
| 792 | entry = payments.get(p_id) |
---|
[7623] | 793 | return entry |
---|
| 794 | |
---|
[9706] | 795 | def updateEntry(self, obj, row, site, filename): |
---|
[8626] | 796 | """Update obj to the values given in row. |
---|
| 797 | """ |
---|
| 798 | items_changed = super(StudentOnlinePaymentProcessor, self).updateEntry( |
---|
[9706] | 799 | obj, row, site, filename) |
---|
[8626] | 800 | student = self.getParent(row, site).__parent__ |
---|
| 801 | student.__parent__.logger.info( |
---|
[9706] | 802 | '%s - %s - %s - updated: %s' |
---|
| 803 | % (self.name, filename, student.student_id, items_changed)) |
---|
[8626] | 804 | return |
---|
| 805 | |
---|
[12623] | 806 | def samePaymentMade(self, student, category, p_session): |
---|
| 807 | for key in student['payments'].keys(): |
---|
| 808 | ticket = student['payments'][key] |
---|
| 809 | if ticket.p_state == 'paid' and\ |
---|
| 810 | ticket.p_category == category and \ |
---|
| 811 | ticket.p_session == p_session: |
---|
| 812 | return True |
---|
| 813 | return False |
---|
| 814 | |
---|
[7623] | 815 | def addEntry(self, obj, row, site): |
---|
| 816 | parent = self.getParent(row, site) |
---|
[12623] | 817 | student = parent.student |
---|
[7626] | 818 | p_id = row['p_id'].strip('#') |
---|
[12623] | 819 | # Requirement added on 19/02/2015: same payment must not exist. |
---|
| 820 | if self.samePaymentMade(student, obj.p_category, obj.p_session): |
---|
| 821 | raise DuplicationError('Same payment has already been made.') |
---|
[9467] | 822 | if len(p_id.split('-')) != 3 and not p_id.startswith('p'): |
---|
[7623] | 823 | # For data migration from old SRP |
---|
[8884] | 824 | obj.p_id = 'p' + p_id[7:] + '0' |
---|
[7623] | 825 | parent[obj.p_id] = obj |
---|
| 826 | else: |
---|
[7626] | 827 | parent[p_id] = obj |
---|
[7623] | 828 | return |
---|
| 829 | |
---|
[8885] | 830 | def delEntry(self, row, site): |
---|
| 831 | payment = self.getEntry(row, site) |
---|
| 832 | parent = self.getParent(row, site) |
---|
| 833 | if payment is not None: |
---|
| 834 | student = self._getStudent(row, site) |
---|
[8886] | 835 | student.__parent__.logger.info('%s - Payment ticket removed: %s' |
---|
[8885] | 836 | % (student.student_id, payment.p_id)) |
---|
| 837 | del parent[payment.p_id] |
---|
[8886] | 838 | return |
---|
[8885] | 839 | |
---|
[7623] | 840 | def checkConversion(self, row, mode='ignore'): |
---|
| 841 | """Validates all values in row. |
---|
| 842 | """ |
---|
[8232] | 843 | errs, inv_errs, conv_dict = super( |
---|
| 844 | StudentOnlinePaymentProcessor, self).checkConversion(row, mode=mode) |
---|
| 845 | |
---|
[7623] | 846 | # We have to check p_id. |
---|
[8884] | 847 | p_id = row.get('p_id', None) |
---|
[8942] | 848 | if not p_id: |
---|
[8884] | 849 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
| 850 | p_id = "p%s" % timestamp |
---|
| 851 | conv_dict['p_id'] = p_id |
---|
| 852 | return errs, inv_errs, conv_dict |
---|
| 853 | else: |
---|
| 854 | p_id = p_id.strip('#') |
---|
[7626] | 855 | if p_id.startswith('p'): |
---|
| 856 | if not len(p_id) == 14: |
---|
[7623] | 857 | errs.append(('p_id','invalid length')) |
---|
| 858 | return errs, inv_errs, conv_dict |
---|
[9467] | 859 | elif len(p_id.split('-')) == 3: |
---|
| 860 | # The SRP used either pins as keys ... |
---|
[9479] | 861 | if len(p_id.split('-')[2]) not in (9, 10): |
---|
[9467] | 862 | errs.append(('p_id','invalid pin')) |
---|
| 863 | return errs, inv_errs, conv_dict |
---|
[7623] | 864 | else: |
---|
[9467] | 865 | # ... or order_ids. |
---|
[7626] | 866 | if not len(p_id) == 19: |
---|
[11779] | 867 | errs.append(('p_id','invalid format')) |
---|
[7623] | 868 | return errs, inv_errs, conv_dict |
---|
[12513] | 869 | # Requirement added on 24/01/2015: p_id must be portal-wide unique. |
---|
| 870 | if mode == 'create': |
---|
| 871 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 872 | results = list(cat.searchResults(p_id=(p_id, p_id))) |
---|
| 873 | if len(results) > 0: |
---|
| 874 | sids = [payment.student.student_id for payment in results] |
---|
| 875 | sids_string = '' |
---|
| 876 | for id in sids: |
---|
| 877 | sids_string += '%s ' % id |
---|
| 878 | errs.append(('p_id','p_id exists in %s' % sids_string)) |
---|
| 879 | return errs, inv_errs, conv_dict |
---|
[7623] | 880 | return errs, inv_errs, conv_dict |
---|
[7951] | 881 | |
---|
| 882 | class StudentVerdictProcessor(StudentStudyCourseProcessor): |
---|
[12882] | 883 | """The Student Verdict Processor inherits from the Student Study |
---|
| 884 | Course Processor. It's a pure updater. Import step 2 raises a warning |
---|
| 885 | message if a datacenter manager tries to select another mode. |
---|
| 886 | But it does more than only overwriting study course attributes. |
---|
[7951] | 887 | |
---|
[12882] | 888 | The Student Verdict Processor is the only processor which cannot be |
---|
| 889 | used for restoring data. Purpose is to announce a verdict at the end of |
---|
| 890 | each academic session. The processor does not only import a verdict, |
---|
| 891 | it also conditions the student data so that the student can pay for the |
---|
| 892 | next session and proceed to the next study level. |
---|
| 893 | |
---|
| 894 | The `checkUpdateRequirements` method ensures that the imported data |
---|
| 895 | really correspond to the actual state of the student. |
---|
| 896 | `current_level` and `current_session` in the row must be on par |
---|
| 897 | with the attributes of the study course object. Thus, the processor |
---|
| 898 | does not use these values to overwrite the attributes of the study course |
---|
| 899 | but to control that the verdict is really meant for the current session of |
---|
| 900 | the student. The verdict is only imported if a corresponding study level |
---|
| 901 | object exists and the student is in the right registration state, |
---|
| 902 | either ``courses validated`` or ``courses registered``. Course registration |
---|
| 903 | can be bypassed by setting `bypass_validation` to ``True``. |
---|
| 904 | |
---|
| 905 | The `updateEntry` method does not only update the current verdict of |
---|
| 906 | the student study course, it also updates the matching student study |
---|
| 907 | level object. It saves the current verdict as `level_verdict` and sets |
---|
| 908 | the `validated_by` and `validation_date` attributes, whereas `validated_by` |
---|
| 909 | is taken from the row of the import file and `validation_date` is set to the |
---|
| 910 | actual UTC datetime. Finally, the student is moved to state ``returning``. |
---|
[7951] | 911 | """ |
---|
| 912 | |
---|
| 913 | util_name = 'verdictupdater' |
---|
| 914 | grok.name(util_name) |
---|
| 915 | |
---|
[11891] | 916 | name = _('Verdict Processor (special processor, update only)') |
---|
[7951] | 917 | iface = IStudentVerdictUpdate |
---|
| 918 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 919 | |
---|
| 920 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 921 | """Checks requirements the studycourse and the student must fulfill |
---|
| 922 | before being updated. |
---|
| 923 | """ |
---|
| 924 | # Check if current_levels correspond |
---|
| 925 | if obj.current_level != row['current_level']: |
---|
| 926 | return 'Current level does not correspond.' |
---|
| 927 | # Check if current_sessions correspond |
---|
| 928 | if obj.current_session != row['current_session']: |
---|
| 929 | return 'Current session does not correspond.' |
---|
[9282] | 930 | # Check if new verdict is provided |
---|
| 931 | if row['current_verdict'] in (IGNORE_MARKER, ''): |
---|
[9293] | 932 | return 'No verdict in import file.' |
---|
[9631] | 933 | # Check if studylevel exists |
---|
[9293] | 934 | level_string = str(obj.current_level) |
---|
| 935 | if obj.get(level_string) is None: |
---|
| 936 | return 'Study level object is missing.' |
---|
[9284] | 937 | # Check if student is in state REGISTERED or VALIDATED |
---|
[9296] | 938 | if row.get('bypass_validation'): |
---|
[9284] | 939 | if obj.student.state not in (VALIDATED, REGISTERED): |
---|
| 940 | return 'Student in wrong state.' |
---|
| 941 | else: |
---|
| 942 | if obj.student.state != VALIDATED: |
---|
| 943 | return 'Student in wrong state.' |
---|
[7951] | 944 | return None |
---|
| 945 | |
---|
[9706] | 946 | def updateEntry(self, obj, row, site, filename): |
---|
[7951] | 947 | """Update obj to the values given in row. |
---|
| 948 | """ |
---|
[8221] | 949 | # Don't set current_session, current_level |
---|
| 950 | vals_to_set = dict((key, val) for key, val in row.items() |
---|
| 951 | if key not in ('current_session','current_level')) |
---|
[9706] | 952 | super(StudentVerdictProcessor, self).updateEntry( |
---|
| 953 | obj, vals_to_set, site, filename) |
---|
[7951] | 954 | parent = self.getParent(row, site) |
---|
[12882] | 955 | # Set current_verdict in corresponding studylevel |
---|
[9293] | 956 | level_string = str(obj.current_level) |
---|
| 957 | obj[level_string].level_verdict = row['current_verdict'] |
---|
[9296] | 958 | # Fire transition and set studylevel attributes |
---|
| 959 | # depending on student's state |
---|
[9284] | 960 | if obj.__parent__.state == REGISTERED: |
---|
[9296] | 961 | validated_by = row.get('validated_by', '') |
---|
| 962 | if validated_by in (IGNORE_MARKER, ''): |
---|
| 963 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 964 | system = translate(_('System'),'waeup.kofa', |
---|
| 965 | target_language=portal_language) |
---|
| 966 | obj[level_string].validated_by = system |
---|
| 967 | else: |
---|
| 968 | obj[level_string].validated_by = validated_by |
---|
| 969 | obj[level_string].validation_date = datetime.utcnow() |
---|
[9284] | 970 | IWorkflowInfo(obj.__parent__).fireTransition('bypass_validation') |
---|
| 971 | else: |
---|
| 972 | IWorkflowInfo(obj.__parent__).fireTransition('return') |
---|
[7951] | 973 | # Update the students_catalog |
---|
| 974 | notify(grok.ObjectModifiedEvent(obj.__parent__)) |
---|
| 975 | return |
---|