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

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

More documentation.

  • Property svn:keywords set to Id
File size: 9.7 KB
Line 
1## $Id: export.py 12858 2015-04-16 12:03:32Z 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              'start_level', 'end_level', 'application_category', 'ratio',
165              'school_fee_1', 'school_fee_2', 'school_fee_3', 'school_fee_4',
166              'custom_textline_1', 'custom_textline_2',
167              'custom_float_1', 'custom_float_2',
168              'users_with_local_roles')
169
170    title = _(u'Certificates')
171
172    def mangle_value(self, value, name, context=None):
173        """The mangler additionally computes the department_code value
174        which is the code of the department that offers the certificate.
175        """
176        return super(CertificateExporter, self).mangle_value(
177            value, name, context)
178
179    def export_all(self, site, filepath=None):
180        """Export certificates into filepath as CSV data.
181        If `filepath` is ``None``, a raw string with CSV data is returned.
182        """
183        writer, outfile = self.get_csv_writer(filepath)
184        faculties = site.get('faculties', {})
185        for faculty in faculties.values():
186            for department in faculty.values():
187                for cert in department.certificates.values():
188                    self.write_item(cert, writer)
189        return self.close_outfile(filepath, outfile)
190
191
192class CertificateCourseExporter(CourseExporter, grok.GlobalUtility):
193    """The Certificate Course Exporter exports all certificate courses
194    (:class:`CertificateCourse
195    <waeup.kofa.university.certificate.CertificateCourse>` instances) in
196    the database. It iterates over all departments and faculties.
197    """
198    grok.implements(ICSVExporter)
199    grok.name('certificate_courses')
200
201    fields = ('course', 'faculty_code', 'department_code', 'certificate_code',
202              'level', 'mandatory')
203
204    title = _(u'Courses in Certificates')
205
206    def mangle_value(self, value, name, context=None):
207        """The mangler computes the codes of the faculty, the department and
208        the certificate which require the course. It also exports the
209        course code.
210
211        .. note:: The course must not necessarily be offered by the same
212                  department.
213        """
214        if name == 'faculty_code':
215            try:
216                value = context.__parent__.__parent__.__parent__.__parent__.code
217            except AttributeError:
218                value = None
219        elif name == 'department_code':
220            try:
221                value = context.__parent__.__parent__.__parent__.code
222            except AttributeError:
223                value = None
224        elif name == 'certificate_code':
225            value = getattr(context, '__parent__', None)
226            value = getattr(value, 'code', None)
227        if name == 'course':
228            value = getattr(value, 'code', None)
229        return super(CourseExporter, self).mangle_value(
230            value, name, context)
231
232    def export_all(self, site, filepath=None):
233        """Export certificate courses into filepath as CSV data.
234        If `filepath` is ``None``, a raw string with CSV data is returned.
235        """
236        writer, outfile = self.get_csv_writer(filepath)
237        faculties = site.get('faculties', {})
238        for faculty in faculties.values():
239            for department in faculty.values():
240                for cert in department.certificates.values():
241                    for certref in cert.values():
242                        self.write_item(certref, writer)
243        return self.close_outfile(filepath, outfile)
Note: See TracBrowser for help on using the repository browser.