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