source: main/waeup.kofa/trunk/src/waeup/kofa/university/export.py @ 15189

Last change on this file since 15189 was 14511, checked in by Henrik Bettermann, 8 years ago

Add officer name fields do IDepartment and IFaculty. Plugins must be updated!

  • Property svn:keywords set to Id
File size: 9.8 KB
RevLine 
[7757]1## $Id: export.py 14511 2017-02-07 08:33:05Z henrik $
2##
3## Copyright (C) 2012 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##
[7728]18"""Exporters for faculties, departments, and other academics components.
19"""
20import grok
[8986]21from zope.securitypolicy.interfaces import IPrincipalRoleMap
[7811]22from waeup.kofa.interfaces import ICSVExporter
[7907]23from waeup.kofa.interfaces import MessageFactory as _
[7861]24from waeup.kofa.utils.batching import ExporterBase
[7728]25
[12858]26
[7861]27class FacultyExporter(grok.GlobalUtility, ExporterBase):
[12869]28    """The Faculty Exporter exports all faculties in the `faculties`
[12857]29    container. This is the only place where faculties are stored.
[7728]30    """
31    grok.implements(ICSVExporter)
[7732]32    grok.name('faculties')
[7728]33
[14511]34    fields = ('code', 'title', 'title_prefix', 'users_with_local_roles',
35              'officer_1', 'officer_2')
[7728]36
[7907]37    title = _(u'Faculties')
38
[8986]39    def mangle_value(self, value, name, context=None):
[13144]40        """The mangler computes the `users_with_local_roles` value which
[12858]41        is a Python expression like:
[12857]42
43        ``[{'user_name': u'bob', 'local_role': u'bobsrole'},
44        {'user_name': u'anna', 'local_role': u'annasrole'}]``
[8986]45        """
46        if name == 'users_with_local_roles':
47            value = []
48            role_map = IPrincipalRoleMap(context)
[12857]49            for local_role, user_name, setting in \
50                role_map.getPrincipalsAndRoles():
[8986]51                value.append({'user_name':user_name,'local_role':local_role})
52        return super(FacultyExporter, self).mangle_value(
53            value, name, context)
54
[7732]55    def export(self, faculties, filepath=None):
56        """Export `faculties`, an iterable, as CSV file.
[7728]57        If `filepath` is ``None``, a raw string with CSV data is returned.
58        """
59        writer, outfile = self.get_csv_writer(filepath)
[7732]60        for faculty in faculties:
61            self.write_item(faculty, writer)
[7728]62        return self.close_outfile(filepath, outfile)
63
64    def export_all(self, site, filepath=None):
65        """Export faculties in facultycontainer into filepath as CSV data.
66        If `filepath` is ``None``, a raw string with CSV data is returned.
67        """
68        faculties = site.get('faculties', {})
[7732]69        return self.export(faculties.values(), filepath)
70
[12858]71
[7732]72class DepartmentExporter(FacultyExporter, grok.GlobalUtility):
[12858]73    """The Department Exporter exports all departments stored in the faculty
74    containers. The exporter iterates over all faculties and then over all
75    departments inside each faculty container.
[7732]76    """
77    grok.implements(ICSVExporter)
78    grok.name('departments')
79
[8986]80    fields = ('code', 'faculty_code', 'title', 'title_prefix',
[14511]81              'users_with_local_roles',
82              'officer_1', 'officer_2','officer_3', 'officer_4')
[7732]83
[7907]84    title = _(u'Departments')
85
[7732]86    def mangle_value(self, value, name, context=None):
[12858]87        """The mangler additionally computes the faculty_code value
88        which is the code (= object id) of the faculty that hosts
89        the department.
[7732]90        """
[7752]91        if name == 'faculty_code':
[7732]92            value = getattr(
93                getattr(context, '__parent__', None),
94                'code', None)
95        return super(DepartmentExporter, self).mangle_value(
96            value, name, context)
97
98    def export_all(self, site, filepath=None):
[9031]99        """Export departments in faculty into filepath as CSV data.
[7732]100        If `filepath` is ``None``, a raw string with CSV data is returned.
101        """
102        writer, outfile = self.get_csv_writer(filepath)
103        faculties = site.get('faculties', {})
104        for faculty in faculties.values():
105            for department in faculty.values():
106                self.write_item(department, writer)
107        return self.close_outfile(filepath, outfile)
[7740]108
109
110class CourseExporter(FacultyExporter, grok.GlobalUtility):
[12858]111    """The Course Exporter exports all courses in the database. It iterates
112    over all departments and faculties.
[7740]113    """
114    grok.implements(ICSVExporter)
115    grok.name('courses')
116
[7752]117    fields = ('code', 'faculty_code', 'department_code', 'title', 'credits',
[9228]118              'passmark', 'semester', 'users_with_local_roles', 'former_course')
[7740]119
[7907]120    title = _(u'Courses')
121
[7740]122    def mangle_value(self, value, name, context=None):
[12858]123        """The mangler additionally computes the department_code value
124        which is the code of the department that offers the course.
[7740]125        """
[8986]126        if name == 'users_with_local_roles':
127            value = []
128            role_map = IPrincipalRoleMap(context)
[12857]129            for local_role, user_name, setting in \
130                role_map.getPrincipalsAndRoles():
[8986]131                value.append({'user_name':user_name,'local_role':local_role})
132        elif name == 'faculty_code':
[7740]133            try:
134                value = context.__parent__.__parent__.__parent__.code
135            except AttributeError:
136                value = None
[7752]137        elif name == 'department_code':
[7753]138            try:
139                value = context.__parent__.__parent__.code
140            except AttributeError:
141                value = None
[7740]142        return super(CourseExporter, self).mangle_value(
143            value, name, context)
144
145    def export_all(self, site, filepath=None):
[9031]146        """Export courses into filepath as CSV data.
[7740]147        If `filepath` is ``None``, a raw string with CSV data is returned.
148        """
149        writer, outfile = self.get_csv_writer(filepath)
150        faculties = site.get('faculties', {})
151        for faculty in faculties.values():
152            for department in faculty.values():
153                for course in department.courses.values():
154                    self.write_item(course, writer)
155        return self.close_outfile(filepath, outfile)
[7753]156
[12858]157
[7753]158class CertificateExporter(CourseExporter, grok.GlobalUtility):
[12858]159    """The Certificate Exporter exports all certificates in the database.
160    It iterates over all departments and faculties.
[7753]161    """
162    grok.implements(ICSVExporter)
163    grok.name('certificates')
164
165    fields = ('code', 'faculty_code', 'department_code', 'title', 'study_mode',
[13654]166              'degree',
[10172]167              'start_level', 'end_level', 'application_category', 'ratio',
[9128]168              'school_fee_1', 'school_fee_2', 'school_fee_3', 'school_fee_4',
[10185]169              'custom_textline_1', 'custom_textline_2',
170              'custom_float_1', 'custom_float_2',
[9128]171              'users_with_local_roles')
[7753]172
[7907]173    title = _(u'Certificates')
174
[12858]175    def mangle_value(self, value, name, context=None):
176        """The mangler additionally computes the department_code value
177        which is the code of the department that offers the certificate.
178        """
179        return super(CertificateExporter, self).mangle_value(
180            value, name, context)
181
[7753]182    def export_all(self, site, filepath=None):
[9031]183        """Export certificates into filepath as CSV data.
[7753]184        If `filepath` is ``None``, a raw string with CSV data is returned.
185        """
186        writer, outfile = self.get_csv_writer(filepath)
187        faculties = site.get('faculties', {})
188        for faculty in faculties.values():
189            for department in faculty.values():
190                for cert in department.certificates.values():
191                    self.write_item(cert, writer)
192        return self.close_outfile(filepath, outfile)
[7756]193
[12858]194
[7756]195class CertificateCourseExporter(CourseExporter, grok.GlobalUtility):
[12858]196    """The Certificate Course Exporter exports all certificate courses
197    (:class:`CertificateCourse
198    <waeup.kofa.university.certificate.CertificateCourse>` instances) in
199    the database. It iterates over all departments and faculties.
[7756]200    """
201    grok.implements(ICSVExporter)
202    grok.name('certificate_courses')
203
204    fields = ('course', 'faculty_code', 'department_code', 'certificate_code',
205              'level', 'mandatory')
206
[7907]207    title = _(u'Courses in Certificates')
208
[7756]209    def mangle_value(self, value, name, context=None):
[12858]210        """The mangler computes the codes of the faculty, the department and
211        the certificate which require the course. It also exports the
212        course code.
213
214        .. note:: The course must not necessarily be offered by the same
215                  department.
[7756]216        """
217        if name == 'faculty_code':
218            try:
219                value = context.__parent__.__parent__.__parent__.__parent__.code
220            except AttributeError:
221                value = None
222        elif name == 'department_code':
223            try:
224                value = context.__parent__.__parent__.__parent__.code
225            except AttributeError:
226                value = None
227        elif name == 'certificate_code':
228            value = getattr(context, '__parent__', None)
229            value = getattr(value, 'code', None)
230        if name == 'course':
231            value = getattr(value, 'code', None)
232        return super(CourseExporter, self).mangle_value(
233            value, name, context)
234
235    def export_all(self, site, filepath=None):
[9031]236        """Export certificate courses into filepath as CSV data.
[7756]237        If `filepath` is ``None``, a raw string with CSV data is returned.
238        """
239        writer, outfile = self.get_csv_writer(filepath)
240        faculties = site.get('faculties', {})
241        for faculty in faculties.values():
242            for department in faculty.values():
243                for cert in department.certificates.values():
244                    for certref in cert.values():
245                        self.write_item(certref, writer)
246        return self.close_outfile(filepath, outfile)
Note: See TracBrowser for help on using the repository browser.