Ignore:
Timestamp:
22 Sep 2011, 01:28:34 (13 years ago)
Author:
uli
Message:

Update local roles of officers for students when the students
certificate is changed/added.

As tests are missing, this is to be considered crap.

Also the other side (change local roles when clearance officers
change), is still missing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/students/studycourse.py

    r6802 r6815  
    2121from grok import index
    2222from zope.component.interfaces import IFactory
     23from zope.securitypolicy.interfaces import IPrincipalRoleManager
    2324from waeup.sirp.students.interfaces import (
    2425    IStudentStudyCourse, IStudentNavigation, IStudentStudyLevel)
     
    6566
    6667StudentStudyCourse = attrs_to_fields(StudentStudyCourse)
     68
     69@grok.subscribe(IStudentStudyCourse, grok.IObjectAddedEvent)
     70def handle_studycourse_added(obj, event):
     71    """When a studycourse is added, update the local roles.
     72    """
     73    cert = getattr(obj, 'certificate', None)
     74    if cert is None:
     75        # cert never set yet, no need to update local roles
     76        return
     77    update_local_roles(obj)
     78    return
     79
     80@grok.subscribe(IStudentStudyCourse, grok.IObjectModifiedEvent)
     81def handle_studycourse_modified(obj, event):
     82    """When a studycourse is changed, also update the local roles.
     83    """
     84    update_local_roles(obj)
     85    return
     86
     87def update_local_roles(studycourse):
     88    """Update local roles of given studycourse.
     89    """
     90    student = getattr(studycourse, '__parent__', None)
     91    if student is None:
     92        # not part of student, no need to update local roles
     93        return
     94    replace_local_clearance_officers(student)
     95    return
     96
     97def replace_local_clearance_officers(student):
     98    """Set local roles of given student.
     99
     100    The local roles are determined by looking up desired
     101    principals. Then we remove those principals from local roles that
     102    are not wanted/outdated and add those wanted.
     103    """
     104    prm = IPrincipalRoleManager(student)
     105    curr_officers = prm.getPrincipalsForRole(
     106        'waeup.local.ClearanceOfficer')
     107    new_officers = get_valid_clearance_officers(student)
     108    #print "STUD %s, CURR: %s, NEW: %s" % (student, curr_officers,
     109    #                                      new_officers)
     110    for officer in curr_officers:
     111        if officer in new_officers:
     112            continue
     113        prm.unsetRoleForPrincipal('waeup.local.ClearanceOfficer', officer)
     114    for officer in new_officers:
     115        if officer in curr_officers:
     116            continue
     117        prm.assignRoleToPrincipal('waeup.local.ClearanceOfficer', officer)
     118
     119    return
     120
     121def get_valid_clearance_officers(student):
     122    """Lookup desired clearance officers for `student`.
     123
     124    We look up the connected certificate, then its dept. and faculty
     125    to see which local roles are required: students get the clearance
     126    officers assigned to their certificate.
     127
     128    Not sure, though, whether we really have to set the real clearance
     129    officer role for the wanted principals on the dept and faculty.
     130    """
     131    cert = getattr(student, 'certificate', None)
     132    if cert is None:
     133        return []
     134    dept = getattr(getattr(cert, '__parent__', None), '__parent__', None)
     135    fac = getattr(dept, '__parent__', None)
     136    if dept is None or fac is None:
     137        # not a serious cert
     138        return []
     139    # get officers (principals) responsible for the dept and faculty
     140    dept_officers = IPrincipalRoleManager(dept).getPrincipalsForRole(
     141        'waeup.local.ClearanceOfficer')
     142    fac_officers =  IPrincipalRoleManager(fac).getPrincipalsForRole(
     143        'waeup.local.ClearanceOfficer')
     144    return dept_officers + fac_officers
Note: See TracChangeset for help on using the changeset viewer.