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

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

Export degree field.

  • Property svn:keywords set to Id
File size: 9.7 KB
RevLine 
[7757]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##
[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',
[13654]164              'degree',
[10172]165              'start_level', 'end_level', 'application_category', 'ratio',
[9128]166              'school_fee_1', 'school_fee_2', 'school_fee_3', 'school_fee_4',
[10185]167              'custom_textline_1', 'custom_textline_2',
168              'custom_float_1', 'custom_float_2',
[9128]169              'users_with_local_roles')
[7753]170
[7907]171    title = _(u'Certificates')
172
[12858]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
[7753]180    def export_all(self, site, filepath=None):
[9031]181        """Export certificates into filepath as CSV data.
[7753]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)
[7756]191
[12858]192
[7756]193class CertificateCourseExporter(CourseExporter, grok.GlobalUtility):
[12858]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.
[7756]198    """
199    grok.implements(ICSVExporter)
200    grok.name('certificate_courses')
201
202    fields = ('course', 'faculty_code', 'department_code', 'certificate_code',
203              'level', 'mandatory')
204
[7907]205    title = _(u'Courses in Certificates')
206
[7756]207    def mangle_value(self, value, name, context=None):
[12858]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.
[7756]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):
[9031]234        """Export certificate courses into filepath as CSV data.
[7756]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.