Ignore:
Timestamp:
22 Jun 2009, 12:56:39 (16 years ago)
Author:
uli
Message:

Add event handler for removed courses/add certificate course factory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/university/certificate.py

    r4328 r4331  
    55from zope.component.interfaces import IFactory
    66from waeup.interfaces import (ICertificate, ICertificateContainer,
    7                               ICertificateCourse)
     7                              ICertificateCourse, ICourse, IDepartment)
    88
    99class Certificate(grok.Container):
     
    7777
    7878    def __init__(self, course=None, level=100, core_or_elective=True):
    79         # XXX Make sure, this reference is deleted, when the course
    80         # does not exist anymore (or the referenced course will still
    81         # exist here).
    8279        self.course = course
    8380        self.level = level
    8481        self.core_or_elective = core_or_elective
     82
     83class CertificateCourseFactory(grok.GlobalUtility):
     84    """A factory for certificate courses.
     85    """
     86    grok.implements(IFactory)
     87    grok.name(u'waeup.CertificateCourse')
     88    title = u"Create a new certificate course.",
     89    description = u"This factory instantiates new certificate courses."
     90
     91    def __call__(self):
     92        return CertificateCourse()
     93
     94    def getInterfaces(self):
     95        return implementedBy(CertificateCourse)
     96
     97@grok.subscribe(ICourse, grok.IObjectRemovedEvent)
     98def removedCourseHandler(course, event):
     99    """If a course is deleted, we make sure that also references in a
     100       certificatecontainer are removed.
     101    """
     102    old_parent = event.oldParent
     103    code = course.code
     104    # Try to find a nearby certificate container...
     105    dept = getattr(old_parent, '__parent__', None)
     106    if not IDepartment.providedBy(dept):
     107        # Hm, looks like this course was not part of a department...
     108        return
     109    certcontainer = dept.certificates
     110    for cert in certcontainer.values():
     111        # Loop through all certificatecourses in this certificate...
     112        if code in cert.keys():
     113            del cert[code]
     114            cert._p_changed = True
     115
Note: See TracChangeset for help on using the changeset viewer.