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

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

Add department mover.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1## $Id: department.py 12609 2015-02-13 06:31:37Z 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"""University departments.
19"""
20import grok
21import zope.location.location
22from zope.event import notify
23from zope.catalog.interfaces import ICatalog
24from zope.component.interfaces import IFactory
25from zope.interface import implementedBy
26from zope.component import getUtility
27from zope.schema import getFields
28from waeup.kofa.university.faculty import longtitle
29from waeup.kofa.university.coursescontainer import CoursesContainer
30from waeup.kofa.university.certificatescontainer import CertificatesContainer
31from waeup.kofa.utils.batching import VirtualExportJobContainer
32from waeup.kofa.interfaces import IKofaUtils, IKofaPluggable
33from waeup.kofa.utils.helpers import attrs_to_fields
34from waeup.kofa.university.interfaces import IDepartment
35
36class VirtualDepartmentExportJobContainer(VirtualExportJobContainer):
37    """A virtual export job container for departments.
38    """
39
40class Department(grok.Container):
41    """A university department.
42    """
43    grok.implements(IDepartment)
44
45    local_roles = [
46        'waeup.local.ApplicationsManager',
47        'waeup.local.DepartmentOfficer',
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',
58        'waeup.local.CourseAdviser700',
59        'waeup.local.CourseAdviser800',
60        'waeup.local.LocalStudentsManager',
61        ]
62
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
71        self.courses = CoursesContainer()
72        self.courses.__parent__ = self
73        self.courses.__name__ = 'courses'
74        self.certificates = CertificatesContainer()
75        self.certificates.__parent__ = self
76        self.certificates.__name__ = 'certificates'
77        self.score_editing_disabled = False
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
87        elif name == 'exports':
88            # create a virtual exports container and return it
89            container = VirtualDepartmentExportJobContainer()
90            zope.location.location.located(container, self, 'exports')
91            return container
92        return None
93
94    @property
95    def longtitle(self):
96        return longtitle(self)
97
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
104        fac = grok.getSite()['faculties'][facname]
105        oldcode = self.code
106        fac[depname] = self
107        del self.__parent__[oldcode]
108        fac[depname].code = depname
109        #self.__parent__._p_changed = True
110        cat = getUtility(ICatalog, name='students_catalog')
111        results = cat.searchResults(depcode=(oldcode, oldcode))
112        for student in results:
113            notify(grok.ObjectModifiedEvent(student))
114            student.__parent__.logger.info(
115                '%s - Department moved' % student.__name__)
116        return
117
118class DepartmentFactory(grok.GlobalUtility):
119    """A factory for department containers.
120    """
121    grok.implements(IFactory)
122    grok.name(u'waeup.Department')
123    title = u"Create a new department.",
124    description = u"This factory instantiates new department instances."
125
126    def __call__(self, *args, **kw):
127        return Department(*args, **kw)
128
129    def getInterfaces(self):
130        """Get interfaces of objects provided by this factory.
131        """
132        return implementedBy(Department)
133
134class DepartmentsPlugin(grok.GlobalUtility):
135    """A plugin that updates courses.
136    """
137
138    grok.implements(IKofaPluggable)
139    grok.name('departments')
140
141    deprecated_attributes = []
142
143    def setup(self, site, name, logger):
144        return
145
146    def update(self, site, name, logger):
147        items = getFields(IDepartment).items()
148        for faculty in site['faculties'].values():
149            for department in faculty.values():
150                # Add new attributes
151                for i in items:
152                    if not hasattr(department,i[0]):
153                        setattr(department,i[0],i[1].missing_value)
154                        logger.info(
155                            'DepartmentsPlugin: %s attribute %s added.' % (
156                            department.code,i[0]))
157                # Remove deprecated attributes
158                for i in self.deprecated_attributes:
159                    try:
160                        delattr(department,i)
161                        logger.info(
162                            'DepartmentsPlugin: %s attribute %s deleted.' % (
163                            department.code,i))
164                    except AttributeError:
165                        pass
166        return
Note: See TracBrowser for help on using the repository browser.