source: main/waeup.kofa/trunk/src/waeup/kofa/students/batching.py @ 9341

Last change on this file since 9341 was 9316, checked in by Henrik Bettermann, 12 years ago

We need to import credits and passmark of course tickets in order to repair them. Let's use the form field validation for import.

Both passmark and credits must not be edited via the UI.

  • Property svn:keywords set to Id
File size: 30.0 KB
Line 
1## $Id: batching.py 9316 2012-10-08 13:19:14Z 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
20Batch processors eat CSV files to add, update or remove large numbers
21of certain kinds of objects at once.
22
23Here we define the processors for students specific objects like
24students, studycourses, payment tickets and accommodation tickets.
25"""
26import grok
27import csv
28from time import time
29from datetime import datetime
30from zope.i18n import translate
31from zope.interface import Interface
32from zope.schema import getFields
33from zope.component import queryUtility, getUtility
34from zope.event import notify
35from zope.catalog.interfaces import ICatalog
36from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo
37from waeup.kofa.interfaces import (
38    IBatchProcessor, FatalCSVError, IObjectConverter, IUserAccount,
39    IObjectHistory, VALIDATED, REGISTERED, IGNORE_MARKER)
40from waeup.kofa.interfaces import IKofaUtils
41from waeup.kofa.interfaces import MessageFactory as _
42from waeup.kofa.students.interfaces import (
43    IStudent, IStudentStudyCourse,
44    IStudentUpdateByRegNo, IStudentUpdateByMatricNo,
45    IStudentStudyLevel, ICourseTicket,
46    IStudentOnlinePayment, IStudentVerdictUpdate)
47from waeup.kofa.students.workflow import  (
48    IMPORTABLE_STATES, IMPORTABLE_TRANSITIONS,
49    FORBIDDEN_POSTGRAD_TRANS, FORBIDDEN_POSTGRAD_STATES)
50from waeup.kofa.utils.batching import BatchProcessor
51
52class 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 row.has_key('state'):
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):
188        """Update obj to the values given in row.
189        """
190        items_changed = ''
191
192        # Remove student_id from row if empty
193        if row.has_key('student_id') and row['student_id'] in (
194            None, IGNORE_MARKER):
195            row.pop('student_id')
196
197        # Update password
198        # XXX: Tale DELETION_MARKER into consideration
199        if row.has_key('password'):
200            passwd = row.get('password', IGNORE_MARKER)
201            if passwd not in ('', IGNORE_MARKER):
202                if passwd.startswith('{SSHA}'):
203                    # already encrypted password
204                    obj.password = passwd
205                else:
206                    # not yet encrypted password
207                    IUserAccount(obj).setPassword(passwd)
208                items_changed += ('%s=%s, ' % ('password', passwd))
209            row.pop('password')
210
211        # Update registration state
212        if row.has_key('state'):
213            state = row.get('state', IGNORE_MARKER)
214            if state not in (IGNORE_MARKER, ''):
215                value = row['state']
216                IWorkflowState(obj).setState(value)
217                msg = _("State '${a}' set", mapping = {'a':value})
218                history = IObjectHistory(obj)
219                history.addMessage(msg)
220                items_changed += ('%s=%s, ' % ('state', state))
221            row.pop('state')
222
223        if row.has_key('transition'):
224            transition = row.get('transition', IGNORE_MARKER)
225            if transition not in (IGNORE_MARKER, ''):
226                value = row['transition']
227                IWorkflowInfo(obj).fireTransition(value)
228                items_changed += ('%s=%s, ' % ('transition', transition))
229            row.pop('transition')
230
231        # apply other values...
232        items_changed += super(StudentProcessor, self).updateEntry(
233            obj, row, site)
234
235        # Log actions...
236        parent = self.getParent(row, site)
237        if hasattr(obj,'student_id'):
238            # Update mode: the student exists and we can get the student_id.
239            # Create mode: the record contains the student_id
240            parent.logger.info(
241                '%s - Student record updated: %s'
242                % (obj.student_id, items_changed))
243        else:
244            # Create mode: the student does not yet exist
245            parent.logger.info('Student data imported: %s' % items_changed)
246        return items_changed
247
248    def getMapping(self, path, headerfields, mode):
249        """Get a mapping from CSV file headerfields to actually used fieldnames.
250        """
251        result = dict()
252        reader = csv.reader(open(path, 'rb'))
253        raw_header = reader.next()
254        for num, field in enumerate(headerfields):
255            if field not in ['student_id', 'reg_number', 'matric_number'
256                             ] and mode == 'remove':
257                continue
258            if field == u'--IGNORE--':
259                # Skip ignored columns in failed and finished data files.
260                continue
261            result[raw_header[num]] = field
262        return result
263
264    def checkConversion(self, row, mode='create'):
265        """Validates all values in row.
266        """
267        iface = self.iface
268        if mode in ['update', 'remove']:
269            if self.getLocator(row) == 'reg_number':
270                iface = self.iface_byregnumber
271            elif self.getLocator(row) == 'matric_number':
272                iface = self.iface_bymatricnumber
273        converter = IObjectConverter(iface)
274        errs, inv_errs, conv_dict =  converter.fromStringDict(
275            row, self.factory_name, mode=mode)
276        if row.has_key('transition'):
277            if row['transition'] not in IMPORTABLE_TRANSITIONS:
278                if row['transition'] not in (IGNORE_MARKER, ''):
279                    errs.append(('transition','not allowed'))
280        if row.has_key('state'):
281            if row['state'] not in IMPORTABLE_STATES:
282                if row['state'] not in (IGNORE_MARKER, ''):
283                    errs.append(('state','not allowed'))
284                else:
285                    # State is an attribute of Student and must not
286                    # be changed if empty.
287                    conv_dict['state'] = IGNORE_MARKER
288        try:
289            # Correct stud_id counter. As the IConverter for students
290            # creates student objects that are not used afterwards, we
291            # have to fix the site-wide student_id counter.
292            site = grok.getSite()
293            students = site['students']
294            students._curr_stud_id -= 1
295        except (KeyError, TypeError, AttributeError):
296                pass
297        return errs, inv_errs, conv_dict
298
299
300class StudentProcessorBase(BatchProcessor):
301    """A base for student subitem processor.
302
303    Helps reducing redundancy.
304    """
305    grok.baseclass()
306
307    # additional available  fields
308    # beside 'student_id', 'reg_number' and 'matric_number'
309    additional_fields = []
310
311    #: header fields additionally required
312    additional_headers = []
313
314    @property
315    def available_fields(self):
316        fields = ['student_id','reg_number','matric_number'
317                  ] + self.additional_fields
318        return sorted(list(set(fields + getFields(
319                self.iface).keys())))
320
321    def checkHeaders(self, headerfields, mode='ignore'):
322        if not 'reg_number' in headerfields and not 'student_id' \
323            in headerfields and not 'matric_number' in headerfields:
324            raise FatalCSVError(
325                "Need at least columns student_id " +
326                "or reg_number or matric_number for import!")
327        for name in self.additional_headers:
328            if not name in headerfields:
329                raise FatalCSVError(
330                    "Need %s for import!" % name)
331
332        # Check for fields to be ignored...
333        not_ignored_fields = [x for x in headerfields
334                              if not x.startswith('--')]
335        if len(set(not_ignored_fields)) < len(not_ignored_fields):
336            raise FatalCSVError(
337                "Double headers: each column name may only appear once.")
338        return True
339
340    def _getStudent(self, row, site):
341        NON_VALUES = ['', IGNORE_MARKER]
342        if not 'students' in site.keys():
343            return None
344        if row.get('student_id', '') not in NON_VALUES:
345            if row['student_id'] in site['students']:
346                student = site['students'][row['student_id']]
347                return student
348        elif row.get('reg_number', '') not in NON_VALUES:
349            reg_number = row['reg_number']
350            cat = queryUtility(ICatalog, name='students_catalog')
351            results = list(
352                cat.searchResults(reg_number=(reg_number, reg_number)))
353            if results:
354                return results[0]
355        elif row.get('matric_number', '') not in NON_VALUES:
356            matric_number = row['matric_number']
357            cat = queryUtility(ICatalog, name='students_catalog')
358            results = list(
359                cat.searchResults(matric_number=(matric_number, matric_number)))
360            if results:
361                return results[0]
362        return None
363
364    def parentsExist(self, row, site):
365        return self.getParent(row, site) is not None
366
367    def entryExists(self, row, site):
368        return self.getEntry(row, site) is not None
369
370    def checkConversion(self, row, mode='ignore'):
371        """Validates all values in row.
372        """
373        converter = IObjectConverter(self.iface)
374        errs, inv_errs, conv_dict =  converter.fromStringDict(
375            row, self.factory_name, mode=mode)
376        return errs, inv_errs, conv_dict
377
378    def getMapping(self, path, headerfields, mode):
379        """Get a mapping from CSV file headerfields to actually used fieldnames.
380        """
381        result = dict()
382        reader = csv.reader(open(path, 'rb'))
383        raw_header = reader.next()
384        for num, field in enumerate(headerfields):
385            if field not in ['student_id', 'reg_number', 'matric_number',
386                             'p_id', 'code', 'level'
387                             ] and mode == 'remove':
388                continue
389            if field == u'--IGNORE--':
390                # Skip ignored columns in failed and finished data files.
391                continue
392            result[raw_header[num]] = field
393        return result
394
395
396class StudentStudyCourseProcessor(StudentProcessorBase):
397    """A batch processor for IStudentStudyCourse objects.
398    """
399    grok.implements(IBatchProcessor)
400    grok.provides(IBatchProcessor)
401    grok.context(Interface)
402    util_name = 'studycourseupdater'
403    grok.name(util_name)
404
405    name = u'StudentStudyCourse Processor (update only)'
406    iface = IStudentStudyCourse
407    factory_name = 'waeup.StudentStudyCourse'
408
409    location_fields = []
410    additional_fields = []
411
412    def getParent(self, row, site):
413        return self._getStudent(row, site)
414
415    def getEntry(self, row, site):
416        student = self.getParent(row, site)
417        if student is None:
418            return None
419        return student.get('studycourse')
420
421    def updateEntry(self, obj, row, site):
422        """Update obj to the values given in row.
423        """
424        items_changed = super(StudentStudyCourseProcessor, self).updateEntry(
425            obj, row, site)
426        parent = self.getParent(row, site)
427        parent.__parent__.logger.info(
428            '%s - Study course updated: %s'
429            % (parent.student_id, items_changed))
430        # Update the students_catalog
431        notify(grok.ObjectModifiedEvent(obj.__parent__))
432        return
433
434    def checkConversion(self, row, mode='ignore'):
435        """Validates all values in row.
436        """
437        errs, inv_errs, conv_dict = super(
438            StudentStudyCourseProcessor, self).checkConversion(row, mode=mode)
439        # We have to check if current_level is in range of certificate.
440        if conv_dict.has_key('certificate') and \
441            conv_dict.has_key('current_level'):
442            cert = conv_dict['certificate']
443            level = conv_dict['current_level']
444            if level < cert.start_level or level > cert.end_level+120:
445                errs.append(('current_level','not in range'))
446        return errs, inv_errs, conv_dict
447
448    def checkUpdateRequirements(self, obj, row, site):
449        """Checks requirements the object must fulfill when being updated.
450
451        Returns error messages as strings in case of requirement
452        problems.
453        """
454        current_level = row.get('current_level', None)
455        if current_level == 999 and \
456            obj.__parent__.state in FORBIDDEN_POSTGRAD_STATES:
457            return 'Not a pg student.'
458        return None
459
460class StudentStudyLevelProcessor(StudentProcessorBase):
461    """A batch processor for IStudentStudyLevel objects.
462    """
463    grok.implements(IBatchProcessor)
464    grok.provides(IBatchProcessor)
465    grok.context(Interface)
466    util_name = 'studylevelprocessor'
467    grok.name(util_name)
468
469    name = u'StudentStudyLevel Processor'
470    iface = IStudentStudyLevel
471    factory_name = 'waeup.StudentStudyLevel'
472
473    location_fields = []
474
475    additional_fields = ['level']
476    additional_headers = ['level']
477
478    def getParent(self, row, site):
479        student = self._getStudent(row, site)
480        if student is None:
481            return None
482        return student['studycourse']
483
484    def getEntry(self, row, site):
485        studycourse = self.getParent(row, site)
486        if studycourse is None:
487            return None
488        return studycourse.get(row['level'])
489
490    def delEntry(self, row, site):
491        studylevel = self.getEntry(row, site)
492        parent = self.getParent(row, site)
493        if studylevel is not None:
494            student = self._getStudent(row, site)
495            student.__parent__.logger.info('%s - Level removed: %s'
496                % (student.student_id, studylevel.__name__))
497            del parent[studylevel.__name__]
498        return
499
500    def updateEntry(self, obj, row, site):
501        """Update obj to the values given in row.
502        """
503        items_changed = super(StudentStudyLevelProcessor, self).updateEntry(
504            obj, row, site)
505        student = self.getParent(row, site).__parent__
506        student.__parent__.logger.info(
507            '%s - Study level updated: %s'
508            % (student.student_id, items_changed))
509        return
510
511    def addEntry(self, obj, row, site):
512        parent = self.getParent(row, site)
513        obj.level = int(row['level'])
514        parent[row['level']] = obj
515        return
516
517    def checkConversion(self, row, mode='ignore'):
518        """Validates all values in row.
519        """
520        errs, inv_errs, conv_dict = super(
521            StudentStudyLevelProcessor, self).checkConversion(row, mode=mode)
522
523        # We have to check if level is a valid integer.
524        # This is not done by the converter.
525        try:
526            level = int(row['level'])
527            if level not in range(0,1000,10) + [999]:
528                errs.append(('level','no valid integer'))
529        except ValueError:
530            errs.append(('level','no integer'))
531        return errs, inv_errs, conv_dict
532
533class CourseTicketProcessor(StudentProcessorBase):
534    """A batch processor for ICourseTicket objects.
535    """
536    grok.implements(IBatchProcessor)
537    grok.provides(IBatchProcessor)
538    grok.context(Interface)
539    util_name = 'courseticketprocessor'
540    grok.name(util_name)
541
542    name = u'CourseTicket Processor'
543    iface = ICourseTicket
544    factory_name = 'waeup.CourseTicket'
545
546    location_fields = []
547    additional_fields = ['level', 'code']
548    additional_headers = ['level', 'code']
549
550    def getParent(self, row, site):
551        student = self._getStudent(row, site)
552        if student is None:
553            return None
554        return student['studycourse'].get(row['level'])
555
556    def getEntry(self, row, site):
557        level = self.getParent(row, site)
558        if level is None:
559            return None
560        return level.get(row['code'])
561
562    def updateEntry(self, obj, row, site):
563        """Update obj to the values given in row.
564        """
565        items_changed = super(CourseTicketProcessor, self).updateEntry(
566            obj, row, site)
567        parent = self.getParent(row, site)
568        student = self.getParent(row, site).__parent__.__parent__
569        student.__parent__.logger.info(
570            '%s - Course ticket in %s updated: %s'
571            % (student.student_id,  parent.level, items_changed))
572        return
573
574    def addEntry(self, obj, row, site):
575        parent = self.getParent(row, site)
576        catalog = getUtility(ICatalog, name='courses_catalog')
577        entries = list(catalog.searchResults(code=(row['code'],row['code'])))
578        obj.fcode = entries[0].__parent__.__parent__.__parent__.code
579        obj.dcode = entries[0].__parent__.__parent__.code
580        obj.title = entries[0].title
581        if getattr(obj, 'credits', None) is None:
582            obj.credits = entries[0].credits
583        if getattr(obj, 'passmark', None) is None:
584            obj.passmark = entries[0].passmark
585        obj.semester = entries[0].semester
586        parent[row['code']] = obj
587        return
588
589    def delEntry(self, row, site):
590        ticket = self.getEntry(row, site)
591        parent = self.getParent(row, site)
592        if ticket is not None:
593            student = self._getStudent(row, site)
594            student.__parent__.logger.info('%s - Course ticket in %s removed: %s'
595                % (student.student_id, parent.level, ticket.code))
596            del parent[ticket.code]
597        return
598
599    def checkConversion(self, row, mode='ignore'):
600        """Validates all values in row.
601        """
602        errs, inv_errs, conv_dict = super(
603            CourseTicketProcessor, self).checkConversion(row, mode=mode)
604
605        # We have to check if course really exists.
606        # This is not done by the converter.
607        catalog = getUtility(ICatalog, name='courses_catalog')
608        entries = catalog.searchResults(code=(row['code'],row['code']))
609        if len(entries) == 0:
610            errs.append(('code','non-existent'))
611            return errs, inv_errs, conv_dict
612        return errs, inv_errs, conv_dict
613
614class StudentOnlinePaymentProcessor(StudentProcessorBase):
615    """A batch processor for IStudentOnlinePayment objects.
616    """
617    grok.implements(IBatchProcessor)
618    grok.provides(IBatchProcessor)
619    grok.context(Interface)
620    util_name = 'paymentprocessor'
621    grok.name(util_name)
622
623    name = u'Student Payment Processor'
624    iface = IStudentOnlinePayment
625    factory_name = 'waeup.StudentOnlinePayment'
626
627    location_fields = []
628    additional_fields = ['p_id']
629    additional_headers = []
630
631    def checkHeaders(self, headerfields, mode='ignore'):
632        super(StudentOnlinePaymentProcessor, self).checkHeaders(headerfields)
633        if mode in ('update', 'remove') and not 'p_id' in headerfields:
634            raise FatalCSVError(
635                "Need p_id for import in update and remove modes!")
636        return True
637
638    def parentsExist(self, row, site):
639        return self.getParent(row, site) is not None
640
641    def getParent(self, row, site):
642        student = self._getStudent(row, site)
643        if student is None:
644            return None
645        return student['payments']
646
647    def getEntry(self, row, site):
648        payments = self.getParent(row, site)
649        if payments is None:
650            return None
651        p_id = row.get('p_id', None)
652        if p_id is None:
653            return None
654        # We can use the hash symbol at the end of p_id in import files
655        # to avoid annoying automatic number transformation
656        # by Excel or Calc
657        p_id = p_id.strip('#')
658        if not p_id.startswith('p'):
659            # For data migration from old SRP only
660            p_id = 'p' + p_id[7:] + '0'
661        entry = payments.get(p_id)
662        return entry
663
664    def updateEntry(self, obj, row, site):
665        """Update obj to the values given in row.
666        """
667        items_changed = super(StudentOnlinePaymentProcessor, self).updateEntry(
668            obj, row, site)
669        student = self.getParent(row, site).__parent__
670        student.__parent__.logger.info(
671            '%s - Payment ticket updated: %s'
672            % (student.student_id, items_changed))
673        return
674
675    def addEntry(self, obj, row, site):
676        parent = self.getParent(row, site)
677        p_id = row['p_id'].strip('#')
678        if not p_id.startswith('p'):
679            # For data migration from old SRP
680            obj.p_id = 'p' + p_id[7:] + '0'
681            parent[obj.p_id] = obj
682        else:
683            parent[p_id] = obj
684        return
685
686    def delEntry(self, row, site):
687        payment = self.getEntry(row, site)
688        parent = self.getParent(row, site)
689        if payment is not None:
690            student = self._getStudent(row, site)
691            student.__parent__.logger.info('%s - Payment ticket removed: %s'
692                % (student.student_id, payment.p_id))
693            del parent[payment.p_id]
694        return
695
696    def checkConversion(self, row, mode='ignore'):
697        """Validates all values in row.
698        """
699        errs, inv_errs, conv_dict = super(
700            StudentOnlinePaymentProcessor, self).checkConversion(row, mode=mode)
701
702        # We have to check p_id.
703        p_id = row.get('p_id', None)
704        if not p_id:
705            timestamp = ("%d" % int(time()*10000))[1:]
706            p_id = "p%s" % timestamp
707            conv_dict['p_id'] = p_id
708            return errs, inv_errs, conv_dict
709        else:
710            p_id = p_id.strip('#')
711        if p_id.startswith('p'):
712            if not len(p_id) == 14:
713                errs.append(('p_id','invalid length'))
714                return errs, inv_errs, conv_dict
715        else:
716            if not len(p_id) == 19:
717                errs.append(('p_id','invalid length'))
718                return errs, inv_errs, conv_dict
719        return errs, inv_errs, conv_dict
720
721class StudentVerdictProcessor(StudentStudyCourseProcessor):
722    """A batch processor for verdicts.
723
724    Import verdicts and perform workflow transitions.
725    """
726
727    util_name = 'verdictupdater'
728    grok.name(util_name)
729
730    name = u'Verdict Processor (update only)'
731    iface = IStudentVerdictUpdate
732    factory_name = 'waeup.StudentStudyCourse'
733
734    additional_fields = [
735        'current_session',
736        'current_level',
737        'bypass_validation',
738        'validated_by']
739
740    def checkUpdateRequirements(self, obj, row, site):
741        """Checks requirements the studycourse and the student must fulfill
742        before being updated.
743        """
744        # Check if current_levels correspond
745        if obj.current_level != row['current_level']:
746            return 'Current level does not correspond.'
747        # Check if current_sessions correspond
748        if obj.current_session != row['current_session']:
749            return 'Current session does not correspond.'
750        # Check if new verdict is provided
751        if row['current_verdict'] in (IGNORE_MARKER, ''):
752            return 'No verdict in import file.'
753        # Check if studylevel exists#
754        level_string = str(obj.current_level)
755        if obj.get(level_string) is None:
756            return 'Study level object is missing.'
757        # Check if student is in state REGISTERED or VALIDATED
758        if row.get('bypass_validation'):
759            if obj.student.state not in (VALIDATED, REGISTERED):
760                return 'Student in wrong state.'
761        else:
762            if obj.student.state != VALIDATED:
763                return 'Student in wrong state.'
764        return None
765
766    def updateEntry(self, obj, row, site):
767        """Update obj to the values given in row.
768        """
769        # Don't set current_session, current_level
770        vals_to_set = dict((key, val) for key, val in row.items()
771                           if key not in ('current_session','current_level'))
772        super(StudentVerdictProcessor, self).updateEntry(obj, vals_to_set, site)
773        parent = self.getParent(row, site)
774        # Set current_vedict in corresponding studylevel
775        level_string = str(obj.current_level)
776        obj[level_string].level_verdict = row['current_verdict']
777        # Fire transition and set studylevel attributes
778        # depending on student's state
779        if obj.__parent__.state == REGISTERED:
780            validated_by = row.get('validated_by', '')
781            if validated_by in (IGNORE_MARKER, ''):
782                portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
783                system = translate(_('System'),'waeup.kofa',
784                                  target_language=portal_language)
785                obj[level_string].validated_by = system
786            else:
787                obj[level_string].validated_by = validated_by
788            obj[level_string].validation_date = datetime.utcnow()
789            IWorkflowInfo(obj.__parent__).fireTransition('bypass_validation')
790        else:
791            IWorkflowInfo(obj.__parent__).fireTransition('return')
792        # Update the students_catalog
793        notify(grok.ObjectModifiedEvent(obj.__parent__))
794        return
Note: See TracBrowser for help on using the repository browser.