source: main/waeup.kofa/trunk/src/waeup/kofa/university/department.py @ 12777

Last change on this file since 12777 was 12620, checked in by Henrik Bettermann, 10 years ago

Improve department and certificate movers. Add utility view for moving certificates.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
RevLine 
[7195]1## $Id: department.py 12620 2015-02-16 11:27:24Z 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##
[4789]18"""University departments.
19"""
20import grok
[9734]21import zope.location.location
[12609]22from zope.event import notify
23from zope.catalog.interfaces import ICatalog
[4789]24from zope.component.interfaces import IFactory
25from zope.interface import implementedBy
[7681]26from zope.component import getUtility
[10635]27from zope.schema import getFields
[10561]28from waeup.kofa.university.faculty import longtitle
[7811]29from waeup.kofa.university.coursescontainer import CoursesContainer
30from waeup.kofa.university.certificatescontainer import CertificatesContainer
[9734]31from waeup.kofa.utils.batching import VirtualExportJobContainer
[10635]32from waeup.kofa.interfaces import IKofaUtils, IKofaPluggable
[10634]33from waeup.kofa.utils.helpers import attrs_to_fields
[10685]34from waeup.kofa.university.interfaces import IDepartment
[4789]35
[9797]36class VirtualDepartmentExportJobContainer(VirtualExportJobContainer):
37    """A virtual export job container for departments.
38    """
39
[4789]40class Department(grok.Container):
41    """A university department.
42    """
[10685]43    grok.implements(IDepartment)
[4789]44
[8993]45    local_roles = [
[10226]46        'waeup.local.ApplicationsManager',
[10279]47        'waeup.local.DepartmentOfficer',
[8993]48        'waeup.local.DepartmentManager',
49        'waeup.local.ClearanceOfficer',
50        'waeup.local.UGClearanceOfficer',
51        'waeup.local.PGClearanceOfficer',
52        'waeup.local.CourseAdviser100',
53        'waeup.local.CourseAdviser200',
54        'waeup.local.CourseAdviser300',
55        'waeup.local.CourseAdviser400',
56        'waeup.local.CourseAdviser500',
57        'waeup.local.CourseAdviser600',
[10064]58        'waeup.local.CourseAdviser700',
59        'waeup.local.CourseAdviser800',
[11891]60        'waeup.local.LocalStudentsManager',
[8993]61        ]
[6152]62
[4789]63    def __init__(self,
64                 title=u'Unnamed Department',
65                 title_prefix=u'department',
66                 code=u"NA", **kw):
67        super(Department, self).__init__(**kw)
68        self.title = title
69        self.title_prefix = title_prefix
70        self.code = code
[7681]71        self.courses = CoursesContainer()
[4789]72        self.courses.__parent__ = self
73        self.courses.__name__ = 'courses'
[7681]74        self.certificates = CertificatesContainer()
[4789]75        self.certificates.__parent__ = self
76        self.certificates.__name__ = 'certificates'
[10635]77        self.score_editing_disabled = False
[4789]78
79    def traverse(self, name):
80        """Deliver appropriate containers, if someone wants to go to courses
81        or departments.
82        """
83        if name == 'courses':
84            return self.courses
85        elif name == 'certificates':
86            return self.certificates
[9734]87        elif name == 'exports':
88            # create a virtual exports container and return it
[9797]89            container = VirtualDepartmentExportJobContainer()
[9734]90            zope.location.location.located(container, self, 'exports')
91            return container
[4789]92        return None
[6813]93
[10650]94    @property
[5988]95    def longtitle(self):
[10561]96        return longtitle(self)
[4789]97
[12609]98    def moveDepartment(self, facname, depname):
99        """ Move department to new department named depname in
100        faculty named facname.
101
102        """
103        self.moved = True
[12620]104        newfac = grok.getSite()['faculties'][facname]
[12609]105        oldcode = self.code
[12620]106        oldfac = self.__parent__
107        newfac[depname] = self
108        del oldfac[oldcode]
109        newfac[depname].code = depname
[12609]110        #self.__parent__._p_changed = True
111        cat = getUtility(ICatalog, name='students_catalog')
112        results = cat.searchResults(depcode=(oldcode, oldcode))
113        for student in results:
114            notify(grok.ObjectModifiedEvent(student))
115            student.__parent__.logger.info(
116                '%s - Department moved' % student.__name__)
117        return
118
[4789]119class DepartmentFactory(grok.GlobalUtility):
120    """A factory for department containers.
121    """
122    grok.implements(IFactory)
123    grok.name(u'waeup.Department')
124    title = u"Create a new department.",
125    description = u"This factory instantiates new department instances."
126
127    def __call__(self, *args, **kw):
128        return Department(*args, **kw)
129
130    def getInterfaces(self):
131        """Get interfaces of objects provided by this factory.
132        """
133        return implementedBy(Department)
[10635]134
135class DepartmentsPlugin(grok.GlobalUtility):
136    """A plugin that updates courses.
137    """
138
139    grok.implements(IKofaPluggable)
140    grok.name('departments')
141
142    deprecated_attributes = []
143
144    def setup(self, site, name, logger):
145        return
146
147    def update(self, site, name, logger):
148        items = getFields(IDepartment).items()
149        for faculty in site['faculties'].values():
150            for department in faculty.values():
151                # Add new attributes
152                for i in items:
153                    if not hasattr(department,i[0]):
154                        setattr(department,i[0],i[1].missing_value)
155                        logger.info(
156                            'DepartmentsPlugin: %s attribute %s added.' % (
157                            department.code,i[0]))
158                # Remove deprecated attributes
159                for i in self.deprecated_attributes:
160                    try:
161                        delattr(department,i)
162                        logger.info(
163                            'DepartmentsPlugin: %s attribute %s deleted.' % (
164                            department.code,i))
165                    except AttributeError:
166                        pass
167        return
Note: See TracBrowser for help on using the repository browser.