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

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

Documentation work in progress.

  • Property svn:keywords set to Id
File size: 8.8 KB
RevLine 
[7757]1## $Id: export.py 12857 2015-04-15 14:28:46Z 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
[7861]26class FacultyExporter(grok.GlobalUtility, ExporterBase):
[12857]27    """The FacultyExporter exports all faculties in the 'faculties'
28    container. This is the only place where faculties are stored.
[7728]29    """
30    grok.implements(ICSVExporter)
[7732]31    grok.name('faculties')
[7728]32
[8986]33    fields = ('code', 'title', 'title_prefix', 'users_with_local_roles')
[7728]34
[7907]35    title = _(u'Faculties')
36
[8986]37    def mangle_value(self, value, name, context=None):
[12857]38        """The users_with_local_roles column contains Python expression like:
39
40        ``[{'user_name': u'bob', 'local_role': u'bobsrole'},
41        {'user_name': u'anna', 'local_role': u'annasrole'}]``
[8986]42        """
43        if name == 'users_with_local_roles':
44            value = []
45            role_map = IPrincipalRoleMap(context)
[12857]46            for local_role, user_name, setting in \
47                role_map.getPrincipalsAndRoles():
[8986]48                value.append({'user_name':user_name,'local_role':local_role})
49        return super(FacultyExporter, self).mangle_value(
50            value, name, context)
51
[7732]52    def export(self, faculties, filepath=None):
53        """Export `faculties`, an iterable, as CSV file.
[7728]54        If `filepath` is ``None``, a raw string with CSV data is returned.
55        """
56        writer, outfile = self.get_csv_writer(filepath)
[7732]57        for faculty in faculties:
58            self.write_item(faculty, writer)
[7728]59        return self.close_outfile(filepath, outfile)
60
61    def export_all(self, site, filepath=None):
62        """Export faculties in facultycontainer into filepath as CSV data.
63        If `filepath` is ``None``, a raw string with CSV data is returned.
64        """
65        faculties = site.get('faculties', {})
[7732]66        return self.export(faculties.values(), filepath)
67
68class DepartmentExporter(FacultyExporter, grok.GlobalUtility):
69    """Exporter for departments.
70    """
71    grok.implements(ICSVExporter)
72    grok.name('departments')
73
74    #: Fieldnames considered by this exporter
[8986]75    fields = ('code', 'faculty_code', 'title', 'title_prefix',
76        'users_with_local_roles')
[7732]77
[7907]78    title = _(u'Departments')
79
[7732]80    def mangle_value(self, value, name, context=None):
81        """Hook for mangling values in derived classes
82        """
[7752]83        if name == 'faculty_code':
[7732]84            value = getattr(
85                getattr(context, '__parent__', None),
86                'code', None)
87        return super(DepartmentExporter, self).mangle_value(
88            value, name, context)
89
90    def export_all(self, site, filepath=None):
[9031]91        """Export departments in faculty into filepath as CSV data.
[7732]92        If `filepath` is ``None``, a raw string with CSV data is returned.
93        """
94        writer, outfile = self.get_csv_writer(filepath)
95        faculties = site.get('faculties', {})
96        for faculty in faculties.values():
97            for department in faculty.values():
98                self.write_item(department, writer)
99        return self.close_outfile(filepath, outfile)
[7740]100
101
102class CourseExporter(FacultyExporter, grok.GlobalUtility):
103    """Exporter for courses.
104    """
105    grok.implements(ICSVExporter)
106    grok.name('courses')
107
108    #: Fieldnames considered by this exporter
[7752]109    fields = ('code', 'faculty_code', 'department_code', 'title', 'credits',
[9228]110              'passmark', 'semester', 'users_with_local_roles', 'former_course')
[7740]111
[7907]112    #: The title under which this exporter will be displayed
113    title = _(u'Courses')
114
[7740]115    def mangle_value(self, value, name, context=None):
116        """
[12857]117        """
[8986]118        if name == 'users_with_local_roles':
119            value = []
120            role_map = IPrincipalRoleMap(context)
[12857]121            for local_role, user_name, setting in \
122                role_map.getPrincipalsAndRoles():
[8986]123                value.append({'user_name':user_name,'local_role':local_role})
124        elif name == 'faculty_code':
[7740]125            try:
126                value = context.__parent__.__parent__.__parent__.code
127            except AttributeError:
128                value = None
[7752]129        elif name == 'department_code':
[7753]130            try:
131                value = context.__parent__.__parent__.code
132            except AttributeError:
133                value = None
[7740]134        return super(CourseExporter, self).mangle_value(
135            value, name, context)
136
137    def export_all(self, site, filepath=None):
[9031]138        """Export courses into filepath as CSV data.
[7740]139
140        If `filepath` is ``None``, a raw string with CSV data is returned.
141        """
142        writer, outfile = self.get_csv_writer(filepath)
143        faculties = site.get('faculties', {})
144        for faculty in faculties.values():
145            for department in faculty.values():
146                for course in department.courses.values():
147                    self.write_item(course, writer)
148        return self.close_outfile(filepath, outfile)
[7753]149
150class CertificateExporter(CourseExporter, grok.GlobalUtility):
151    """Exporter for courses.
152    """
153    grok.implements(ICSVExporter)
154    grok.name('certificates')
155
156    #: Fieldnames considered by this exporter
157    fields = ('code', 'faculty_code', 'department_code', 'title', 'study_mode',
[10172]158              'start_level', 'end_level', 'application_category', 'ratio',
[9128]159              'school_fee_1', 'school_fee_2', 'school_fee_3', 'school_fee_4',
[10185]160              'custom_textline_1', 'custom_textline_2',
161              'custom_float_1', 'custom_float_2',
[9128]162              'users_with_local_roles')
[7753]163
[7907]164    #: The title under which this exporter will be displayed
165    title = _(u'Certificates')
166
[7753]167    def export_all(self, site, filepath=None):
[9031]168        """Export certificates into filepath as CSV data.
[7753]169
170        If `filepath` is ``None``, a raw string with CSV data is returned.
171        """
172        writer, outfile = self.get_csv_writer(filepath)
173        faculties = site.get('faculties', {})
174        for faculty in faculties.values():
175            for department in faculty.values():
176                for cert in department.certificates.values():
177                    self.write_item(cert, writer)
178        return self.close_outfile(filepath, outfile)
[7756]179
180class CertificateCourseExporter(CourseExporter, grok.GlobalUtility):
181    """Exporter for courses.
182    """
183    grok.implements(ICSVExporter)
184    grok.name('certificate_courses')
185
186    #: Fieldnames considered by this exporter
187    fields = ('course', 'faculty_code', 'department_code', 'certificate_code',
188              'level', 'mandatory')
189
[7907]190    #: The title under which this exporter will be displayed
191    title = _(u'Courses in Certificates')
192
[7756]193    def mangle_value(self, value, name, context=None):
194        """Hook for mangling values in derived classes
195        """
196        if name == 'faculty_code':
197            try:
198                value = context.__parent__.__parent__.__parent__.__parent__.code
199            except AttributeError:
200                value = None
201        elif name == 'department_code':
202            try:
203                value = context.__parent__.__parent__.__parent__.code
204            except AttributeError:
205                value = None
206        elif name == 'certificate_code':
207            value = getattr(context, '__parent__', None)
208            value = getattr(value, 'code', None)
209        if name == 'course':
210            value = getattr(value, 'code', None)
211        return super(CourseExporter, self).mangle_value(
212            value, name, context)
213
214    def export_all(self, site, filepath=None):
[9031]215        """Export certificate courses into filepath as CSV data.
[7756]216
217        If `filepath` is ``None``, a raw string with CSV data is returned.
218        """
219        writer, outfile = self.get_csv_writer(filepath)
220        faculties = site.get('faculties', {})
221        for faculty in faculties.values():
222            for department in faculty.values():
223                for cert in department.certificates.values():
224                    for certref in cert.values():
225                        self.write_item(certref, writer)
226        return self.close_outfile(filepath, outfile)
Note: See TracBrowser for help on using the repository browser.