[7195] | 1 | ## $Id: batching.py 7665 2012-02-17 12:06:10Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 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 | ## |
---|
[5009] | 18 | """Batch processing components for academics objects. |
---|
| 19 | |
---|
| 20 | Batch processors eat CSV files to add, update or remove large numbers |
---|
| 21 | of certain kinds of objects at once. |
---|
| 22 | |
---|
| 23 | Here we define the processors for academics specific objects like |
---|
| 24 | faculties, departments and the like. |
---|
| 25 | """ |
---|
| 26 | import grok |
---|
| 27 | from zope.interface import Interface |
---|
| 28 | from waeup.sirp.interfaces import IBatchProcessor |
---|
| 29 | from waeup.sirp.university.interfaces import ( |
---|
[7333] | 30 | IFacultiesContainer, IFaculty, ICourse, IDepartment, ICertificate, |
---|
[5009] | 31 | ICertificateCourse) |
---|
| 32 | from waeup.sirp.utils.batching import BatchProcessor |
---|
| 33 | |
---|
| 34 | class FacultyProcessor(BatchProcessor): |
---|
| 35 | """A batch processor for IFaculty objects. |
---|
| 36 | """ |
---|
[6628] | 37 | grok.implements(IBatchProcessor) |
---|
[5009] | 38 | grok.provides(IBatchProcessor) |
---|
| 39 | grok.context(Interface) |
---|
| 40 | util_name = 'facultyimporter' |
---|
| 41 | grok.name(util_name) |
---|
| 42 | |
---|
[6821] | 43 | name = u'Faculty Importer' |
---|
[5009] | 44 | iface = IFaculty |
---|
| 45 | |
---|
| 46 | location_fields = ['code',] |
---|
| 47 | factory_name = 'waeup.Faculty' |
---|
| 48 | |
---|
[6628] | 49 | mode = None |
---|
| 50 | |
---|
[5009] | 51 | def parentsExist(self, row, site): |
---|
| 52 | return 'faculties' in site.keys() |
---|
| 53 | |
---|
| 54 | def entryExists(self, row, site): |
---|
| 55 | return row['code'] in site['faculties'].keys() |
---|
| 56 | |
---|
| 57 | def getParent(self, row, site): |
---|
| 58 | return site['faculties'] |
---|
| 59 | |
---|
| 60 | def getEntry(self, row, site): |
---|
| 61 | if not self.entryExists(row, site): |
---|
| 62 | return None |
---|
| 63 | parent = self.getParent(row, site) |
---|
| 64 | return parent.get(row['code']) |
---|
[6628] | 65 | |
---|
[5009] | 66 | def addEntry(self, obj, row, site): |
---|
| 67 | parent = self.getParent(row, site) |
---|
| 68 | parent.addFaculty(obj) |
---|
| 69 | return |
---|
| 70 | |
---|
| 71 | def delEntry(self, row, site): |
---|
| 72 | parent = self.getParent(row, site) |
---|
| 73 | del parent[row['code']] |
---|
| 74 | pass |
---|
| 75 | |
---|
| 76 | class DepartmentProcessor(BatchProcessor): |
---|
| 77 | """A batch processor for IDepartment objects. |
---|
| 78 | """ |
---|
[6628] | 79 | grok.implements(IBatchProcessor) |
---|
[5009] | 80 | grok.provides(IBatchProcessor) |
---|
| 81 | grok.context(Interface) |
---|
| 82 | util_name = 'departmentimporter' |
---|
| 83 | grok.name(util_name) |
---|
| 84 | |
---|
[6821] | 85 | name = u'Department Importer' |
---|
[5009] | 86 | iface = IDepartment |
---|
| 87 | |
---|
| 88 | location_fields = ['code', 'faculty_code'] |
---|
| 89 | factory_name = 'waeup.Department' |
---|
| 90 | |
---|
[6628] | 91 | mode = None |
---|
| 92 | |
---|
[5009] | 93 | def parentsExist(self, row, site): |
---|
| 94 | if not 'faculties' in site.keys(): |
---|
| 95 | return False |
---|
| 96 | return row['faculty_code'] in site['faculties'] |
---|
| 97 | |
---|
| 98 | def entryExists(self, row, site): |
---|
| 99 | if not self.parentsExist(row, site): |
---|
| 100 | return False |
---|
| 101 | parent = self.getParent(row, site) |
---|
| 102 | return row['code'] in parent.keys() |
---|
| 103 | |
---|
| 104 | def getParent(self, row, site): |
---|
| 105 | return site['faculties'][row['faculty_code']] |
---|
| 106 | |
---|
| 107 | def getEntry(self, row, site): |
---|
| 108 | if not self.entryExists(row, site): |
---|
| 109 | return None |
---|
| 110 | parent = self.getParent(row, site) |
---|
| 111 | return parent.get(row['code']) |
---|
[6628] | 112 | |
---|
[5009] | 113 | def addEntry(self, obj, row, site): |
---|
| 114 | parent = self.getParent(row, site) |
---|
| 115 | parent.addDepartment(obj) |
---|
| 116 | return |
---|
| 117 | |
---|
| 118 | def delEntry(self, row, site): |
---|
| 119 | parent = self.getParent(row, site) |
---|
| 120 | del parent[row['code']] |
---|
| 121 | return |
---|
| 122 | |
---|
| 123 | class CourseProcessor(BatchProcessor): |
---|
| 124 | """A batch processor for ICourse objects. |
---|
| 125 | """ |
---|
[6628] | 126 | grok.implements(IBatchProcessor) |
---|
[5009] | 127 | grok.provides(IBatchProcessor) |
---|
| 128 | grok.context(Interface) |
---|
| 129 | util_name = 'courseimporter' |
---|
| 130 | grok.name(util_name) |
---|
| 131 | |
---|
[6821] | 132 | name = u'Course Importer' |
---|
[5009] | 133 | iface = ICourse |
---|
| 134 | |
---|
| 135 | location_fields = ['code', 'faculty_code', 'department_code'] |
---|
| 136 | factory_name = 'waeup.Course' |
---|
| 137 | |
---|
[6628] | 138 | mode = None |
---|
| 139 | |
---|
[5009] | 140 | def parentsExist(self, row, site): |
---|
| 141 | if not 'faculties' in site.keys(): |
---|
| 142 | return False |
---|
| 143 | if not row['faculty_code'] in site['faculties'].keys(): |
---|
| 144 | return False |
---|
| 145 | faculty = site['faculties'][row['faculty_code']] |
---|
| 146 | return row['department_code'] in faculty.keys() |
---|
| 147 | |
---|
| 148 | def entryExists(self, row, site): |
---|
| 149 | if not self.parentsExist(row, site): |
---|
| 150 | return False |
---|
| 151 | parent = self.getParent(row, site) |
---|
| 152 | return row['code'] in parent.keys() |
---|
| 153 | |
---|
| 154 | def getParent(self, row, site): |
---|
| 155 | dept = site['faculties'][row['faculty_code']][row['department_code']] |
---|
| 156 | return dept.courses |
---|
| 157 | |
---|
| 158 | def getEntry(self, row, site): |
---|
| 159 | if not self.entryExists(row, site): |
---|
| 160 | return None |
---|
| 161 | parent = self.getParent(row, site) |
---|
| 162 | return parent.get(row['code']) |
---|
[6628] | 163 | |
---|
[5009] | 164 | def addEntry(self, obj, row, site): |
---|
| 165 | parent = self.getParent(row, site) |
---|
| 166 | parent.addCourse(obj) |
---|
| 167 | return |
---|
| 168 | |
---|
| 169 | def delEntry(self, row, site): |
---|
| 170 | parent = self.getParent(row, site) |
---|
| 171 | del parent[row['code']] |
---|
| 172 | return |
---|
| 173 | |
---|
| 174 | class CertificateProcessor(BatchProcessor): |
---|
| 175 | """A batch processor for ICertificate objects. |
---|
| 176 | """ |
---|
[6628] | 177 | grok.implements(IBatchProcessor) |
---|
[5009] | 178 | grok.provides(IBatchProcessor) |
---|
| 179 | grok.context(Interface) |
---|
| 180 | util_name = 'certificateimporter' |
---|
| 181 | grok.name(util_name) |
---|
| 182 | |
---|
[6821] | 183 | name = u'Certificate Importer' |
---|
[5009] | 184 | iface = ICertificate |
---|
| 185 | |
---|
| 186 | location_fields = ['code', 'faculty_code', 'department_code'] |
---|
| 187 | factory_name = 'waeup.Certificate' |
---|
| 188 | |
---|
[6628] | 189 | mode = None |
---|
| 190 | |
---|
[5009] | 191 | def parentsExist(self, row, site): |
---|
| 192 | if not 'faculties' in site.keys(): |
---|
| 193 | return False |
---|
| 194 | if not row['faculty_code'] in site['faculties'].keys(): |
---|
| 195 | return False |
---|
| 196 | faculty = site['faculties'][row['faculty_code']] |
---|
| 197 | return row['department_code'] in faculty.keys() |
---|
| 198 | |
---|
| 199 | def entryExists(self, row, site): |
---|
| 200 | if not self.parentsExist(row, site): |
---|
| 201 | return False |
---|
| 202 | parent = self.getParent(row, site) |
---|
| 203 | return row['code'] in parent.keys() |
---|
| 204 | |
---|
| 205 | def getParent(self, row, site): |
---|
| 206 | dept = site['faculties'][row['faculty_code']][row['department_code']] |
---|
| 207 | return dept.certificates |
---|
| 208 | |
---|
| 209 | def getEntry(self, row, site): |
---|
| 210 | if not self.entryExists(row, site): |
---|
| 211 | return None |
---|
| 212 | parent = self.getParent(row, site) |
---|
| 213 | return parent.get(row['code']) |
---|
| 214 | |
---|
| 215 | def addEntry(self, obj, row, site): |
---|
| 216 | parent = self.getParent(row, site) |
---|
[6243] | 217 | parent.addCertificate(obj) |
---|
| 218 | return |
---|
[5009] | 219 | |
---|
| 220 | def delEntry(self, row, site): |
---|
| 221 | parent = self.getParent(row, site) |
---|
| 222 | del parent[row['code']] |
---|
| 223 | return |
---|
| 224 | |
---|
| 225 | class CertificateCourseProcessor(BatchProcessor): |
---|
| 226 | """A batch processor for ICertificateCourse objects. |
---|
| 227 | """ |
---|
[6628] | 228 | grok.implements(IBatchProcessor) |
---|
[5009] | 229 | grok.provides(IBatchProcessor) |
---|
| 230 | grok.context(Interface) |
---|
| 231 | util_name = 'certificatecourseimporter' |
---|
| 232 | grok.name(util_name) |
---|
| 233 | |
---|
[6821] | 234 | name = u'CertificateCourse Importer' |
---|
[5009] | 235 | iface = ICertificateCourse |
---|
| 236 | |
---|
| 237 | location_fields = ['course', 'level', 'faculty_code', 'department_code', |
---|
| 238 | 'certificate_code',] |
---|
| 239 | factory_name = 'waeup.CertificateCourse' |
---|
| 240 | |
---|
[6628] | 241 | mode = None |
---|
| 242 | |
---|
[5009] | 243 | def parentsExist(self, row, site): |
---|
| 244 | if not 'faculties' in site.keys(): |
---|
| 245 | return False |
---|
| 246 | if not row['faculty_code'] in site['faculties'].keys(): |
---|
| 247 | return False |
---|
| 248 | faculty = site['faculties'][row['faculty_code']] |
---|
| 249 | if not row['department_code'] in faculty.keys(): |
---|
| 250 | return False |
---|
| 251 | dept = faculty[row['department_code']] |
---|
| 252 | return row['certificate_code'] in dept.certificates.keys() |
---|
| 253 | |
---|
| 254 | def entryExists(self, row, site): |
---|
| 255 | if not self.parentsExist(row, site): |
---|
| 256 | return False |
---|
| 257 | parent = self.getParent(row, site) |
---|
| 258 | code = "%s_%s" % (row['course'].code, row['level']) |
---|
| 259 | return code in parent.keys() |
---|
| 260 | |
---|
| 261 | def getParent(self, row, site): |
---|
| 262 | dept = site['faculties'][row['faculty_code']][row['department_code']] |
---|
| 263 | return dept.certificates[row['certificate_code']] |
---|
| 264 | |
---|
| 265 | def getEntry(self, row, site): |
---|
| 266 | if not self.entryExists(row, site): |
---|
| 267 | return None |
---|
| 268 | parent = self.getParent(row, site) |
---|
[6628] | 269 | return parent.get("%s_%s" % (row['course'].code, row['level'])) |
---|
[5009] | 270 | |
---|
| 271 | def addEntry(self, obj, row, site): |
---|
| 272 | parent = self.getParent(row, site) |
---|
| 273 | parent.addCourseRef(row['course'], |
---|
[7665] | 274 | row['level'], row['mandatory']) |
---|
[5009] | 275 | return |
---|
| 276 | |
---|
| 277 | def delEntry(self, row, site): |
---|
| 278 | parent = self.getParent(row, site) |
---|
| 279 | parent.delCourseRef(row['course'].code, row['level']) |
---|
| 280 | return |
---|