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

Last change on this file since 7982 was 7907, checked in by uli, 13 years ago

Add title attribute for exporters.

  • Property svn:keywords set to Id
File size: 7.6 KB
RevLine 
[7757]1## $Id: export.py 7907 2012-03-18 14:25:25Z uli $
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
[7811]21from waeup.kofa.interfaces import ICSVExporter
[7907]22from waeup.kofa.interfaces import MessageFactory as _
[7861]23from waeup.kofa.utils.batching import ExporterBase
[7728]24
[7861]25class FacultyExporter(grok.GlobalUtility, ExporterBase):
[7728]26    """Exporter for faculties.
27    """
28    grok.implements(ICSVExporter)
[7732]29    grok.name('faculties')
[7728]30
31    #: Fieldnames considered by this exporter
32    fields = ('code', 'title', 'title_prefix')
33
[7907]34    #: The title under which this exporter will be displayed
35    title = _(u'Faculties')
36
[7732]37    def export(self, faculties, filepath=None):
38        """Export `faculties`, an iterable, as CSV file.
[7728]39
40        If `filepath` is ``None``, a raw string with CSV data is returned.
41        """
42        writer, outfile = self.get_csv_writer(filepath)
[7732]43        for faculty in faculties:
44            self.write_item(faculty, writer)
[7728]45        return self.close_outfile(filepath, outfile)
46
47    def export_all(self, site, filepath=None):
48        """Export faculties in facultycontainer into filepath as CSV data.
49
50        If `filepath` is ``None``, a raw string with CSV data is returned.
51        """
52        writer, outfile = self.get_csv_writer(filepath)
53        faculties = site.get('faculties', {})
[7732]54        return self.export(faculties.values(), filepath)
55
56class DepartmentExporter(FacultyExporter, grok.GlobalUtility):
57    """Exporter for departments.
58    """
59    grok.implements(ICSVExporter)
60    grok.name('departments')
61
62    #: Fieldnames considered by this exporter
[7752]63    fields = ('code', 'faculty_code', 'title', 'title_prefix')
[7732]64
[7907]65    #: The title under which this exporter will be displayed
66    title = _(u'Departments')
67
[7732]68    def mangle_value(self, value, name, context=None):
69        """Hook for mangling values in derived classes
70        """
[7752]71        if name == 'faculty_code':
[7732]72            value = getattr(
73                getattr(context, '__parent__', None),
74                'code', None)
75        return super(DepartmentExporter, self).mangle_value(
76            value, name, context)
77
78    def export_all(self, site, filepath=None):
79        """Export faculties in facultycontainer into filepath as CSV data.
80
81        If `filepath` is ``None``, a raw string with CSV data is returned.
82        """
83        writer, outfile = self.get_csv_writer(filepath)
84        faculties = site.get('faculties', {})
85        for faculty in faculties.values():
86            for department in faculty.values():
87                self.write_item(department, writer)
88        return self.close_outfile(filepath, outfile)
[7740]89
90
91class CourseExporter(FacultyExporter, grok.GlobalUtility):
92    """Exporter for courses.
93    """
94    grok.implements(ICSVExporter)
95    grok.name('courses')
96
97    #: Fieldnames considered by this exporter
[7752]98    fields = ('code', 'faculty_code', 'department_code', 'title', 'credits',
[7740]99              'passmark', 'semester')
100
[7907]101    #: The title under which this exporter will be displayed
102    title = _(u'Courses')
103
[7740]104    def mangle_value(self, value, name, context=None):
105        """Hook for mangling values in derived classes
106        """
[7752]107        if name == 'faculty_code':
[7740]108            try:
109                value = context.__parent__.__parent__.__parent__.code
110            except AttributeError:
111                value = None
[7752]112        elif name == 'department_code':
[7753]113            try:
114                value = context.__parent__.__parent__.code
115            except AttributeError:
116                value = None
[7740]117        return super(CourseExporter, self).mangle_value(
118            value, name, context)
119
120    def export_all(self, site, filepath=None):
121        """Export faculties in facultycontainer into filepath as CSV data.
122
123        If `filepath` is ``None``, a raw string with CSV data is returned.
124        """
125        writer, outfile = self.get_csv_writer(filepath)
126        faculties = site.get('faculties', {})
127        for faculty in faculties.values():
128            for department in faculty.values():
129                for course in department.courses.values():
130                    self.write_item(course, writer)
131        return self.close_outfile(filepath, outfile)
[7753]132
133class CertificateExporter(CourseExporter, grok.GlobalUtility):
134    """Exporter for courses.
135    """
136    grok.implements(ICSVExporter)
137    grok.name('certificates')
138
139    #: Fieldnames considered by this exporter
140    fields = ('code', 'faculty_code', 'department_code', 'title', 'study_mode',
141              'start_level', 'end_level', 'application_category')
142
[7907]143    #: The title under which this exporter will be displayed
144    title = _(u'Certificates')
145
[7753]146    def export_all(self, site, filepath=None):
147        """Export faculties in facultycontainer into filepath as CSV data.
148
149        If `filepath` is ``None``, a raw string with CSV data is returned.
150        """
151        writer, outfile = self.get_csv_writer(filepath)
152        faculties = site.get('faculties', {})
153        for faculty in faculties.values():
154            for department in faculty.values():
155                for cert in department.certificates.values():
156                    self.write_item(cert, writer)
157        return self.close_outfile(filepath, outfile)
[7756]158
159class CertificateCourseExporter(CourseExporter, grok.GlobalUtility):
160    """Exporter for courses.
161    """
162    grok.implements(ICSVExporter)
163    grok.name('certificate_courses')
164
165    #: Fieldnames considered by this exporter
166    fields = ('course', 'faculty_code', 'department_code', 'certificate_code',
167              'level', 'mandatory')
168
[7907]169    #: The title under which this exporter will be displayed
170    title = _(u'Courses in Certificates')
171
[7756]172    def mangle_value(self, value, name, context=None):
173        """Hook for mangling values in derived classes
174        """
175        if name == 'faculty_code':
176            try:
177                value = context.__parent__.__parent__.__parent__.__parent__.code
178            except AttributeError:
179                value = None
180        elif name == 'department_code':
181            try:
182                value = context.__parent__.__parent__.__parent__.code
183            except AttributeError:
184                value = None
185        elif name == 'certificate_code':
186            value = getattr(context, '__parent__', None)
187            value = getattr(value, 'code', None)
188        if name == 'course':
189            value = getattr(value, 'code', None)
190        return super(CourseExporter, self).mangle_value(
191            value, name, context)
192
193    def export_all(self, site, filepath=None):
194        """Export faculties in facultycontainer into filepath as CSV data.
195
196        If `filepath` is ``None``, a raw string with CSV data is returned.
197        """
198        writer, outfile = self.get_csv_writer(filepath)
199        faculties = site.get('faculties', {})
200        for faculty in faculties.values():
201            for department in faculty.values():
202                for cert in department.certificates.values():
203                    for certref in cert.values():
204                        self.write_item(certref, writer)
205        return self.close_outfile(filepath, outfile)
Note: See TracBrowser for help on using the repository browser.