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

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

Remove trash.

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