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

Last change on this file since 14154 was 13654, checked in by Henrik Bettermann, 9 years ago

Export degree field.

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