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