1 | ## $Id: export.py 16450 2021-04-11 10:17:28Z 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 zope.securitypolicy.interfaces import IPrincipalRoleMap |
---|
22 | from waeup.kofa.interfaces import ICSVExporter |
---|
23 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
24 | from waeup.kofa.utils.batching import ExporterBase |
---|
25 | |
---|
26 | |
---|
27 | class FacultyExporter(grok.GlobalUtility, ExporterBase): |
---|
28 | """The Faculty Exporter exports all faculties in the `faculties` |
---|
29 | container. This is the only place where faculties are stored. |
---|
30 | """ |
---|
31 | grok.implements(ICSVExporter) |
---|
32 | grok.name('faculties') |
---|
33 | |
---|
34 | fields = ('code', 'title', 'title_prefix', 'users_with_local_roles', |
---|
35 | 'officer_1', 'officer_2') |
---|
36 | |
---|
37 | title = _(u'Faculties') |
---|
38 | |
---|
39 | def mangle_value(self, value, name, context=None): |
---|
40 | """The mangler computes the `users_with_local_roles` value which |
---|
41 | is a Python expression like: |
---|
42 | |
---|
43 | ``[{'user_name': u'bob', 'local_role': u'bobsrole'}, |
---|
44 | {'user_name': u'anna', 'local_role': u'annasrole'}]`` |
---|
45 | """ |
---|
46 | if name == 'users_with_local_roles': |
---|
47 | value = [] |
---|
48 | role_map = IPrincipalRoleMap(context) |
---|
49 | for local_role, user_name, setting in \ |
---|
50 | role_map.getPrincipalsAndRoles(): |
---|
51 | value.append({'user_name':user_name,'local_role':local_role}) |
---|
52 | return super(FacultyExporter, self).mangle_value( |
---|
53 | value, name, context) |
---|
54 | |
---|
55 | def export(self, faculties, filepath=None): |
---|
56 | """Export `faculties`, an iterable, as CSV file. |
---|
57 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
58 | """ |
---|
59 | writer, outfile = self.get_csv_writer(filepath) |
---|
60 | for faculty in faculties: |
---|
61 | self.write_item(faculty, writer) |
---|
62 | return self.close_outfile(filepath, outfile) |
---|
63 | |
---|
64 | def export_all(self, site, filepath=None): |
---|
65 | """Export faculties in facultycontainer into filepath as CSV data. |
---|
66 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
67 | """ |
---|
68 | faculties = site.get('faculties', {}) |
---|
69 | return self.export(faculties.values(), filepath) |
---|
70 | |
---|
71 | |
---|
72 | class DepartmentExporter(FacultyExporter, grok.GlobalUtility): |
---|
73 | """The Department Exporter exports all departments stored in the faculty |
---|
74 | containers. The exporter iterates over all faculties and then over all |
---|
75 | departments inside each faculty container. |
---|
76 | """ |
---|
77 | grok.implements(ICSVExporter) |
---|
78 | grok.name('departments') |
---|
79 | |
---|
80 | fields = ('code', 'faculty_code', 'title', 'title_prefix', |
---|
81 | 'users_with_local_roles', |
---|
82 | 'officer_1', 'officer_2','officer_3', 'officer_4') |
---|
83 | |
---|
84 | title = _(u'Departments') |
---|
85 | |
---|
86 | def mangle_value(self, value, name, context=None): |
---|
87 | """The mangler additionally computes the faculty_code value |
---|
88 | which is the code (= object id) of the faculty that hosts |
---|
89 | the department. |
---|
90 | """ |
---|
91 | if name == 'faculty_code': |
---|
92 | value = getattr( |
---|
93 | getattr(context, '__parent__', None), |
---|
94 | 'code', None) |
---|
95 | return super(DepartmentExporter, self).mangle_value( |
---|
96 | value, name, context) |
---|
97 | |
---|
98 | def export_all(self, site, filepath=None): |
---|
99 | """Export departments in faculty into filepath as CSV data. |
---|
100 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
101 | """ |
---|
102 | writer, outfile = self.get_csv_writer(filepath) |
---|
103 | faculties = site.get('faculties', {}) |
---|
104 | for faculty in faculties.values(): |
---|
105 | for department in faculty.values(): |
---|
106 | self.write_item(department, writer) |
---|
107 | return self.close_outfile(filepath, outfile) |
---|
108 | |
---|
109 | |
---|
110 | class CourseExporter(FacultyExporter, grok.GlobalUtility): |
---|
111 | """The Course Exporter exports all courses in the database. It iterates |
---|
112 | over all departments and faculties. |
---|
113 | """ |
---|
114 | grok.implements(ICSVExporter) |
---|
115 | grok.name('courses') |
---|
116 | |
---|
117 | fields = ('code', 'faculty_code', 'department_code', 'title', 'credits', |
---|
118 | 'passmark', 'semester', 'users_with_local_roles', 'former_course', |
---|
119 | 'results_validated_by', 'results_validation_date', |
---|
120 | 'results_validation_session') |
---|
121 | |
---|
122 | title = _(u'Courses') |
---|
123 | |
---|
124 | def mangle_value(self, value, name, context=None): |
---|
125 | """The mangler additionally computes the department_code value |
---|
126 | which is the code of the department that offers the course. |
---|
127 | """ |
---|
128 | if name == 'users_with_local_roles': |
---|
129 | value = [] |
---|
130 | role_map = IPrincipalRoleMap(context) |
---|
131 | for local_role, user_name, setting in \ |
---|
132 | role_map.getPrincipalsAndRoles(): |
---|
133 | value.append({'user_name':user_name,'local_role':local_role}) |
---|
134 | elif name == 'faculty_code': |
---|
135 | try: |
---|
136 | value = context.__parent__.__parent__.__parent__.code |
---|
137 | except AttributeError: |
---|
138 | value = None |
---|
139 | elif name == 'department_code': |
---|
140 | try: |
---|
141 | value = context.__parent__.__parent__.code |
---|
142 | except AttributeError: |
---|
143 | value = None |
---|
144 | return super(CourseExporter, self).mangle_value( |
---|
145 | value, name, context) |
---|
146 | |
---|
147 | def export_all(self, site, filepath=None): |
---|
148 | """Export courses into filepath as CSV data. |
---|
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 course in department.courses.values(): |
---|
156 | self.write_item(course, writer) |
---|
157 | return self.close_outfile(filepath, outfile) |
---|
158 | |
---|
159 | |
---|
160 | class CertificateExporter(CourseExporter, grok.GlobalUtility): |
---|
161 | """The Certificate Exporter exports all certificates in the database. |
---|
162 | It iterates over all departments and faculties. |
---|
163 | """ |
---|
164 | grok.implements(ICSVExporter) |
---|
165 | grok.name('certificates') |
---|
166 | |
---|
167 | fields = ('code', 'faculty_code', 'department_code', 'title', 'study_mode', |
---|
168 | 'degree','key', |
---|
169 | 'start_level', 'end_level', 'application_category', 'ratio', |
---|
170 | 'school_fee_1', 'school_fee_2', 'school_fee_3', 'school_fee_4', |
---|
171 | 'custom_textline_1', 'custom_textline_2', |
---|
172 | 'custom_float_1', 'custom_float_2', |
---|
173 | 'users_with_local_roles') |
---|
174 | |
---|
175 | title = _(u'Certificates') |
---|
176 | |
---|
177 | def mangle_value(self, value, name, context=None): |
---|
178 | """The mangler additionally computes the department_code value |
---|
179 | which is the code of the department that offers the certificate. |
---|
180 | """ |
---|
181 | if name == 'key': |
---|
182 | value = context.__name__ |
---|
183 | return super(CertificateExporter, self).mangle_value( |
---|
184 | value, name, context) |
---|
185 | |
---|
186 | def export_all(self, site, filepath=None): |
---|
187 | """Export certificates into filepath as CSV data. |
---|
188 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
189 | """ |
---|
190 | writer, outfile = self.get_csv_writer(filepath) |
---|
191 | faculties = site.get('faculties', {}) |
---|
192 | for faculty in faculties.values(): |
---|
193 | for department in faculty.values(): |
---|
194 | for cert in department.certificates.values(): |
---|
195 | self.write_item(cert, writer) |
---|
196 | return self.close_outfile(filepath, outfile) |
---|
197 | |
---|
198 | |
---|
199 | class CertificateCourseExporter(CourseExporter, grok.GlobalUtility): |
---|
200 | """The Certificate Course Exporter exports all certificate courses |
---|
201 | (:class:`CertificateCourse |
---|
202 | <waeup.kofa.university.certificate.CertificateCourse>` instances) in |
---|
203 | the database. It iterates over all departments and faculties. |
---|
204 | """ |
---|
205 | grok.implements(ICSVExporter) |
---|
206 | grok.name('certificate_courses') |
---|
207 | |
---|
208 | fields = ('course', 'faculty_code', 'department_code', 'certificate_code', |
---|
209 | 'level', 'mandatory', 'course_category') |
---|
210 | |
---|
211 | title = _(u'Courses in Certificates') |
---|
212 | |
---|
213 | def mangle_value(self, value, name, context=None): |
---|
214 | """The mangler computes the codes of the faculty, the department and |
---|
215 | the certificate which require the course. It also exports the |
---|
216 | course code. |
---|
217 | |
---|
218 | .. note:: The course must not necessarily be offered by the same |
---|
219 | department. |
---|
220 | """ |
---|
221 | if name == 'faculty_code': |
---|
222 | try: |
---|
223 | value = context.__parent__.__parent__.__parent__.__parent__.code |
---|
224 | except AttributeError: |
---|
225 | value = None |
---|
226 | elif name == 'department_code': |
---|
227 | try: |
---|
228 | value = context.__parent__.__parent__.__parent__.code |
---|
229 | except AttributeError: |
---|
230 | value = None |
---|
231 | elif name == 'certificate_code': |
---|
232 | value = getattr(context, '__parent__', None) |
---|
233 | value = getattr(value, 'code', None) |
---|
234 | if name == 'course': |
---|
235 | value = getattr(value, 'code', None) |
---|
236 | return super(CourseExporter, self).mangle_value( |
---|
237 | value, name, context) |
---|
238 | |
---|
239 | def export_all(self, site, filepath=None): |
---|
240 | """Export certificate courses into filepath as CSV data. |
---|
241 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
242 | """ |
---|
243 | writer, outfile = self.get_csv_writer(filepath) |
---|
244 | faculties = site.get('faculties', {}) |
---|
245 | for faculty in faculties.values(): |
---|
246 | for department in faculty.values(): |
---|
247 | for cert in department.certificates.values(): |
---|
248 | for certref in cert.values(): |
---|
249 | self.write_item(certref, writer) |
---|
250 | return self.close_outfile(filepath, outfile) |
---|