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

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

More docs and adjustments in Python code.

  • Property svn:keywords set to Id
File size: 9.7 KB
RevLine 
[7757]1## $Id: export.py 13144 2015-07-07 05:21:14Z 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
[8986]34    fields = ('code', 'title', 'title_prefix', 'users_with_local_roles')
[7728]35
[7907]36    title = _(u'Faculties')
37
[8986]38    def mangle_value(self, value, name, context=None):
[13144]39        """The mangler computes the `users_with_local_roles` value which
[12858]40        is a Python expression like:
[12857]41
42        ``[{'user_name': u'bob', 'local_role': u'bobsrole'},
43        {'user_name': u'anna', 'local_role': u'annasrole'}]``
[8986]44        """
45        if name == 'users_with_local_roles':
46            value = []
47            role_map = IPrincipalRoleMap(context)
[12857]48            for local_role, user_name, setting in \
49                role_map.getPrincipalsAndRoles():
[8986]50                value.append({'user_name':user_name,'local_role':local_role})
51        return super(FacultyExporter, self).mangle_value(
52            value, name, context)
53
[7732]54    def export(self, faculties, filepath=None):
55        """Export `faculties`, an iterable, as CSV file.
[7728]56        If `filepath` is ``None``, a raw string with CSV data is returned.
57        """
58        writer, outfile = self.get_csv_writer(filepath)
[7732]59        for faculty in faculties:
60            self.write_item(faculty, writer)
[7728]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', {})
[7732]68        return self.export(faculties.values(), filepath)
69
[12858]70
[7732]71class DepartmentExporter(FacultyExporter, grok.GlobalUtility):
[12858]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.
[7732]75    """
76    grok.implements(ICSVExporter)
77    grok.name('departments')
78
[8986]79    fields = ('code', 'faculty_code', 'title', 'title_prefix',
80        'users_with_local_roles')
[7732]81
[7907]82    title = _(u'Departments')
83
[7732]84    def mangle_value(self, value, name, context=None):
[12858]85        """The mangler additionally computes the faculty_code value
86        which is the code (= object id) of the faculty that hosts
87        the department.
[7732]88        """
[7752]89        if name == 'faculty_code':
[7732]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):
[9031]97        """Export departments in faculty into filepath as CSV data.
[7732]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)
[7740]106
107
108class CourseExporter(FacultyExporter, grok.GlobalUtility):
[12858]109    """The Course Exporter exports all courses in the database. It iterates
110    over all departments and faculties.
[7740]111    """
112    grok.implements(ICSVExporter)
113    grok.name('courses')
114
[7752]115    fields = ('code', 'faculty_code', 'department_code', 'title', 'credits',
[9228]116              'passmark', 'semester', 'users_with_local_roles', 'former_course')
[7740]117
[7907]118    title = _(u'Courses')
119
[7740]120    def mangle_value(self, value, name, context=None):
[12858]121        """The mangler additionally computes the department_code value
122        which is the code of the department that offers the course.
[7740]123        """
[8986]124        if name == 'users_with_local_roles':
125            value = []
126            role_map = IPrincipalRoleMap(context)
[12857]127            for local_role, user_name, setting in \
128                role_map.getPrincipalsAndRoles():
[8986]129                value.append({'user_name':user_name,'local_role':local_role})
130        elif name == 'faculty_code':
[7740]131            try:
132                value = context.__parent__.__parent__.__parent__.code
133            except AttributeError:
134                value = None
[7752]135        elif name == 'department_code':
[7753]136            try:
137                value = context.__parent__.__parent__.code
138            except AttributeError:
139                value = None
[7740]140        return super(CourseExporter, self).mangle_value(
141            value, name, context)
142
143    def export_all(self, site, filepath=None):
[9031]144        """Export courses into filepath as CSV data.
[7740]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)
[7753]154
[12858]155
[7753]156class CertificateExporter(CourseExporter, grok.GlobalUtility):
[12858]157    """The Certificate Exporter exports all certificates in the database.
158    It iterates over all departments and faculties.
[7753]159    """
160    grok.implements(ICSVExporter)
161    grok.name('certificates')
162
163    fields = ('code', 'faculty_code', 'department_code', 'title', 'study_mode',
[10172]164              'start_level', 'end_level', 'application_category', 'ratio',
[9128]165              'school_fee_1', 'school_fee_2', 'school_fee_3', 'school_fee_4',
[10185]166              'custom_textline_1', 'custom_textline_2',
167              'custom_float_1', 'custom_float_2',
[9128]168              'users_with_local_roles')
[7753]169
[7907]170    title = _(u'Certificates')
171
[12858]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
[7753]179    def export_all(self, site, filepath=None):
[9031]180        """Export certificates into filepath as CSV data.
[7753]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)
[7756]190
[12858]191
[7756]192class CertificateCourseExporter(CourseExporter, grok.GlobalUtility):
[12858]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.
[7756]197    """
198    grok.implements(ICSVExporter)
199    grok.name('certificate_courses')
200
201    fields = ('course', 'faculty_code', 'department_code', 'certificate_code',
202              'level', 'mandatory')
203
[7907]204    title = _(u'Courses in Certificates')
205
[7756]206    def mangle_value(self, value, name, context=None):
[12858]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.
[7756]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):
[9031]233        """Export certificate courses into filepath as CSV data.
[7756]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.