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 | ## |
---|
18 | """Exporters for faculties, departments, and other academics components. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from waeup.kofa.interfaces import ICSVExporter |
---|
22 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
23 | from waeup.kofa.utils.batching import ExporterBase |
---|
24 | |
---|
25 | class FacultyExporter(grok.GlobalUtility, ExporterBase): |
---|
26 | """Exporter for faculties. |
---|
27 | """ |
---|
28 | grok.implements(ICSVExporter) |
---|
29 | grok.name('faculties') |
---|
30 | |
---|
31 | #: Fieldnames considered by this exporter |
---|
32 | fields = ('code', 'title', 'title_prefix') |
---|
33 | |
---|
34 | #: The title under which this exporter will be displayed |
---|
35 | title = _(u'Faculties') |
---|
36 | |
---|
37 | def export(self, faculties, filepath=None): |
---|
38 | """Export `faculties`, an iterable, as CSV file. |
---|
39 | |
---|
40 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
41 | """ |
---|
42 | writer, outfile = self.get_csv_writer(filepath) |
---|
43 | for faculty in faculties: |
---|
44 | self.write_item(faculty, writer) |
---|
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', {}) |
---|
54 | return self.export(faculties.values(), filepath) |
---|
55 | |
---|
56 | class DepartmentExporter(FacultyExporter, grok.GlobalUtility): |
---|
57 | """Exporter for departments. |
---|
58 | """ |
---|
59 | grok.implements(ICSVExporter) |
---|
60 | grok.name('departments') |
---|
61 | |
---|
62 | #: Fieldnames considered by this exporter |
---|
63 | fields = ('code', 'faculty_code', 'title', 'title_prefix') |
---|
64 | |
---|
65 | #: The title under which this exporter will be displayed |
---|
66 | title = _(u'Departments') |
---|
67 | |
---|
68 | def mangle_value(self, value, name, context=None): |
---|
69 | """Hook for mangling values in derived classes |
---|
70 | """ |
---|
71 | if name == 'faculty_code': |
---|
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) |
---|
89 | |
---|
90 | |
---|
91 | class CourseExporter(FacultyExporter, grok.GlobalUtility): |
---|
92 | """Exporter for courses. |
---|
93 | """ |
---|
94 | grok.implements(ICSVExporter) |
---|
95 | grok.name('courses') |
---|
96 | |
---|
97 | #: Fieldnames considered by this exporter |
---|
98 | fields = ('code', 'faculty_code', 'department_code', 'title', 'credits', |
---|
99 | 'passmark', 'semester') |
---|
100 | |
---|
101 | #: The title under which this exporter will be displayed |
---|
102 | title = _(u'Courses') |
---|
103 | |
---|
104 | def mangle_value(self, value, name, context=None): |
---|
105 | """Hook for mangling values in derived classes |
---|
106 | """ |
---|
107 | if name == 'faculty_code': |
---|
108 | try: |
---|
109 | value = context.__parent__.__parent__.__parent__.code |
---|
110 | except AttributeError: |
---|
111 | value = None |
---|
112 | elif name == 'department_code': |
---|
113 | try: |
---|
114 | value = context.__parent__.__parent__.code |
---|
115 | except AttributeError: |
---|
116 | value = None |
---|
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) |
---|
132 | |
---|
133 | class 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 | |
---|
143 | #: The title under which this exporter will be displayed |
---|
144 | title = _(u'Certificates') |
---|
145 | |
---|
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) |
---|
158 | |
---|
159 | class 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 | |
---|
169 | #: The title under which this exporter will be displayed |
---|
170 | title = _(u'Courses in Certificates') |
---|
171 | |
---|
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) |
---|