source: main/waeup.kofa/trunk/src/waeup/kofa/students/student.py @ 17772

Last change on this file since 17772 was 17769, checked in by Henrik Bettermann, 6 months ago

After import with entry_mode 'transfer' we must ensure that after export and reimport the student is not transferred again.

  • Property svn:keywords set to Id
File size: 23.1 KB
RevLine 
[7191]1## $Id: student.py 17769 2024-05-13 06:26:57Z henrik $
2##
[6621]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"""
19Container for the various objects owned by students.
20"""
[7097]21import os
[8448]22import re
[8403]23import shutil
[6621]24import grok
[9334]25from datetime import datetime, timedelta
[7949]26from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo
[9334]27from zope.password.interfaces import IPasswordManager
[8323]28from zope.component import getUtility, createObject
[6749]29from zope.component.interfaces import IFactory
[6621]30from zope.interface import implementedBy
[6838]31from zope.securitypolicy.interfaces import IPrincipalRoleManager
[9137]32from zope.schema.interfaces import ConstraintNotSatisfied
[15606]33from zope.event import notify
[8403]34
[15606]35from waeup.kofa.authentication import LocalRoleSetEvent
[7949]36from waeup.kofa.image import KofaImageFile
37from waeup.kofa.imagestorage import DefaultFileStoreHandler
[7811]38from waeup.kofa.interfaces import (
[7359]39    IObjectHistory, IUserAccount, IFileStoreNameChooser, IFileStoreHandler,
[9517]40    IKofaUtils, registration_states_vocab, IExtFileStore,
[14169]41    CREATED, ADMITTED, CLEARANCE, PAID, REGISTERED, VALIDATED, RETURNING,
[15163]42    GRADUATED, TRANSVAL, TRANSREL)
[7949]43from waeup.kofa.students.accommodation import StudentAccommodation
[8411]44from waeup.kofa.students.interfaces import (
[12104]45    IStudent, IStudentNavigation, IStudentPersonalEdit, ICSVStudentExporter,
46    IStudentsUtils)
[7949]47from waeup.kofa.students.payments import StudentPaymentsContainer
48from waeup.kofa.students.utils import generate_student_id
[8403]49from waeup.kofa.utils.helpers import attrs_to_fields, now, copy_filesystem_tree
[6621]50
[8448]51RE_STUDID_NON_NUM = re.compile('[^\d]+')
52
[6621]53class Student(grok.Container):
54    """This is a student container for the various objects
55    owned by students.
56    """
[9563]57    grok.implements(IStudent, IStudentNavigation, IStudentPersonalEdit)
[6621]58    grok.provides(IStudent)
59
60    def __init__(self):
61        super(Student, self).__init__()
[6749]62        # The site doesn't exist in unit tests
[6652]63        try:
[8481]64            self.student_id = generate_student_id()
[6749]65        except TypeError:
[6666]66            self.student_id = u'Z654321'
[6699]67        self.password = None
[9334]68        self.temp_password = None
[15606]69        self.parents_password = None
[6621]70        return
71
[9334]72    def setTempPassword(self, user, password):
73        """Set a temporary password (LDAP-compatible) SSHA encoded for
74        officers.
75        """
76        passwordmanager = getUtility(IPasswordManager, 'SSHA')
77        self.temp_password = {}
78        self.temp_password[
79            'password'] = passwordmanager.encodePassword(password)
80        self.temp_password['user'] = user
81        self.temp_password['timestamp'] = datetime.utcnow() # offset-naive datetime
82
83    def getTempPassword(self):
84        """Check if a temporary password has been set and if it
85        is not expired.
86
87        Return the temporary password if valid,
88        None otherwise. Unset the temporary password if expired.
89        """
90        temp_password_dict = getattr(self, 'temp_password', None)
[15609]91        kofa_utils = getUtility(IKofaUtils)
92        temp_password_minutes =  kofa_utils.TEMP_PASSWORD_MINUTES
[9334]93        if temp_password_dict is not None:
[15609]94            delta = timedelta(minutes=temp_password_minutes)
[9334]95            now = datetime.utcnow()
96            if now < temp_password_dict.get('timestamp') + delta:
97                return temp_password_dict.get('password')
98            else:
99                # Unset temporary password if expired
100                self.temp_password = None
101        return None
102
[15606]103    def setParentsPassword(self, password):
104        """Set a temporary password (LDAP-compatible) SSHA encoded for
105        parents.
106        """
107        passwordmanager = getUtility(IPasswordManager, 'SSHA')
108        self.parents_password = {}
109        self.parents_password[
110            'password'] = passwordmanager.encodePassword(password)
111        self.parents_password['timestamp'] = datetime.utcnow() # offset-naive datetime
112
113    def getParentsPassword(self):
114        """Check if a parents password has been set and if it
115        is not expired.
116
117        Return the parents password if valid,
118        None otherwise. Unset the parents password and replace roles
119        if expired.
120        """
121        parents_password_dict = getattr(self, 'parents_password', None)
[15609]122        kofa_utils = getUtility(IKofaUtils)
123        temp_password_minutes =  kofa_utils.TEMP_PASSWORD_MINUTES
[15606]124        if parents_password_dict is not None:
[15609]125            delta = timedelta(minutes=temp_password_minutes)
[15606]126            now = datetime.utcnow()
127            if now < parents_password_dict.get('timestamp') + delta:
128                return parents_password_dict.get('password')
129            else:
130                # Unset parents password if expired
131                self.parents_password = None
132                # Replace roles if expired
133                role_manager = IPrincipalRoleManager(self)
134                role_manager.removeRoleFromPrincipal(
135                    'waeup.local.Parents', self.student_id)
136                notify(LocalRoleSetEvent(
137                    self, 'waeup.local.Parents',
138                    self.student_id, granted=False))
139                role_manager.assignRoleToPrincipal(
140                    'waeup.local.StudentRecordOwner', self.student_id)
141                notify(LocalRoleSetEvent(
142                    self, 'waeup.local.StudentRecordOwner',
143                    self.student_id, granted=True))
144        return None
145
[8735]146    def writeLogMessage(self, view, message):
147        ob_class = view.__implemented__.__name__.replace('waeup.kofa.','')
[8737]148        self.__parent__.logger.info(
149            '%s - %s - %s' % (ob_class, self.__name__, message))
150        return
[6637]151
152    @property
[7364]153    def display_fullname(self):
[7357]154        middlename = getattr(self, 'middlename', None)
[7819]155        kofa_utils = getUtility(IKofaUtils)
[7811]156        return kofa_utils.fullname(self.firstname, self.lastname, middlename)
[7357]157
158    @property
[7364]159    def fullname(self):
160        middlename = getattr(self, 'middlename', None)
161        if middlename:
162            return '%s-%s-%s' % (self.firstname.lower(),
163                middlename.lower(), self.lastname.lower())
164        else:
165            return '%s-%s' % (self.firstname.lower(), self.lastname.lower())
166
167    @property
[6637]168    def state(self):
169        state = IWorkflowState(self).getState()
170        return state
171
172    @property
[7677]173    def translated_state(self):
[13080]174        try:
175            state = registration_states_vocab.getTermByToken(
176                self.state).title
177        except LookupError:  # in unit tests
178            return
[7677]179        return state
180
181    @property
[6637]182    def history(self):
183        history = IObjectHistory(self)
184        return history
185
[8736]186    @property
187    def student(self):
[6642]188        return self
189
[6814]190    @property
[7203]191    def certcode(self):
[6814]192        cert = getattr(self.get('studycourse', None), 'certificate', None)
[7203]193        if cert is not None:
194            return cert.code
195        return
[6814]196
[7062]197    @property
[7203]198    def faccode(self):
199        cert = getattr(self.get('studycourse', None), 'certificate', None)
200        if cert is not None:
201            return cert.__parent__.__parent__.__parent__.code
202        return
203
204    @property
205    def depcode(self):
206        cert = getattr(self.get('studycourse', None), 'certificate', None)
207        if cert is not None:
208            return cert.__parent__.__parent__.code
209        return
210
211    @property
[7062]212    def current_session(self):
[7948]213        session = getattr(
214            self.get('studycourse', None), 'current_session', None)
[7641]215        return session
[7062]216
[7641]217    @property
[9761]218    def entry_session(self):
219        session = getattr(
220            self.get('studycourse', None), 'entry_session', None)
221        return session
222
223    @property
[10245]224    def entry_mode(self):
225        session = getattr(
226            self.get('studycourse', None), 'entry_mode', None)
227        return session
228
229    @property
[9142]230    def current_level(self):
231        level = getattr(
232            self.get('studycourse', None), 'current_level', None)
233        return level
234
235    @property
[9183]236    def current_verdict(self):
[16412]237        current_verdict = getattr(
[9183]238            self.get('studycourse', None), 'current_verdict', None)
[16412]239        return current_verdict
[9183]240
241    @property
[7641]242    def current_mode(self):
[7948]243        certificate = getattr(
244            self.get('studycourse', None), 'certificate', None)
[7641]245        if certificate is not None:
246            return certificate.study_mode
[8472]247        return None
[7641]248
[8472]249    @property
250    def is_postgrad(self):
251        is_postgrad = getattr(
252            self.get('studycourse', None), 'is_postgrad', False)
253        return is_postgrad
254
[9517]255    @property
[10155]256    def is_special_postgrad(self):
257        is_special_postgrad = getattr(
258            self.get('studycourse', None), 'is_special_postgrad', False)
259        return is_special_postgrad
260
261    @property
[11604]262    def is_fresh(self):
263        return self.current_session == self.entry_session
264
265    @property
[9521]266    def before_payment(self):
[14169]267        non_fresh_states = (PAID, REGISTERED, VALIDATED, RETURNING, GRADUATED)
[11604]268        if self.is_fresh and self.state not in non_fresh_states:
[9517]269            return True
270        return False
271
[9545]272    @property
273    def personal_data_expired(self):
[16248]274        #if self.state in (CREATED, ADMITTED,):
275        #    return False
[9545]276        now = datetime.utcnow()
[9569]277        if self.personal_updated is None:
278            return True
[16713]279        try:
280            days_ago = getattr(now - self.personal_updated, 'days')
281        except TypeError:
282            # This happens after reimport of personal_updated because
283            # Python  can't subtract offset-naive and offset-aware datetimes
284            return True
[9569]285        if days_ago > 180:
286            return True
[9545]287        return False
288
[10266]289    @property
290    def transcript_enabled(self):
291        return True
292
[13103]293    @property
[15163]294    def studycourse_locked(self):
295        return self.state in (GRADUATED, TRANSREL, TRANSVAL)
296
297    @property
[13103]298    def clearance_locked(self):
299        return self.state != CLEARANCE
300
[9131]301    def transfer(self, certificate, current_session=None,
[9136]302        current_level=None, current_verdict=None, previous_verdict=None):
[9131]303        """ Creates a new studycourse and backups the old one.
304        """
[9962]305        newcourse = createObject(u'waeup.StudentStudyCourse')
[9137]306        try:
[9962]307            newcourse.certificate = certificate
[17769]308            # After import with entry_mode 'transfer'
309            # we must ensure that after export and reimport
310            # the student is not transferred again.
311            newcourse.entry_mode = 'transferred'
[9962]312            newcourse.current_session = current_session
313            newcourse.current_level = current_level
314            newcourse.current_verdict = current_verdict
315            newcourse.previous_verdict = previous_verdict
[9137]316        except ConstraintNotSatisfied:
317            return -1
[9962]318        oldcourse = self['studycourse']
319        if getattr(oldcourse, 'entry_session', None) is None or\
320            getattr(oldcourse, 'certificate', None) is None:
[9138]321            return -2
[9962]322        newcourse.entry_session = oldcourse.entry_session
[9131]323        # Students can be transferred only two times.
324        if 'studycourse_1' in self.keys():
325            if 'studycourse_2' in self.keys():
[9138]326                return -3
[9962]327            self['studycourse_2'] = oldcourse
[9131]328        else:
[9962]329            self['studycourse_1'] = oldcourse
[9131]330        del self['studycourse']
[9962]331        self['studycourse'] = newcourse
[9131]332        self.__parent__.logger.info(
333            '%s - transferred from %s to %s' % (
[13028]334            self.student_id,
335            oldcourse.certificate.code,
336            newcourse.certificate.code))
[9137]337        history = IObjectHistory(self)
338        history.addMessage('Transferred from %s to %s' % (
[9962]339            oldcourse.certificate.code, newcourse.certificate.code))
[9137]340        return
[9131]341
[10054]342    def revert_transfer(self):
343        """ Revert previous transfer.
[9131]344
[10054]345        """
346        if not self.has_key('studycourse_1'):
347            return -1
348        del self['studycourse']
349        if 'studycourse_2' in self.keys():
350            studycourse = self['studycourse_2']
351            self['studycourse'] = studycourse
352            del self['studycourse_2']
353        else:
354            studycourse = self['studycourse_1']
355            self['studycourse'] = studycourse
356            del self['studycourse_1']
357        self.__parent__.logger.info(
358            '%s - transfer reverted' % self.student_id)
359        history = IObjectHistory(self)
360        history.addMessage('Transfer reverted')
361        return
362
[6621]363# Set all attributes of Student required in IStudent as field
364# properties. Doing this, we do not have to set initial attributes
365# ourselves and as a bonus we get free validation when an attribute is
366# set.
367Student = attrs_to_fields(Student)
368
369class StudentFactory(grok.GlobalUtility):
370    """A factory for students.
371    """
372    grok.implements(IFactory)
373    grok.name(u'waeup.Student')
374    title = u"Create a new student.",
375    description = u"This factory instantiates new student instances."
376
377    def __call__(self, *args, **kw):
378        return Student()
379
380    def getInterfaces(self):
381        return implementedBy(Student)
[6836]382
[6838]383@grok.subscribe(IStudent, grok.IObjectAddedEvent)
[6839]384def handle_student_added(student, event):
[6838]385    """If a student is added all subcontainers are automatically added
[7948]386    and the transition create is fired. The latter produces a logging
387    message.
[6838]388    """
[8323]389    studycourse = createObject(u'waeup.StudentStudyCourse')
[6838]390    student['studycourse'] = studycourse
[6859]391    payments = StudentPaymentsContainer()
[6838]392    student['payments'] = payments
393    accommodation = StudentAccommodation()
394    student['accommodation'] = accommodation
395    # Assign global student role for new student
396    account = IUserAccount(student)
397    account.roles = ['waeup.Student']
398    # Assign local StudentRecordOwner role
399    role_manager = IPrincipalRoleManager(student)
400    role_manager.assignRoleToPrincipal(
401        'waeup.local.StudentRecordOwner', student.student_id)
[8375]402    if student.state is None:
[7513]403        IWorkflowInfo(student).fireTransition('create')
[6838]404    return
405
[8448]406def path_from_studid(student_id):
407    """Convert a student_id into a predictable relative folder path.
408
409    Used for storing files.
410
411    Returns the name of folder in which files for a particular student
412    should be stored. This is a relative path, relative to any general
[8452]413    students folder with 5 zero-padded digits (except when student_id
414    is overlong).
[8448]415
[8452]416    We normally map 1,000 different student ids into one single
417    path. For instance ``K1000000`` will give ``01000/K1000000``,
418    ``K1234567`` will give ``0123/K1234567`` and ``K12345678`` will
419    result in ``1234/K12345678``.
420
421    For lower numbers < 10**6 we return the same path for up to 10,000
422    student_ids. So for instance ``KM123456`` will result in
423    ``00120/KM123456`` (there will be no path starting with
424    ``00123``).
425
426    Works also with overlong number: here the leading zeros will be
427    missing but ``K123456789`` will give reliably
428    ``12345/K123456789`` as expected.
[8448]429    """
430    # remove all non numeric characters and turn this into an int.
431    num = int(RE_STUDID_NON_NUM.sub('', student_id))
[8452]432    if num < 10**6:
433        # store max. of 10000 studs per folder and correct num for 5 digits
434        num = num / 10000 * 10
435    else:
436        # store max. of 1000 studs per folder
437        num = num / 1000
438    # format folder name to have 5 zero-padded digits
439    folder_name = u'%05d' % num
[8448]440    folder_name = os.path.join(folder_name, student_id)
441    return folder_name
442
[8403]443def move_student_files(student, del_dir):
444    """Move files belonging to `student` to `del_dir`.
445
446    `del_dir` is expected to be the path to the site-wide directory
447    for storing backup data.
448
449    The current files of the student are removed after backup.
450
451    If the student has no associated files stored, nothing is done.
452    """
453    stud_id = student.student_id
454
455    src = getUtility(IExtFileStore).root
[8448]456    src = os.path.join(src, 'students', path_from_studid(stud_id))
[8403]457
[8448]458    dst = os.path.join(
459        del_dir, 'media', 'students', path_from_studid(stud_id))
[8403]460
461    if not os.path.isdir(src):
462        # Do not copy if no files were stored.
463        return
464    if not os.path.exists(dst):
465        os.makedirs(dst, 0755)
466    copy_filesystem_tree(src, dst)
467    shutil.rmtree(src)
468    return
469
470def update_student_deletion_csvs(student, del_dir):
471    """Update deletion CSV files with data from student.
472
473    `del_dir` is expected to be the path to the site-wide directory
474    for storing backup data.
475
476    Each exporter available for students (and their many subobjects)
477    is called in order to export CSV data of the given student to csv
478    files in the site-wide backup directory for object data (see
479    DataCenter).
480
481    Each exported row is appended a column giving the deletion date
482    (column `del_date`) as a UTC timestamp.
483    """
[12104]484
[12971]485    STUDENT_BACKUP_EXPORTER_NAMES = getUtility(
486        IStudentsUtils).STUDENT_BACKUP_EXPORTER_NAMES
[12104]487
[12971]488    for name in STUDENT_BACKUP_EXPORTER_NAMES:
[8411]489        exporter = getUtility(ICSVStudentExporter, name=name)
490        csv_data = exporter.export_student(student)
[8403]491        csv_data = csv_data.split('\r\n')
492
493        # append a deletion timestamp on each data row
494        timestamp = str(now().replace(microsecond=0)) # store UTC timestamp
495        for num, row in enumerate(csv_data[1:-1]):
496            csv_data[num+1] = csv_data[num+1] + ',' + timestamp
497        csv_path = os.path.join(del_dir, '%s.csv' % name)
498
499        # write data to CSV file
500        if not os.path.exists(csv_path):
501            # create new CSV file (including header line)
502            csv_data[0] = csv_data[0] + ',del_date'
503            open(csv_path, 'wb').write('\r\n'.join(csv_data))
504        else:
505            # append existing CSV file (omitting headerline)
506            open(csv_path, 'a').write('\r\n'.join(csv_data[1:]))
507    return
508
[6836]509@grok.subscribe(IStudent, grok.IObjectRemovedEvent)
[6839]510def handle_student_removed(student, event):
[8403]511    """If a student is removed a message is logged and data is put
512       into a backup location.
513
514    The data of the removed student is appended to CSV files in local
515    datacenter and any existing external files (passport images, etc.)
516    are copied over to this location as well.
517
518    Documents in the file storage refering to the given student are
519    removed afterwards (if they exist). Please make no assumptions
520    about how the deletion takes place. Files might be deleted
521    individually (leaving the students file directory intact) or the
522    whole student directory might be deleted completely.
523
524    All CSV rows created/appended contain a timestamp with the
525    datetime of removal in an additional `del_date` column.
526
527    XXX: blocking of used student_ids yet not implemented.
[6836]528    """
529    comment = 'Student record removed'
530    target = student.student_id
[6841]531    try:
[8403]532        site = grok.getSite()
533        site['students'].logger.info('%s - %s' % (
[7652]534            target, comment))
[7212]535    except KeyError:
536        # If we delete an entire university instance there won't be
537        # a students subcontainer
538        return
[8403]539
540    del_dir = site['datacenter'].deleted_path
541
[15416]542    if student.state == GRADUATED:
543        del_dir = site['datacenter'].graduated_path
544
[8403]545    # save files of the student
546    move_student_files(student, del_dir)
547
548    # update CSV files
549    update_student_deletion_csvs(student, del_dir)
[13224]550
551    # remove global role
552    role_manager = IPrincipalRoleManager(grok.getSite())
553    role_manager.unsetRoleForPrincipal('waeup.Student', student.student_id)
[7097]554    return
555
556#: The file id marker for student files
557STUDENT_FILE_STORE_NAME = 'file-student'
558
559class StudentFileNameChooser(grok.Adapter):
[7099]560    """A file id chooser for :class:`Student` objects.
[7097]561
[7099]562    `context` is an :class:`Student` instance.
[7097]563
[7099]564    The :class:`StudentImageNameChooser` can build/check file ids for
565    :class:`Student` objects suitable for use with
[7097]566    :class:`ExtFileStore` instances. The delivered file_id contains
[7099]567    the file id marker for :class:`Student` object and the student id
568    of the context student.
[7097]569
570    This chooser is registered as an adapter providing
[7811]571    :class:`waeup.kofa.interfaces.IFileStoreNameChooser`.
[7097]572
573    File store name choosers like this one are only convenience
[7099]574    components to ease the task of creating file ids for student
[7097]575    objects. You are nevertheless encouraged to use them instead of
[7099]576    manually setting up filenames for students.
[7097]577
[7811]578    .. seealso:: :mod:`waeup.kofa.imagestorage`
[7097]579
580    """
581    grok.context(IStudent)
582    grok.implements(IFileStoreNameChooser)
583
584    def checkName(self, name=None, attr=None):
585        """Check whether the given name is a valid file id for the context.
586
587        Returns ``True`` only if `name` equals the result of
588        :meth:`chooseName`.
589
590        """
591        return name == self.chooseName()
592
[7106]593    def chooseName(self, attr, name=None):
[7097]594        """Get a valid file id for student context.
595
596        *Example:*
597
[7105]598        For a student with student id ``'A123456'`` and
[7106]599        with attr ``'nice_image.jpeg'`` stored in
[7097]600        the students container this chooser would create:
601
[7106]602          ``'__file-student__students/A/A123456/nice_image_A123456.jpeg'``
[7097]603
604        meaning that the nice image of this applicant would be
605        stored in the site-wide file storage in path:
606
[7106]607          ``students/A/A123456/nice_image_A123456.jpeg``
[7097]608
609        """
[7106]610        basename, ext = os.path.splitext(attr)
[7099]611        stud_id = self.context.student_id
[8448]612        marked_filename = '__%s__%s/%s_%s%s' % (
613            STUDENT_FILE_STORE_NAME, path_from_studid(stud_id), basename,
[7948]614            stud_id, ext)
[7097]615        return marked_filename
616
617
618class StudentFileStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility):
619    """Student specific file handling.
620
621    This handler knows in which path in a filestore to store student
622    files and how to turn this kind of data into some (browsable)
623    file object.
624
625    It is called from the global file storage, when it wants to
626    get/store a file with a file id starting with
627    ``__file-student__`` (the marker string for student files).
628
629    Like each other file store handler it does not handle the files
630    really (this is done by the global file store) but only computes
631    paths and things like this.
632    """
633    grok.implements(IFileStoreHandler)
634    grok.name(STUDENT_FILE_STORE_NAME)
635
636    def pathFromFileID(self, store, root, file_id):
[7099]637        """All student files are put in directory ``students``.
[7097]638        """
639        marker, filename, basename, ext = store.extractMarker(file_id)
[7122]640        sub_root = os.path.join(root, 'students')
641        return super(StudentFileStoreHandler, self).pathFromFileID(
642            store, sub_root, basename)
[7097]643
644    def createFile(self, store, root, filename, file_id, file):
645        """Create a browsable file-like object.
646        """
[7122]647        # call super method to ensure that any old files with
648        # different filename extension are deleted.
649        file, path, file_obj =  super(
650            StudentFileStoreHandler, self).createFile(
651            store, root,  filename, file_id, file)
[7819]652        return file, path, KofaImageFile(
[7122]653            file_obj.filename, file_obj.data)
Note: See TracBrowser for help on using the repository browser.