1 | ## $Id: export.py 8301 2012-04-28 07:52:00Z 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 | ## |
---|
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 | 'school_fee_1', 'school_fee_2') |
---|
143 | |
---|
144 | #: The title under which this exporter will be displayed |
---|
145 | title = _(u'Certificates') |
---|
146 | |
---|
147 | def export_all(self, site, filepath=None): |
---|
148 | """Export faculties in facultycontainer into filepath as CSV data. |
---|
149 | |
---|
150 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
151 | """ |
---|
152 | writer, outfile = self.get_csv_writer(filepath) |
---|
153 | faculties = site.get('faculties', {}) |
---|
154 | for faculty in faculties.values(): |
---|
155 | for department in faculty.values(): |
---|
156 | for cert in department.certificates.values(): |
---|
157 | self.write_item(cert, writer) |
---|
158 | return self.close_outfile(filepath, outfile) |
---|
159 | |
---|
160 | class CertificateCourseExporter(CourseExporter, grok.GlobalUtility): |
---|
161 | """Exporter for courses. |
---|
162 | """ |
---|
163 | grok.implements(ICSVExporter) |
---|
164 | grok.name('certificate_courses') |
---|
165 | |
---|
166 | #: Fieldnames considered by this exporter |
---|
167 | fields = ('course', 'faculty_code', 'department_code', 'certificate_code', |
---|
168 | 'level', 'mandatory') |
---|
169 | |
---|
170 | #: The title under which this exporter will be displayed |
---|
171 | title = _(u'Courses in Certificates') |
---|
172 | |
---|
173 | def mangle_value(self, value, name, context=None): |
---|
174 | """Hook for mangling values in derived classes |
---|
175 | """ |
---|
176 | if name == 'faculty_code': |
---|
177 | try: |
---|
178 | value = context.__parent__.__parent__.__parent__.__parent__.code |
---|
179 | except AttributeError: |
---|
180 | value = None |
---|
181 | elif name == 'department_code': |
---|
182 | try: |
---|
183 | value = context.__parent__.__parent__.__parent__.code |
---|
184 | except AttributeError: |
---|
185 | value = None |
---|
186 | elif name == 'certificate_code': |
---|
187 | value = getattr(context, '__parent__', None) |
---|
188 | value = getattr(value, 'code', None) |
---|
189 | if name == 'course': |
---|
190 | value = getattr(value, 'code', None) |
---|
191 | return super(CourseExporter, self).mangle_value( |
---|
192 | value, name, context) |
---|
193 | |
---|
194 | def export_all(self, site, filepath=None): |
---|
195 | """Export faculties in facultycontainer into filepath as CSV data. |
---|
196 | |
---|
197 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
198 | """ |
---|
199 | writer, outfile = self.get_csv_writer(filepath) |
---|
200 | faculties = site.get('faculties', {}) |
---|
201 | for faculty in faculties.values(): |
---|
202 | for department in faculty.values(): |
---|
203 | for cert in department.certificates.values(): |
---|
204 | for certref in cert.values(): |
---|
205 | self.write_item(certref, writer) |
---|
206 | return self.close_outfile(filepath, outfile) |
---|