[7191] | 1 | ## $Id: batching.py 15446 2019-06-03 10:32:23Z 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) |
---|
[13871] | 47 | from waeup.kofa.payments.interfaces import IPayer |
---|
[8309] | 48 | from waeup.kofa.students.workflow import ( |
---|
[9028] | 49 | IMPORTABLE_STATES, IMPORTABLE_TRANSITIONS, |
---|
| 50 | FORBIDDEN_POSTGRAD_TRANS, FORBIDDEN_POSTGRAD_STATES) |
---|
[7811] | 51 | from waeup.kofa.utils.batching import BatchProcessor |
---|
[6821] | 52 | |
---|
| 53 | class StudentProcessor(BatchProcessor): |
---|
[12872] | 54 | """The Student Processor imports student base data. |
---|
| 55 | |
---|
| 56 | In create mode no locator is required. If no `student_id` is given, |
---|
| 57 | the portal automatically assigns a new student id. |
---|
| 58 | |
---|
| 59 | In update or remove mode the processor uses |
---|
| 60 | either the `student_id`, `reg_number` or `matric_number` to localize the |
---|
| 61 | student object, exactly in this order. If `student_id` is given and an |
---|
| 62 | object can be found, `reg_number` and `matric_number` will be overwritten |
---|
| 63 | by the values provided in the import file. If `student_id` is missing, |
---|
| 64 | `reg_number` is used to localize the object and only `matric_number` |
---|
| 65 | will be overwritten. `matric_number` is used as locator only if both |
---|
| 66 | `student_id` and `reg_number` are missing. `student_id` can't be changed |
---|
| 67 | by the batch processor. |
---|
| 68 | |
---|
[13024] | 69 | There are two ways to change the workflow state of the student, |
---|
[12872] | 70 | an unsafe and a safe way. The safe way makes use of workflow transitions. |
---|
| 71 | Transitions are only possible between allowed workflow states. Only |
---|
| 72 | transitions ensure that the registration workflow is maintained. |
---|
| 73 | |
---|
| 74 | **Always prefer the safe way!** |
---|
[6821] | 75 | """ |
---|
| 76 | grok.implements(IBatchProcessor) |
---|
| 77 | grok.provides(IBatchProcessor) |
---|
| 78 | grok.context(Interface) |
---|
[7933] | 79 | util_name = 'studentprocessor' |
---|
[6821] | 80 | grok.name(util_name) |
---|
| 81 | |
---|
[11891] | 82 | name = _('Student Processor') |
---|
[6821] | 83 | iface = IStudent |
---|
[8581] | 84 | iface_byregnumber = IStudentUpdateByRegNo |
---|
| 85 | iface_bymatricnumber = IStudentUpdateByMatricNo |
---|
[6821] | 86 | |
---|
| 87 | factory_name = 'waeup.Student' |
---|
| 88 | |
---|
| 89 | @property |
---|
[6849] | 90 | def available_fields(self): |
---|
[8176] | 91 | fields = getFields(self.iface) |
---|
[6849] | 92 | return sorted(list(set( |
---|
[7513] | 93 | ['student_id','reg_number','matric_number', |
---|
[8309] | 94 | 'password', 'state', 'transition'] + fields.keys()))) |
---|
[6821] | 95 | |
---|
[6849] | 96 | def checkHeaders(self, headerfields, mode='create'): |
---|
[8309] | 97 | if 'state' in headerfields and 'transition' in headerfields: |
---|
| 98 | raise FatalCSVError( |
---|
[12180] | 99 | "State and transition can't be imported at the same time!") |
---|
[6854] | 100 | if not 'reg_number' in headerfields and not 'student_id' \ |
---|
| 101 | in headerfields and not 'matric_number' in headerfields: |
---|
[6849] | 102 | raise FatalCSVError( |
---|
[6854] | 103 | "Need at least columns student_id or reg_number " + |
---|
| 104 | "or matric_number for import!") |
---|
[6849] | 105 | if mode == 'create': |
---|
| 106 | for field in self.required_fields: |
---|
| 107 | if not field in headerfields: |
---|
| 108 | raise FatalCSVError( |
---|
| 109 | "Need at least columns %s for import!" % |
---|
| 110 | ', '.join(["'%s'" % x for x in self.required_fields])) |
---|
| 111 | # Check for fields to be ignored... |
---|
| 112 | not_ignored_fields = [x for x in headerfields |
---|
| 113 | if not x.startswith('--')] |
---|
| 114 | if len(set(not_ignored_fields)) < len(not_ignored_fields): |
---|
| 115 | raise FatalCSVError( |
---|
| 116 | "Double headers: each column name may only appear once.") |
---|
| 117 | return True |
---|
| 118 | |
---|
[6821] | 119 | def parentsExist(self, row, site): |
---|
| 120 | return 'students' in site.keys() |
---|
| 121 | |
---|
[6849] | 122 | def getLocator(self, row): |
---|
[8232] | 123 | if row.get('student_id',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 124 | return 'student_id' |
---|
[8232] | 125 | elif row.get('reg_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 126 | return 'reg_number' |
---|
[8232] | 127 | elif row.get('matric_number',None) not in (None, IGNORE_MARKER): |
---|
[6849] | 128 | return 'matric_number' |
---|
| 129 | else: |
---|
| 130 | return None |
---|
| 131 | |
---|
[6821] | 132 | # The entry never exists in create mode. |
---|
| 133 | def entryExists(self, row, site): |
---|
[7267] | 134 | return self.getEntry(row, site) is not None |
---|
| 135 | |
---|
| 136 | def getParent(self, row, site): |
---|
| 137 | return site['students'] |
---|
| 138 | |
---|
| 139 | def getEntry(self, row, site): |
---|
[6846] | 140 | if not 'students' in site.keys(): |
---|
[6849] | 141 | return None |
---|
| 142 | if self.getLocator(row) == 'student_id': |
---|
[6846] | 143 | if row['student_id'] in site['students']: |
---|
| 144 | student = site['students'][row['student_id']] |
---|
| 145 | return student |
---|
[6849] | 146 | elif self.getLocator(row) == 'reg_number': |
---|
[6846] | 147 | reg_number = row['reg_number'] |
---|
| 148 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 149 | results = list( |
---|
| 150 | cat.searchResults(reg_number=(reg_number, reg_number))) |
---|
| 151 | if results: |
---|
| 152 | return results[0] |
---|
[6849] | 153 | elif self.getLocator(row) == 'matric_number': |
---|
[6846] | 154 | matric_number = row['matric_number'] |
---|
| 155 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
| 156 | results = list( |
---|
| 157 | cat.searchResults(matric_number=(matric_number, matric_number))) |
---|
| 158 | if results: |
---|
| 159 | return results[0] |
---|
[6849] | 160 | return None |
---|
[6821] | 161 | |
---|
| 162 | def addEntry(self, obj, row, site): |
---|
| 163 | parent = self.getParent(row, site) |
---|
| 164 | parent.addStudent(obj) |
---|
[8491] | 165 | # Reset _curr_stud_id if student_id has been imported |
---|
| 166 | if self.getLocator(row) == 'student_id': |
---|
| 167 | parent._curr_stud_id -= 1 |
---|
[8287] | 168 | # We have to log this if state is provided. If not, |
---|
[7959] | 169 | # logging is done by the event handler handle_student_added |
---|
[9701] | 170 | if 'state' in row: |
---|
[7959] | 171 | parent.logger.info('%s - Student record created' % obj.student_id) |
---|
[6821] | 172 | return |
---|
| 173 | |
---|
| 174 | def delEntry(self, row, site): |
---|
[7267] | 175 | student = self.getEntry(row, site) |
---|
[7263] | 176 | if student is not None: |
---|
[6846] | 177 | parent = self.getParent(row, site) |
---|
[7656] | 178 | parent.logger.info('%s - Student removed' % student.student_id) |
---|
[6846] | 179 | del parent[student.student_id] |
---|
[6821] | 180 | pass |
---|
[6825] | 181 | |
---|
[8309] | 182 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 183 | """Checks requirements the object must fulfill when being updated. |
---|
| 184 | |
---|
| 185 | This method is not used in case of deleting or adding objects. |
---|
| 186 | |
---|
| 187 | Returns error messages as strings in case of requirement |
---|
| 188 | problems. |
---|
| 189 | """ |
---|
| 190 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 191 | if transition not in (IGNORE_MARKER, ''): |
---|
| 192 | allowed_transitions = IWorkflowInfo(obj).getManualTransitionIds() |
---|
| 193 | if transition not in allowed_transitions: |
---|
| 194 | return 'Transition not allowed.' |
---|
[9028] | 195 | if transition in FORBIDDEN_POSTGRAD_TRANS and \ |
---|
| 196 | obj.is_postgrad: |
---|
| 197 | return 'Transition not allowed (pg student).' |
---|
| 198 | state = row.get('state', IGNORE_MARKER) |
---|
| 199 | if state not in (IGNORE_MARKER, ''): |
---|
| 200 | if state in FORBIDDEN_POSTGRAD_STATES and \ |
---|
| 201 | obj.is_postgrad: |
---|
| 202 | return 'State not allowed (pg student).' |
---|
[8309] | 203 | return None |
---|
| 204 | |
---|
[9706] | 205 | def updateEntry(self, obj, row, site, filename): |
---|
[7497] | 206 | """Update obj to the values given in row. |
---|
| 207 | """ |
---|
[8221] | 208 | items_changed = '' |
---|
| 209 | |
---|
[7643] | 210 | # Remove student_id from row if empty |
---|
[9701] | 211 | if 'student_id' in row and row['student_id'] in (None, IGNORE_MARKER): |
---|
[7643] | 212 | row.pop('student_id') |
---|
[8221] | 213 | |
---|
| 214 | # Update password |
---|
[9701] | 215 | # XXX: Take DELETION_MARKER into consideration |
---|
| 216 | if 'password' in row: |
---|
[8498] | 217 | passwd = row.get('password', IGNORE_MARKER) |
---|
| 218 | if passwd not in ('', IGNORE_MARKER): |
---|
| 219 | if passwd.startswith('{SSHA}'): |
---|
| 220 | # already encrypted password |
---|
| 221 | obj.password = passwd |
---|
| 222 | else: |
---|
| 223 | # not yet encrypted password |
---|
| 224 | IUserAccount(obj).setPassword(passwd) |
---|
| 225 | items_changed += ('%s=%s, ' % ('password', passwd)) |
---|
[8221] | 226 | row.pop('password') |
---|
| 227 | |
---|
| 228 | # Update registration state |
---|
[9701] | 229 | if 'state' in row: |
---|
[8498] | 230 | state = row.get('state', IGNORE_MARKER) |
---|
| 231 | if state not in (IGNORE_MARKER, ''): |
---|
| 232 | value = row['state'] |
---|
| 233 | IWorkflowState(obj).setState(value) |
---|
| 234 | msg = _("State '${a}' set", mapping = {'a':value}) |
---|
| 235 | history = IObjectHistory(obj) |
---|
| 236 | history.addMessage(msg) |
---|
| 237 | items_changed += ('%s=%s, ' % ('state', state)) |
---|
[8287] | 238 | row.pop('state') |
---|
[8498] | 239 | |
---|
[9701] | 240 | if 'transition' in row: |
---|
[8498] | 241 | transition = row.get('transition', IGNORE_MARKER) |
---|
| 242 | if transition not in (IGNORE_MARKER, ''): |
---|
| 243 | value = row['transition'] |
---|
| 244 | IWorkflowInfo(obj).fireTransition(value) |
---|
| 245 | items_changed += ('%s=%s, ' % ('transition', transition)) |
---|
[8309] | 246 | row.pop('transition') |
---|
[8221] | 247 | |
---|
| 248 | # apply other values... |
---|
[8498] | 249 | items_changed += super(StudentProcessor, self).updateEntry( |
---|
[9706] | 250 | obj, row, site, filename) |
---|
[8221] | 251 | |
---|
| 252 | # Log actions... |
---|
[7656] | 253 | parent = self.getParent(row, site) |
---|
| 254 | if hasattr(obj,'student_id'): |
---|
[8995] | 255 | # Update mode: the student exists and we can get the student_id. |
---|
| 256 | # Create mode: the record contains the student_id |
---|
[7656] | 257 | parent.logger.info( |
---|
[9706] | 258 | '%s - %s - %s - updated: %s' |
---|
| 259 | % (self.name, filename, obj.student_id, items_changed)) |
---|
[7656] | 260 | else: |
---|
| 261 | # Create mode: the student does not yet exist |
---|
[9706] | 262 | # XXX: It seems that this never happens because student_id |
---|
| 263 | # is always set. |
---|
| 264 | parent.logger.info( |
---|
| 265 | '%s - %s - %s - imported: %s' |
---|
| 266 | % (self.name, filename, obj.student_id, items_changed)) |
---|
[15418] | 267 | notify(grok.ObjectModifiedEvent(obj)) |
---|
[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 | """ |
---|
[15163] | 529 | if obj.student.studycourse_locked: |
---|
| 530 | return 'Studycourse is locked.' |
---|
[9960] | 531 | certificate = getattr(obj, 'certificate', None) |
---|
| 532 | entry_session = getattr(obj, 'entry_session', None) |
---|
[9028] | 533 | current_level = row.get('current_level', None) |
---|
[9960] | 534 | entry_mode = row.get('entry_mode', None) |
---|
| 535 | # We have to ensure that the student can be transferred. |
---|
| 536 | if entry_mode == 'transfer': |
---|
| 537 | if certificate is None or entry_session is None: |
---|
| 538 | return 'Former study course record incomplete.' |
---|
| 539 | if 'studycourse_1' in obj.__parent__.keys() and \ |
---|
| 540 | 'studycourse_2' in obj.__parent__.keys(): |
---|
| 541 | return 'Maximum number of transfers exceeded.' |
---|
[9735] | 542 | if current_level: |
---|
| 543 | if current_level == 999 and \ |
---|
| 544 | obj.__parent__.state in FORBIDDEN_POSTGRAD_STATES: |
---|
| 545 | return 'Not a pg student.' |
---|
| 546 | cert = row.get('certificate', None) |
---|
| 547 | if certificate is None and cert is None: |
---|
| 548 | return 'No certificate to check level.' |
---|
[9960] | 549 | if certificate is not None and cert is None and ( |
---|
[9735] | 550 | current_level < certificate.start_level or \ |
---|
| 551 | current_level > certificate.end_level+120): |
---|
| 552 | return 'current_level not in range.' |
---|
[9028] | 553 | return None |
---|
| 554 | |
---|
[8232] | 555 | class StudentStudyLevelProcessor(StudentProcessorBase): |
---|
[12872] | 556 | """The Student Study Level Processor imports study level data. |
---|
| 557 | It overwrites the container attributes but not the content of the container, |
---|
| 558 | i.e. the course tickets stored inside the container. There is nothing |
---|
| 559 | special about this processor. |
---|
[7536] | 560 | """ |
---|
| 561 | grok.implements(IBatchProcessor) |
---|
| 562 | grok.provides(IBatchProcessor) |
---|
| 563 | grok.context(Interface) |
---|
[7933] | 564 | util_name = 'studylevelprocessor' |
---|
[7536] | 565 | grok.name(util_name) |
---|
| 566 | |
---|
[11891] | 567 | name = _('StudentStudyLevel Processor') |
---|
[7536] | 568 | iface = IStudentStudyLevel |
---|
| 569 | factory_name = 'waeup.StudentStudyLevel' |
---|
| 570 | |
---|
[9690] | 571 | @property |
---|
| 572 | def available_fields(self): |
---|
| 573 | fields = super(StudentStudyLevelProcessor, self).available_fields |
---|
| 574 | fields.remove('total_credits') |
---|
[10479] | 575 | fields.remove('gpa') |
---|
[9690] | 576 | return fields |
---|
| 577 | |
---|
[7536] | 578 | def getParent(self, row, site): |
---|
[8232] | 579 | student = self._getStudent(row, site) |
---|
| 580 | if student is None: |
---|
[7536] | 581 | return None |
---|
[8232] | 582 | return student['studycourse'] |
---|
[7536] | 583 | |
---|
| 584 | def getEntry(self, row, site): |
---|
| 585 | studycourse = self.getParent(row, site) |
---|
| 586 | if studycourse is None: |
---|
| 587 | return None |
---|
[14337] | 588 | try: |
---|
| 589 | entry = studycourse.get(str(row['level'])) |
---|
| 590 | except KeyError: |
---|
| 591 | return None |
---|
| 592 | return entry |
---|
[7536] | 593 | |
---|
[9301] | 594 | def delEntry(self, row, site): |
---|
| 595 | studylevel = self.getEntry(row, site) |
---|
| 596 | parent = self.getParent(row, site) |
---|
| 597 | if studylevel is not None: |
---|
| 598 | student = self._getStudent(row, site) |
---|
| 599 | student.__parent__.logger.info('%s - Level removed: %s' |
---|
| 600 | % (student.student_id, studylevel.__name__)) |
---|
| 601 | del parent[studylevel.__name__] |
---|
| 602 | return |
---|
| 603 | |
---|
[9706] | 604 | def updateEntry(self, obj, row, site, filename): |
---|
[8626] | 605 | """Update obj to the values given in row. |
---|
| 606 | """ |
---|
| 607 | items_changed = super(StudentStudyLevelProcessor, self).updateEntry( |
---|
[9706] | 608 | obj, row, site, filename) |
---|
[8626] | 609 | student = self.getParent(row, site).__parent__ |
---|
| 610 | student.__parent__.logger.info( |
---|
[9706] | 611 | '%s - %s - %s - updated: %s' |
---|
| 612 | % (self.name, filename, student.student_id, items_changed)) |
---|
[8626] | 613 | return |
---|
| 614 | |
---|
[7536] | 615 | def addEntry(self, obj, row, site): |
---|
[15065] | 616 | if IGNORE_MARKER == str(row['level']): |
---|
[15066] | 617 | raise FatalCSVError("level: Invalid value") |
---|
[7536] | 618 | parent = self.getParent(row, site) |
---|
[12873] | 619 | parent[str(row['level'])] = obj |
---|
[7536] | 620 | return |
---|
| 621 | |
---|
[15163] | 622 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 623 | """ |
---|
| 624 | """ |
---|
| 625 | if obj.student.studycourse_locked: |
---|
| 626 | return 'Studylevel is locked.' |
---|
| 627 | return None |
---|
| 628 | |
---|
[8232] | 629 | class CourseTicketProcessor(StudentProcessorBase): |
---|
[12882] | 630 | """The Course Ticket Processor imports course tickets, the subobjects |
---|
| 631 | of student study levels (= course lists). |
---|
| 632 | |
---|
| 633 | An imported course ticket contains a copy of the original course data. |
---|
| 634 | During import only a few attributes can be set/overwritten. |
---|
| 635 | |
---|
| 636 | Like all other student data importers, this processor also requires |
---|
| 637 | either `student_id`, `reg_number` or `matric_number` to find the student. |
---|
| 638 | Then it needs `level` and `code` to localize the course ticket. |
---|
| 639 | |
---|
| 640 | `checkConversion` first searches the courses catalog for the imported |
---|
| 641 | `code` and ensures that a course with such a code really exists |
---|
| 642 | in the academic section. It furthermore checks if `level_session` in |
---|
| 643 | the row corresponds with the session of the parent student |
---|
| 644 | study level object. It fails if one of the conditions is not met. |
---|
| 645 | |
---|
| 646 | In create mode `fcode`, `dcode`, `title`, `credits`, `passmark` and |
---|
| 647 | `semester` are taken from the course found in the academic section. |
---|
[14189] | 648 | `fcode` and `dcode` can nevermore be changed, neither via the user interface |
---|
[14654] | 649 | nor by import. Other values can be overwritten by import. |
---|
[15203] | 650 | |
---|
| 651 | `ticket_session` is an additional attribute which can be used to store the |
---|
| 652 | session of the course when it was taken. Usually this information is |
---|
[15446] | 653 | redundant because the parent study level object already contains this |
---|
| 654 | information, except for the study level zero container which can be used to |
---|
[15203] | 655 | store 'orphaned' course tickets. |
---|
[7548] | 656 | """ |
---|
| 657 | grok.implements(IBatchProcessor) |
---|
| 658 | grok.provides(IBatchProcessor) |
---|
| 659 | grok.context(Interface) |
---|
[7933] | 660 | util_name = 'courseticketprocessor' |
---|
[7548] | 661 | grok.name(util_name) |
---|
| 662 | |
---|
[11891] | 663 | name = _('CourseTicket Processor') |
---|
[9420] | 664 | iface = ICourseTicketImport |
---|
[7548] | 665 | factory_name = 'waeup.CourseTicket' |
---|
| 666 | |
---|
[9316] | 667 | additional_fields = ['level', 'code'] |
---|
[12869] | 668 | additional_fields_required = additional_fields |
---|
[7548] | 669 | |
---|
[9420] | 670 | @property |
---|
| 671 | def available_fields(self): |
---|
| 672 | fields = [ |
---|
| 673 | 'student_id','reg_number','matric_number', |
---|
| 674 | 'mandatory', 'score', 'carry_over', 'automatic', |
---|
[14642] | 675 | 'outstanding', 'course_category', 'level_session', |
---|
[15203] | 676 | 'title', 'credits', 'passmark', 'semester', 'ticket_session' |
---|
[9420] | 677 | ] + self.additional_fields |
---|
| 678 | return sorted(fields) |
---|
| 679 | |
---|
[7548] | 680 | def getParent(self, row, site): |
---|
[8232] | 681 | student = self._getStudent(row, site) |
---|
| 682 | if student is None: |
---|
[7548] | 683 | return None |
---|
[12873] | 684 | return student['studycourse'].get(str(row['level'])) |
---|
[7548] | 685 | |
---|
| 686 | def getEntry(self, row, site): |
---|
| 687 | level = self.getParent(row, site) |
---|
| 688 | if level is None: |
---|
| 689 | return None |
---|
| 690 | return level.get(row['code']) |
---|
| 691 | |
---|
[9706] | 692 | def updateEntry(self, obj, row, site, filename): |
---|
[8626] | 693 | """Update obj to the values given in row. |
---|
| 694 | """ |
---|
| 695 | items_changed = super(CourseTicketProcessor, self).updateEntry( |
---|
[9706] | 696 | obj, row, site, filename) |
---|
[8888] | 697 | parent = self.getParent(row, site) |
---|
[8626] | 698 | student = self.getParent(row, site).__parent__.__parent__ |
---|
| 699 | student.__parent__.logger.info( |
---|
[9706] | 700 | '%s - %s - %s - %s - updated: %s' |
---|
| 701 | % (self.name, filename, student.student_id, parent.level, items_changed)) |
---|
[8626] | 702 | return |
---|
| 703 | |
---|
[7548] | 704 | def addEntry(self, obj, row, site): |
---|
| 705 | parent = self.getParent(row, site) |
---|
| 706 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 707 | entries = list(catalog.searchResults(code=(row['code'],row['code']))) |
---|
| 708 | obj.fcode = entries[0].__parent__.__parent__.__parent__.code |
---|
| 709 | obj.dcode = entries[0].__parent__.__parent__.code |
---|
[14189] | 710 | if getattr(obj, 'title', None) is None: |
---|
| 711 | obj.title = entries[0].title |
---|
| 712 | if getattr(obj, 'credits', None) is None: |
---|
| 713 | obj.credits = entries[0].credits |
---|
| 714 | if getattr(obj, 'passmark', None) is None: |
---|
| 715 | obj.passmark = entries[0].passmark |
---|
| 716 | if getattr(obj, 'semester', None) is None: |
---|
| 717 | obj.semester = entries[0].semester |
---|
[7548] | 718 | parent[row['code']] = obj |
---|
| 719 | return |
---|
| 720 | |
---|
[8888] | 721 | def delEntry(self, row, site): |
---|
| 722 | ticket = self.getEntry(row, site) |
---|
| 723 | parent = self.getParent(row, site) |
---|
| 724 | if ticket is not None: |
---|
| 725 | student = self._getStudent(row, site) |
---|
| 726 | student.__parent__.logger.info('%s - Course ticket in %s removed: %s' |
---|
| 727 | % (student.student_id, parent.level, ticket.code)) |
---|
| 728 | del parent[ticket.code] |
---|
| 729 | return |
---|
| 730 | |
---|
[15163] | 731 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 732 | """ |
---|
| 733 | """ |
---|
| 734 | if obj.student.studycourse_locked: |
---|
| 735 | return 'Studycourse is locked.' |
---|
| 736 | return None |
---|
| 737 | |
---|
[7548] | 738 | def checkConversion(self, row, mode='ignore'): |
---|
| 739 | """Validates all values in row. |
---|
| 740 | """ |
---|
[8232] | 741 | errs, inv_errs, conv_dict = super( |
---|
| 742 | CourseTicketProcessor, self).checkConversion(row, mode=mode) |
---|
[9420] | 743 | if mode == 'remove': |
---|
| 744 | return errs, inv_errs, conv_dict |
---|
| 745 | # In update and create mode we have to check if course really exists. |
---|
[7548] | 746 | # This is not done by the converter. |
---|
| 747 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
| 748 | entries = catalog.searchResults(code=(row['code'],row['code'])) |
---|
| 749 | if len(entries) == 0: |
---|
| 750 | errs.append(('code','non-existent')) |
---|
| 751 | return errs, inv_errs, conv_dict |
---|
[9420] | 752 | # If level_session is provided in row we have to check if |
---|
| 753 | # the parent studylevel exists and if its level_session |
---|
| 754 | # attribute corresponds with the expected value in row. |
---|
[9421] | 755 | level_session = conv_dict.get('level_session', IGNORE_MARKER) |
---|
| 756 | if level_session not in (IGNORE_MARKER, None): |
---|
[9420] | 757 | site = grok.getSite() |
---|
| 758 | studylevel = self.getParent(row, site) |
---|
| 759 | if studylevel is not None: |
---|
| 760 | if studylevel.level_session != level_session: |
---|
| 761 | errs.append(('level_session','does not match %s' |
---|
| 762 | % studylevel.level_session)) |
---|
| 763 | else: |
---|
[13001] | 764 | errs.append(('level object','does not exist')) |
---|
[7623] | 765 | return errs, inv_errs, conv_dict |
---|
| 766 | |
---|
[8232] | 767 | class StudentOnlinePaymentProcessor(StudentProcessorBase): |
---|
[13871] | 768 | """The Student Online Payment Processor imports student payment tickets. |
---|
| 769 | The tickets are located in the ``payments`` subfolder of the student |
---|
[12882] | 770 | container. The only additional locator is `p_id`, the object id. |
---|
| 771 | |
---|
| 772 | The `checkConversion` method checks the format of the payment identifier. |
---|
[14804] | 773 | In create mode it does also ensures that same `p_id` does not exist |
---|
[12882] | 774 | elsewhere. It must be portal-wide unique. |
---|
| 775 | |
---|
| 776 | When adding a payment ticket, the `addEntry` method checks if the same |
---|
| 777 | payment has already been made. It compares `p_category` and `p_session` |
---|
| 778 | in the row with the corresponding attributes of existing payment |
---|
| 779 | tickets in state ``paid``. If they match, a `DuplicationError` is raised. |
---|
[7623] | 780 | """ |
---|
| 781 | grok.implements(IBatchProcessor) |
---|
| 782 | grok.provides(IBatchProcessor) |
---|
| 783 | grok.context(Interface) |
---|
[7933] | 784 | util_name = 'paymentprocessor' |
---|
[7623] | 785 | grok.name(util_name) |
---|
| 786 | |
---|
[11891] | 787 | name = _('StudentOnlinePayment Processor') |
---|
[8174] | 788 | iface = IStudentOnlinePayment |
---|
[7623] | 789 | factory_name = 'waeup.StudentOnlinePayment' |
---|
| 790 | |
---|
[8232] | 791 | additional_fields = ['p_id'] |
---|
[7623] | 792 | |
---|
[13871] | 793 | @property |
---|
| 794 | def available_fields(self): |
---|
| 795 | af = super( |
---|
| 796 | StudentOnlinePaymentProcessor, self).available_fields |
---|
| 797 | af.remove('display_item') |
---|
| 798 | return af |
---|
| 799 | |
---|
[8884] | 800 | def checkHeaders(self, headerfields, mode='ignore'): |
---|
| 801 | super(StudentOnlinePaymentProcessor, self).checkHeaders(headerfields) |
---|
[8885] | 802 | if mode in ('update', 'remove') and not 'p_id' in headerfields: |
---|
[8884] | 803 | raise FatalCSVError( |
---|
[8885] | 804 | "Need p_id for import in update and remove modes!") |
---|
[8884] | 805 | return True |
---|
| 806 | |
---|
[8232] | 807 | def parentsExist(self, row, site): |
---|
| 808 | return self.getParent(row, site) is not None |
---|
[7623] | 809 | |
---|
| 810 | def getParent(self, row, site): |
---|
[8232] | 811 | student = self._getStudent(row, site) |
---|
| 812 | if student is None: |
---|
[7623] | 813 | return None |
---|
[8232] | 814 | return student['payments'] |
---|
[7623] | 815 | |
---|
| 816 | def getEntry(self, row, site): |
---|
| 817 | payments = self.getParent(row, site) |
---|
| 818 | if payments is None: |
---|
| 819 | return None |
---|
[8884] | 820 | p_id = row.get('p_id', None) |
---|
[12996] | 821 | if p_id in (None, IGNORE_MARKER): |
---|
[8884] | 822 | return None |
---|
[7626] | 823 | # We can use the hash symbol at the end of p_id in import files |
---|
| 824 | # to avoid annoying automatic number transformation |
---|
| 825 | # by Excel or Calc |
---|
[8884] | 826 | p_id = p_id.strip('#') |
---|
[9467] | 827 | if len(p_id.split('-')) != 3 and not p_id.startswith('p'): |
---|
[8884] | 828 | # For data migration from old SRP only |
---|
| 829 | p_id = 'p' + p_id[7:] + '0' |
---|
| 830 | entry = payments.get(p_id) |
---|
[7623] | 831 | return entry |
---|
| 832 | |
---|
[9706] | 833 | def updateEntry(self, obj, row, site, filename): |
---|
[8626] | 834 | """Update obj to the values given in row. |
---|
| 835 | """ |
---|
| 836 | items_changed = super(StudentOnlinePaymentProcessor, self).updateEntry( |
---|
[9706] | 837 | obj, row, site, filename) |
---|
[8626] | 838 | student = self.getParent(row, site).__parent__ |
---|
| 839 | student.__parent__.logger.info( |
---|
[9706] | 840 | '%s - %s - %s - updated: %s' |
---|
| 841 | % (self.name, filename, student.student_id, items_changed)) |
---|
[8626] | 842 | return |
---|
| 843 | |
---|
[12623] | 844 | def samePaymentMade(self, student, category, p_session): |
---|
| 845 | for key in student['payments'].keys(): |
---|
| 846 | ticket = student['payments'][key] |
---|
| 847 | if ticket.p_state == 'paid' and\ |
---|
| 848 | ticket.p_category == category and \ |
---|
[14053] | 849 | ticket.p_session == p_session and \ |
---|
| 850 | ticket.p_item != 'Balance': |
---|
[12623] | 851 | return True |
---|
| 852 | return False |
---|
| 853 | |
---|
[7623] | 854 | def addEntry(self, obj, row, site): |
---|
| 855 | parent = self.getParent(row, site) |
---|
[12623] | 856 | student = parent.student |
---|
[7626] | 857 | p_id = row['p_id'].strip('#') |
---|
[12623] | 858 | # Requirement added on 19/02/2015: same payment must not exist. |
---|
[14213] | 859 | if obj.p_item != 'Balance' and self.samePaymentMade( |
---|
| 860 | student, obj.p_category, obj.p_session): |
---|
[13872] | 861 | student.__parent__.logger.info( |
---|
| 862 | '%s - %s - previous update cancelled' |
---|
| 863 | % (self.name, student.student_id)) |
---|
[12623] | 864 | raise DuplicationError('Same payment has already been made.') |
---|
[9467] | 865 | if len(p_id.split('-')) != 3 and not p_id.startswith('p'): |
---|
[7623] | 866 | # For data migration from old SRP |
---|
[8884] | 867 | obj.p_id = 'p' + p_id[7:] + '0' |
---|
[7623] | 868 | parent[obj.p_id] = obj |
---|
| 869 | else: |
---|
[7626] | 870 | parent[p_id] = obj |
---|
[7623] | 871 | return |
---|
| 872 | |
---|
[8885] | 873 | def delEntry(self, row, site): |
---|
| 874 | payment = self.getEntry(row, site) |
---|
| 875 | parent = self.getParent(row, site) |
---|
| 876 | if payment is not None: |
---|
| 877 | student = self._getStudent(row, site) |
---|
[8886] | 878 | student.__parent__.logger.info('%s - Payment ticket removed: %s' |
---|
[8885] | 879 | % (student.student_id, payment.p_id)) |
---|
| 880 | del parent[payment.p_id] |
---|
[8886] | 881 | return |
---|
[8885] | 882 | |
---|
[7623] | 883 | def checkConversion(self, row, mode='ignore'): |
---|
| 884 | """Validates all values in row. |
---|
| 885 | """ |
---|
[8232] | 886 | errs, inv_errs, conv_dict = super( |
---|
| 887 | StudentOnlinePaymentProcessor, self).checkConversion(row, mode=mode) |
---|
| 888 | |
---|
[7623] | 889 | # We have to check p_id. |
---|
[8884] | 890 | p_id = row.get('p_id', None) |
---|
[12996] | 891 | if mode == 'create' and p_id in (None, IGNORE_MARKER): |
---|
[8884] | 892 | timestamp = ("%d" % int(time()*10000))[1:] |
---|
| 893 | p_id = "p%s" % timestamp |
---|
| 894 | conv_dict['p_id'] = p_id |
---|
| 895 | return errs, inv_errs, conv_dict |
---|
[12996] | 896 | elif p_id in (None, IGNORE_MARKER): |
---|
| 897 | errs.append(('p_id','missing')) |
---|
| 898 | return errs, inv_errs, conv_dict |
---|
[8884] | 899 | else: |
---|
| 900 | p_id = p_id.strip('#') |
---|
[12996] | 901 | if p_id.startswith('p'): |
---|
| 902 | if not len(p_id) == 14: |
---|
| 903 | errs.append(('p_id','invalid length')) |
---|
| 904 | return errs, inv_errs, conv_dict |
---|
| 905 | elif len(p_id.split('-')) == 3: |
---|
| 906 | # The SRP used either pins as keys ... |
---|
| 907 | if len(p_id.split('-')[2]) not in (9, 10): |
---|
| 908 | errs.append(('p_id','invalid pin')) |
---|
| 909 | return errs, inv_errs, conv_dict |
---|
| 910 | else: |
---|
| 911 | # ... or order_ids. |
---|
| 912 | if not len(p_id) == 19: |
---|
| 913 | errs.append(('p_id','invalid format')) |
---|
| 914 | return errs, inv_errs, conv_dict |
---|
[12513] | 915 | # Requirement added on 24/01/2015: p_id must be portal-wide unique. |
---|
| 916 | if mode == 'create': |
---|
| 917 | cat = getUtility(ICatalog, name='payments_catalog') |
---|
| 918 | results = list(cat.searchResults(p_id=(p_id, p_id))) |
---|
| 919 | if len(results) > 0: |
---|
[13871] | 920 | sids = [IPayer(payment).id for payment in results] |
---|
[12513] | 921 | sids_string = '' |
---|
| 922 | for id in sids: |
---|
| 923 | sids_string += '%s ' % id |
---|
| 924 | errs.append(('p_id','p_id exists in %s' % sids_string)) |
---|
| 925 | return errs, inv_errs, conv_dict |
---|
[7623] | 926 | return errs, inv_errs, conv_dict |
---|
[7951] | 927 | |
---|
| 928 | class StudentVerdictProcessor(StudentStudyCourseProcessor): |
---|
[12882] | 929 | """The Student Verdict Processor inherits from the Student Study |
---|
| 930 | Course Processor. It's a pure updater. Import step 2 raises a warning |
---|
| 931 | message if a datacenter manager tries to select another mode. |
---|
| 932 | But it does more than only overwriting study course attributes. |
---|
[7951] | 933 | |
---|
[12882] | 934 | The Student Verdict Processor is the only processor which cannot be |
---|
| 935 | used for restoring data. Purpose is to announce a verdict at the end of |
---|
| 936 | each academic session. The processor does not only import a verdict, |
---|
| 937 | it also conditions the student data so that the student can pay for the |
---|
| 938 | next session and proceed to the next study level. |
---|
| 939 | |
---|
| 940 | The `checkUpdateRequirements` method ensures that the imported data |
---|
| 941 | really correspond to the actual state of the student. |
---|
| 942 | `current_level` and `current_session` in the row must be on par |
---|
| 943 | with the attributes of the study course object. Thus, the processor |
---|
| 944 | does not use these values to overwrite the attributes of the study course |
---|
| 945 | but to control that the verdict is really meant for the current session of |
---|
| 946 | the student. The verdict is only imported if a corresponding study level |
---|
| 947 | object exists and the student is in the right registration state, |
---|
| 948 | either ``courses validated`` or ``courses registered``. Course registration |
---|
| 949 | can be bypassed by setting `bypass_validation` to ``True``. |
---|
| 950 | |
---|
| 951 | The `updateEntry` method does not only update the current verdict of |
---|
| 952 | the student study course, it also updates the matching student study |
---|
| 953 | level object. It saves the current verdict as `level_verdict` and sets |
---|
| 954 | the `validated_by` and `validation_date` attributes, whereas `validated_by` |
---|
| 955 | is taken from the row of the import file and `validation_date` is set to the |
---|
| 956 | actual UTC datetime. Finally, the student is moved to state ``returning``. |
---|
[7951] | 957 | """ |
---|
| 958 | |
---|
| 959 | util_name = 'verdictupdater' |
---|
| 960 | grok.name(util_name) |
---|
| 961 | |
---|
[11891] | 962 | name = _('Verdict Processor (special processor, update only)') |
---|
[7951] | 963 | iface = IStudentVerdictUpdate |
---|
| 964 | factory_name = 'waeup.StudentStudyCourse' |
---|
| 965 | |
---|
[14901] | 966 | additional_fields_required = [ |
---|
| 967 | 'current_level', 'current_session', 'current_verdict'] |
---|
| 968 | |
---|
[7951] | 969 | def checkUpdateRequirements(self, obj, row, site): |
---|
| 970 | """Checks requirements the studycourse and the student must fulfill |
---|
| 971 | before being updated. |
---|
| 972 | """ |
---|
| 973 | # Check if current_levels correspond |
---|
| 974 | if obj.current_level != row['current_level']: |
---|
| 975 | return 'Current level does not correspond.' |
---|
| 976 | # Check if current_sessions correspond |
---|
| 977 | if obj.current_session != row['current_session']: |
---|
| 978 | return 'Current session does not correspond.' |
---|
[9282] | 979 | # Check if new verdict is provided |
---|
| 980 | if row['current_verdict'] in (IGNORE_MARKER, ''): |
---|
[9293] | 981 | return 'No verdict in import file.' |
---|
[9631] | 982 | # Check if studylevel exists |
---|
[9293] | 983 | level_string = str(obj.current_level) |
---|
| 984 | if obj.get(level_string) is None: |
---|
| 985 | return 'Study level object is missing.' |
---|
[9284] | 986 | # Check if student is in state REGISTERED or VALIDATED |
---|
[9296] | 987 | if row.get('bypass_validation'): |
---|
[9284] | 988 | if obj.student.state not in (VALIDATED, REGISTERED): |
---|
| 989 | return 'Student in wrong state.' |
---|
| 990 | else: |
---|
| 991 | if obj.student.state != VALIDATED: |
---|
| 992 | return 'Student in wrong state.' |
---|
[7951] | 993 | return None |
---|
| 994 | |
---|
[9706] | 995 | def updateEntry(self, obj, row, site, filename): |
---|
[7951] | 996 | """Update obj to the values given in row. |
---|
| 997 | """ |
---|
[8221] | 998 | # Don't set current_session, current_level |
---|
| 999 | vals_to_set = dict((key, val) for key, val in row.items() |
---|
| 1000 | if key not in ('current_session','current_level')) |
---|
[9706] | 1001 | super(StudentVerdictProcessor, self).updateEntry( |
---|
| 1002 | obj, vals_to_set, site, filename) |
---|
[7951] | 1003 | parent = self.getParent(row, site) |
---|
[12882] | 1004 | # Set current_verdict in corresponding studylevel |
---|
[9293] | 1005 | level_string = str(obj.current_level) |
---|
| 1006 | obj[level_string].level_verdict = row['current_verdict'] |
---|
[9296] | 1007 | # Fire transition and set studylevel attributes |
---|
| 1008 | # depending on student's state |
---|
[9284] | 1009 | if obj.__parent__.state == REGISTERED: |
---|
[9296] | 1010 | validated_by = row.get('validated_by', '') |
---|
| 1011 | if validated_by in (IGNORE_MARKER, ''): |
---|
| 1012 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 1013 | system = translate(_('System'),'waeup.kofa', |
---|
| 1014 | target_language=portal_language) |
---|
| 1015 | obj[level_string].validated_by = system |
---|
| 1016 | else: |
---|
| 1017 | obj[level_string].validated_by = validated_by |
---|
| 1018 | obj[level_string].validation_date = datetime.utcnow() |
---|
[9284] | 1019 | IWorkflowInfo(obj.__parent__).fireTransition('bypass_validation') |
---|
| 1020 | else: |
---|
| 1021 | IWorkflowInfo(obj.__parent__).fireTransition('return') |
---|
[7951] | 1022 | # Update the students_catalog |
---|
| 1023 | notify(grok.ObjectModifiedEvent(obj.__parent__)) |
---|
| 1024 | return |
---|