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

Last change on this file since 9103 was 9031, checked in by Henrik Bettermann, 12 years ago

Do not open filepath twice.

Fix dicstring.

  • Property svn:keywords set to Id
File size: 8.5 KB
RevLine 
[7757]1## $Id: export.py 9031 2012-07-21 07:33:57Z 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):
[7728]27    """Exporter for faculties.
28    """
29    grok.implements(ICSVExporter)
[7732]30    grok.name('faculties')
[7728]31
32    #: Fieldnames considered by this exporter
[8986]33    fields = ('code', 'title', 'title_prefix', 'users_with_local_roles')
[7728]34
[7907]35    #: The title under which this exporter will be displayed
36    title = _(u'Faculties')
37
[8986]38    def mangle_value(self, value, name, context=None):
39        """Hook for mangling values in derived classes
40        """
41        if name == 'users_with_local_roles':
42            value = []
43            role_map = IPrincipalRoleMap(context)
44            for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
45                value.append({'user_name':user_name,'local_role':local_role})
46        return super(FacultyExporter, self).mangle_value(
47            value, name, context)
48
[7732]49    def export(self, faculties, filepath=None):
50        """Export `faculties`, an iterable, as CSV file.
[7728]51
52        If `filepath` is ``None``, a raw string with CSV data is returned.
53        """
54        writer, outfile = self.get_csv_writer(filepath)
[7732]55        for faculty in faculties:
56            self.write_item(faculty, writer)
[7728]57        return self.close_outfile(filepath, outfile)
58
59    def export_all(self, site, filepath=None):
60        """Export faculties in facultycontainer into filepath as CSV data.
61
62        If `filepath` is ``None``, a raw string with CSV data is returned.
63        """
64        faculties = site.get('faculties', {})
[7732]65        return self.export(faculties.values(), filepath)
66
67class DepartmentExporter(FacultyExporter, grok.GlobalUtility):
68    """Exporter for departments.
69    """
70    grok.implements(ICSVExporter)
71    grok.name('departments')
72
73    #: Fieldnames considered by this exporter
[8986]74    fields = ('code', 'faculty_code', 'title', 'title_prefix',
75        'users_with_local_roles')
[7732]76
[7907]77    #: The title under which this exporter will be displayed
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
93        If `filepath` is ``None``, a raw string with CSV data is returned.
94        """
95        writer, outfile = self.get_csv_writer(filepath)
96        faculties = site.get('faculties', {})
97        for faculty in faculties.values():
98            for department in faculty.values():
99                self.write_item(department, writer)
100        return self.close_outfile(filepath, outfile)
[7740]101
102
103class CourseExporter(FacultyExporter, grok.GlobalUtility):
104    """Exporter for courses.
105    """
106    grok.implements(ICSVExporter)
107    grok.name('courses')
108
109    #: Fieldnames considered by this exporter
[7752]110    fields = ('code', 'faculty_code', 'department_code', 'title', 'credits',
[8986]111              'passmark', 'semester', 'users_with_local_roles')
[7740]112
[7907]113    #: The title under which this exporter will be displayed
114    title = _(u'Courses')
115
[7740]116    def mangle_value(self, value, name, context=None):
117        """Hook for mangling values in derived classes
118        """
[8986]119        if name == 'users_with_local_roles':
120            value = []
121            role_map = IPrincipalRoleMap(context)
122            for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
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',
[8301]158              'start_level', 'end_level', 'application_category',
[8986]159              'school_fee_1', 'school_fee_2', 'users_with_local_roles')
[7753]160
[7907]161    #: The title under which this exporter will be displayed
162    title = _(u'Certificates')
163
[7753]164    def export_all(self, site, filepath=None):
[9031]165        """Export certificates into filepath as CSV data.
[7753]166
167        If `filepath` is ``None``, a raw string with CSV data is returned.
168        """
169        writer, outfile = self.get_csv_writer(filepath)
170        faculties = site.get('faculties', {})
171        for faculty in faculties.values():
172            for department in faculty.values():
173                for cert in department.certificates.values():
174                    self.write_item(cert, writer)
175        return self.close_outfile(filepath, outfile)
[7756]176
177class CertificateCourseExporter(CourseExporter, grok.GlobalUtility):
178    """Exporter for courses.
179    """
180    grok.implements(ICSVExporter)
181    grok.name('certificate_courses')
182
183    #: Fieldnames considered by this exporter
184    fields = ('course', 'faculty_code', 'department_code', 'certificate_code',
185              'level', 'mandatory')
186
[7907]187    #: The title under which this exporter will be displayed
188    title = _(u'Courses in Certificates')
189
[7756]190    def mangle_value(self, value, name, context=None):
191        """Hook for mangling values in derived classes
192        """
193        if name == 'faculty_code':
194            try:
195                value = context.__parent__.__parent__.__parent__.__parent__.code
196            except AttributeError:
197                value = None
198        elif name == 'department_code':
199            try:
200                value = context.__parent__.__parent__.__parent__.code
201            except AttributeError:
202                value = None
203        elif name == 'certificate_code':
204            value = getattr(context, '__parent__', None)
205            value = getattr(value, 'code', None)
206        if name == 'course':
207            value = getattr(value, 'code', None)
208        return super(CourseExporter, self).mangle_value(
209            value, name, context)
210
211    def export_all(self, site, filepath=None):
[9031]212        """Export certificate courses into filepath as CSV data.
[7756]213
214        If `filepath` is ``None``, a raw string with CSV data is returned.
215        """
216        writer, outfile = self.get_csv_writer(filepath)
217        faculties = site.get('faculties', {})
218        for faculty in faculties.values():
219            for department in faculty.values():
220                for cert in department.certificates.values():
221                    for certref in cert.values():
222                        self.write_item(certref, writer)
223        return self.close_outfile(filepath, outfile)
Note: See TracBrowser for help on using the repository browser.