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

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

Set validated_by when bypassing course validation. Declare validated_by and bypass_validation in IStudentVerdictUpdate.

  • Property svn:keywords set to Id
File size: 29.5 KB
Line 
1## $Id: batching.py 9296 2012-10-05 05:42:19Z 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 updateEntry(self, obj, row, site):
491        """Update obj to the values given in row.
492        """
493        items_changed = super(StudentStudyLevelProcessor, self).updateEntry(
494            obj, row, site)
495        student = self.getParent(row, site).__parent__
496        student.__parent__.logger.info(
497            '%s - Study level updated: %s'
498            % (student.student_id, items_changed))
499        return
500
501    def addEntry(self, obj, row, site):
502        parent = self.getParent(row, site)
503        obj.level = int(row['level'])
504        parent[row['level']] = obj
505        return
506
507    def checkConversion(self, row, mode='ignore'):
508        """Validates all values in row.
509        """
510        errs, inv_errs, conv_dict = super(
511            StudentStudyLevelProcessor, self).checkConversion(row, mode=mode)
512
513        # We have to check if level is a valid integer.
514        # This is not done by the converter.
515        try:
516            level = int(row['level'])
517            if level not in range(0,700,10):
518                errs.append(('level','no valid integer'))
519        except ValueError:
520            errs.append(('level','no integer'))
521        return errs, inv_errs, conv_dict
522
523class CourseTicketProcessor(StudentProcessorBase):
524    """A batch processor for ICourseTicket objects.
525    """
526    grok.implements(IBatchProcessor)
527    grok.provides(IBatchProcessor)
528    grok.context(Interface)
529    util_name = 'courseticketprocessor'
530    grok.name(util_name)
531
532    name = u'CourseTicket Processor'
533    iface = ICourseTicket
534    factory_name = 'waeup.CourseTicket'
535
536    location_fields = []
537    additional_fields = ['level', 'code']
538    additional_headers = ['level', 'code']
539
540    def getParent(self, row, site):
541        student = self._getStudent(row, site)
542        if student is None:
543            return None
544        return student['studycourse'].get(row['level'])
545
546    def getEntry(self, row, site):
547        level = self.getParent(row, site)
548        if level is None:
549            return None
550        return level.get(row['code'])
551
552    def updateEntry(self, obj, row, site):
553        """Update obj to the values given in row.
554        """
555        items_changed = super(CourseTicketProcessor, self).updateEntry(
556            obj, row, site)
557        parent = self.getParent(row, site)
558        student = self.getParent(row, site).__parent__.__parent__
559        student.__parent__.logger.info(
560            '%s - Course ticket in %s updated: %s'
561            % (student.student_id,  parent.level, items_changed))
562        return
563
564    def addEntry(self, obj, row, site):
565        parent = self.getParent(row, site)
566        catalog = getUtility(ICatalog, name='courses_catalog')
567        entries = list(catalog.searchResults(code=(row['code'],row['code'])))
568        obj.fcode = entries[0].__parent__.__parent__.__parent__.code
569        obj.dcode = entries[0].__parent__.__parent__.code
570        obj.title = entries[0].title
571        obj.credits = entries[0].credits
572        obj.passmark = entries[0].passmark
573        obj.semester = entries[0].semester
574        parent[row['code']] = obj
575        return
576
577    def delEntry(self, row, site):
578        ticket = self.getEntry(row, site)
579        parent = self.getParent(row, site)
580        if ticket is not None:
581            student = self._getStudent(row, site)
582            student.__parent__.logger.info('%s - Course ticket in %s removed: %s'
583                % (student.student_id, parent.level, ticket.code))
584            del parent[ticket.code]
585        return
586
587    def checkConversion(self, row, mode='ignore'):
588        """Validates all values in row.
589        """
590        errs, inv_errs, conv_dict = super(
591            CourseTicketProcessor, self).checkConversion(row, mode=mode)
592
593        # We have to check if course really exists.
594        # This is not done by the converter.
595        catalog = getUtility(ICatalog, name='courses_catalog')
596        entries = catalog.searchResults(code=(row['code'],row['code']))
597        if len(entries) == 0:
598            errs.append(('code','non-existent'))
599            return errs, inv_errs, conv_dict
600        return errs, inv_errs, conv_dict
601
602class StudentOnlinePaymentProcessor(StudentProcessorBase):
603    """A batch processor for IStudentOnlinePayment objects.
604    """
605    grok.implements(IBatchProcessor)
606    grok.provides(IBatchProcessor)
607    grok.context(Interface)
608    util_name = 'paymentprocessor'
609    grok.name(util_name)
610
611    name = u'Student Payment Processor'
612    iface = IStudentOnlinePayment
613    factory_name = 'waeup.StudentOnlinePayment'
614
615    location_fields = []
616    additional_fields = ['p_id']
617    additional_headers = []
618
619    def checkHeaders(self, headerfields, mode='ignore'):
620        super(StudentOnlinePaymentProcessor, self).checkHeaders(headerfields)
621        if mode in ('update', 'remove') and not 'p_id' in headerfields:
622            raise FatalCSVError(
623                "Need p_id for import in update and remove modes!")
624        return True
625
626    def parentsExist(self, row, site):
627        return self.getParent(row, site) is not None
628
629    def getParent(self, row, site):
630        student = self._getStudent(row, site)
631        if student is None:
632            return None
633        return student['payments']
634
635    def getEntry(self, row, site):
636        payments = self.getParent(row, site)
637        if payments is None:
638            return None
639        p_id = row.get('p_id', None)
640        if p_id is None:
641            return None
642        # We can use the hash symbol at the end of p_id in import files
643        # to avoid annoying automatic number transformation
644        # by Excel or Calc
645        p_id = p_id.strip('#')
646        if not p_id.startswith('p'):
647            # For data migration from old SRP only
648            p_id = 'p' + p_id[7:] + '0'
649        entry = payments.get(p_id)
650        return entry
651
652    def updateEntry(self, obj, row, site):
653        """Update obj to the values given in row.
654        """
655        items_changed = super(StudentOnlinePaymentProcessor, self).updateEntry(
656            obj, row, site)
657        student = self.getParent(row, site).__parent__
658        student.__parent__.logger.info(
659            '%s - Payment ticket updated: %s'
660            % (student.student_id, items_changed))
661        return
662
663    def addEntry(self, obj, row, site):
664        parent = self.getParent(row, site)
665        p_id = row['p_id'].strip('#')
666        if not p_id.startswith('p'):
667            # For data migration from old SRP
668            obj.p_id = 'p' + p_id[7:] + '0'
669            parent[obj.p_id] = obj
670        else:
671            parent[p_id] = obj
672        return
673
674    def delEntry(self, row, site):
675        payment = self.getEntry(row, site)
676        parent = self.getParent(row, site)
677        if payment is not None:
678            student = self._getStudent(row, site)
679            student.__parent__.logger.info('%s - Payment ticket removed: %s'
680                % (student.student_id, payment.p_id))
681            del parent[payment.p_id]
682        return
683
684    def checkConversion(self, row, mode='ignore'):
685        """Validates all values in row.
686        """
687        errs, inv_errs, conv_dict = super(
688            StudentOnlinePaymentProcessor, self).checkConversion(row, mode=mode)
689
690        # We have to check p_id.
691        p_id = row.get('p_id', None)
692        if not p_id:
693            timestamp = ("%d" % int(time()*10000))[1:]
694            p_id = "p%s" % timestamp
695            conv_dict['p_id'] = p_id
696            return errs, inv_errs, conv_dict
697        else:
698            p_id = p_id.strip('#')
699        if p_id.startswith('p'):
700            if not len(p_id) == 14:
701                errs.append(('p_id','invalid length'))
702                return errs, inv_errs, conv_dict
703        else:
704            if not len(p_id) == 19:
705                errs.append(('p_id','invalid length'))
706                return errs, inv_errs, conv_dict
707        return errs, inv_errs, conv_dict
708
709class StudentVerdictProcessor(StudentStudyCourseProcessor):
710    """A batch processor for verdicts.
711
712    Import verdicts and perform workflow transitions.
713    """
714
715    util_name = 'verdictupdater'
716    grok.name(util_name)
717
718    name = u'Verdict Processor (update only)'
719    iface = IStudentVerdictUpdate
720    factory_name = 'waeup.StudentStudyCourse'
721
722    additional_fields = [
723        'current_session',
724        'current_level',
725        'bypass_validation',
726        'validated_by']
727
728    def checkUpdateRequirements(self, obj, row, site):
729        """Checks requirements the studycourse and the student must fulfill
730        before being updated.
731        """
732        # Check if current_levels correspond
733        if obj.current_level != row['current_level']:
734            return 'Current level does not correspond.'
735        # Check if current_sessions correspond
736        if obj.current_session != row['current_session']:
737            return 'Current session does not correspond.'
738        # Check if new verdict is provided
739        if row['current_verdict'] in (IGNORE_MARKER, ''):
740            return 'No verdict in import file.'
741        # Check if studylevel exists#
742        level_string = str(obj.current_level)
743        if obj.get(level_string) is None:
744            return 'Study level object is missing.'
745        # Check if student is in state REGISTERED or VALIDATED
746        if row.get('bypass_validation'):
747            if obj.student.state not in (VALIDATED, REGISTERED):
748                return 'Student in wrong state.'
749        else:
750            if obj.student.state != VALIDATED:
751                return 'Student in wrong state.'
752        return None
753
754    def updateEntry(self, obj, row, site):
755        """Update obj to the values given in row.
756        """
757        # Don't set current_session, current_level
758        vals_to_set = dict((key, val) for key, val in row.items()
759                           if key not in ('current_session','current_level'))
760        super(StudentVerdictProcessor, self).updateEntry(obj, vals_to_set, site)
761        parent = self.getParent(row, site)
762        # Set current_vedict in corresponding studylevel
763        level_string = str(obj.current_level)
764        obj[level_string].level_verdict = row['current_verdict']
765        # Fire transition and set studylevel attributes
766        # depending on student's state
767        if obj.__parent__.state == REGISTERED:
768            validated_by = row.get('validated_by', '')
769            if validated_by in (IGNORE_MARKER, ''):
770                portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
771                system = translate(_('System'),'waeup.kofa',
772                                  target_language=portal_language)
773                obj[level_string].validated_by = system
774            else:
775                obj[level_string].validated_by = validated_by
776            obj[level_string].validation_date = datetime.utcnow()
777            IWorkflowInfo(obj.__parent__).fireTransition('bypass_validation')
778        else:
779            IWorkflowInfo(obj.__parent__).fireTransition('return')
780        # Update the students_catalog
781        notify(grok.ObjectModifiedEvent(obj.__parent__))
782        return
Note: See TracBrowser for help on using the repository browser.