[7195] | 1 | ## $Id: batching.py 9826 2013-01-05 14:07:47Z 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 |
---|
[8302] | 28 | from zope.component import queryUtility |
---|
| 29 | from zope.schema import getFields |
---|
| 30 | from zope.catalog.interfaces import ICatalog |
---|
[8995] | 31 | from zope.event import notify |
---|
[9001] | 32 | from zope.securitypolicy.interfaces import ( |
---|
| 33 | IPrincipalRoleManager, IPrincipalRoleMap) |
---|
[8995] | 34 | from waeup.kofa.authentication import LocalRoleSetEvent |
---|
[9001] | 35 | from waeup.kofa.interfaces import ( |
---|
| 36 | IBatchProcessor, IGNORE_MARKER, DELETION_MARKER, FatalCSVError) |
---|
[7811] | 37 | from waeup.kofa.university.interfaces import ( |
---|
[7333] | 38 | IFacultiesContainer, IFaculty, ICourse, IDepartment, ICertificate, |
---|
[5009] | 39 | ICertificateCourse) |
---|
[8996] | 40 | from waeup.kofa.university import ( |
---|
| 41 | Faculty, Department, Course, Certificate) |
---|
[7811] | 42 | from waeup.kofa.utils.batching import BatchProcessor |
---|
[5009] | 43 | |
---|
| 44 | class FacultyProcessor(BatchProcessor): |
---|
| 45 | """A batch processor for IFaculty objects. |
---|
| 46 | """ |
---|
[6628] | 47 | grok.implements(IBatchProcessor) |
---|
[5009] | 48 | grok.provides(IBatchProcessor) |
---|
| 49 | grok.context(Interface) |
---|
[7933] | 50 | util_name = 'facultyprocessor' |
---|
[5009] | 51 | grok.name(util_name) |
---|
| 52 | |
---|
[7933] | 53 | name = u'Faculty Processor' |
---|
[5009] | 54 | iface = IFaculty |
---|
[8994] | 55 | allowed_roles = Faculty.local_roles |
---|
[5009] | 56 | |
---|
| 57 | location_fields = ['code',] |
---|
| 58 | factory_name = 'waeup.Faculty' |
---|
| 59 | |
---|
[6628] | 60 | mode = None |
---|
| 61 | |
---|
[5009] | 62 | def parentsExist(self, row, site): |
---|
| 63 | return 'faculties' in site.keys() |
---|
| 64 | |
---|
[8994] | 65 | @property |
---|
| 66 | def available_fields(self): |
---|
| 67 | fields = getFields(self.iface) |
---|
| 68 | return sorted(list(set( |
---|
[8996] | 69 | self.location_fields + fields.keys() + ['local_roles'] |
---|
| 70 | ))) |
---|
[8994] | 71 | |
---|
[5009] | 72 | def entryExists(self, row, site): |
---|
| 73 | return row['code'] in site['faculties'].keys() |
---|
| 74 | |
---|
| 75 | def getParent(self, row, site): |
---|
| 76 | return site['faculties'] |
---|
| 77 | |
---|
| 78 | def getEntry(self, row, site): |
---|
| 79 | if not self.entryExists(row, site): |
---|
| 80 | return None |
---|
| 81 | parent = self.getParent(row, site) |
---|
| 82 | return parent.get(row['code']) |
---|
[6628] | 83 | |
---|
[5009] | 84 | def addEntry(self, obj, row, site): |
---|
| 85 | parent = self.getParent(row, site) |
---|
| 86 | parent.addFaculty(obj) |
---|
| 87 | return |
---|
| 88 | |
---|
| 89 | def delEntry(self, row, site): |
---|
| 90 | parent = self.getParent(row, site) |
---|
| 91 | del parent[row['code']] |
---|
| 92 | pass |
---|
| 93 | |
---|
[9706] | 94 | def updateEntry(self, obj, row, site, filename): |
---|
[8995] | 95 | """Update obj to the values given in row. |
---|
| 96 | """ |
---|
| 97 | items_changed = '' |
---|
| 98 | |
---|
[9701] | 99 | if 'local_roles' in row and row['local_roles'] not in ( |
---|
[9001] | 100 | None, IGNORE_MARKER): |
---|
| 101 | role_manager = IPrincipalRoleManager(obj) |
---|
| 102 | role_map = IPrincipalRoleMap(obj) |
---|
| 103 | # Remove all existing local roles. |
---|
| 104 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
| 105 | role_manager.unsetRoleForPrincipal(local_role, user_name) |
---|
| 106 | notify(LocalRoleSetEvent( |
---|
| 107 | obj, local_role, user_name, granted=False)) |
---|
| 108 | # Add new local roles. |
---|
| 109 | if row['local_roles'] != DELETION_MARKER: |
---|
| 110 | local_roles = eval(row['local_roles']) |
---|
| 111 | for rolemap in local_roles: |
---|
| 112 | user = rolemap['user_name'] |
---|
| 113 | local_role = rolemap['local_role'] |
---|
| 114 | role_manager.assignRoleToPrincipal(local_role, user) |
---|
| 115 | notify(LocalRoleSetEvent(obj, local_role, user, granted=True)) |
---|
| 116 | items_changed += ( |
---|
| 117 | '%s=%s, ' % ('local_roles', '%s|%s' % (user,local_role))) |
---|
[8995] | 118 | row.pop('local_roles') |
---|
| 119 | |
---|
| 120 | # apply other values... |
---|
| 121 | items_changed += super(FacultyProcessor, self).updateEntry( |
---|
[9706] | 122 | obj, row, site, filename) |
---|
[8995] | 123 | |
---|
| 124 | # Log actions... |
---|
[9087] | 125 | location_field = self.location_fields[0] |
---|
[9706] | 126 | grok.getSite().logger.info('%s - %s - %s - updated: %s' |
---|
| 127 | % (self.name, filename, row[location_field], items_changed)) |
---|
[8995] | 128 | return items_changed |
---|
| 129 | |
---|
[8994] | 130 | def checkConversion(self, row, mode='create'): |
---|
| 131 | """Validates all values in row. |
---|
| 132 | """ |
---|
| 133 | errs, inv_errs, conv_dict = super( |
---|
| 134 | FacultyProcessor, self).checkConversion(row, mode=mode) |
---|
[9701] | 135 | if 'local_roles' in row: |
---|
[9001] | 136 | if row['local_roles'] in (None, DELETION_MARKER, IGNORE_MARKER): |
---|
| 137 | return errs, inv_errs, conv_dict |
---|
[8994] | 138 | try: |
---|
| 139 | local_roles = eval(row['local_roles']) |
---|
| 140 | except: |
---|
| 141 | errs.append(('local_roles','Error')) |
---|
| 142 | return errs, inv_errs, conv_dict |
---|
| 143 | if not isinstance(local_roles, list): |
---|
| 144 | errs.append(('local_roles','no list')) |
---|
| 145 | return errs, inv_errs, conv_dict |
---|
| 146 | for rolemap in local_roles: |
---|
| 147 | if not isinstance(rolemap, dict): |
---|
| 148 | errs.append(('local_roles','no dicts')) |
---|
| 149 | return errs, inv_errs, conv_dict |
---|
| 150 | if not 'user_name' in rolemap.keys() or not \ |
---|
| 151 | 'local_role' in rolemap.keys(): |
---|
| 152 | errs.append(('local_roles','user_name or local_role missing')) |
---|
| 153 | return errs, inv_errs, conv_dict |
---|
| 154 | local_role = rolemap['local_role'] |
---|
| 155 | if not local_role in self.allowed_roles: |
---|
| 156 | errs.append(('local_roles','%s not allowed' % local_role)) |
---|
| 157 | return errs, inv_errs, conv_dict |
---|
| 158 | user = rolemap['user_name'] |
---|
| 159 | users = grok.getSite()['users'] |
---|
| 160 | if not user in users.keys(): |
---|
| 161 | errs.append(('local_roles','%s does not exist' % user)) |
---|
| 162 | return errs, inv_errs, conv_dict |
---|
| 163 | return errs, inv_errs, conv_dict |
---|
| 164 | |
---|
[8996] | 165 | class DepartmentProcessor(FacultyProcessor): |
---|
[5009] | 166 | """A batch processor for IDepartment objects. |
---|
| 167 | """ |
---|
[6628] | 168 | grok.implements(IBatchProcessor) |
---|
[5009] | 169 | grok.provides(IBatchProcessor) |
---|
| 170 | grok.context(Interface) |
---|
[7933] | 171 | util_name = 'departmentprocessor' |
---|
[5009] | 172 | grok.name(util_name) |
---|
| 173 | |
---|
[7933] | 174 | name = u'Department Processor' |
---|
[5009] | 175 | iface = IDepartment |
---|
[8996] | 176 | allowed_roles = Department.local_roles |
---|
[5009] | 177 | |
---|
| 178 | location_fields = ['code', 'faculty_code'] |
---|
| 179 | factory_name = 'waeup.Department' |
---|
| 180 | |
---|
[6628] | 181 | mode = None |
---|
| 182 | |
---|
[5009] | 183 | def parentsExist(self, row, site): |
---|
| 184 | if not 'faculties' in site.keys(): |
---|
| 185 | return False |
---|
| 186 | return row['faculty_code'] in site['faculties'] |
---|
| 187 | |
---|
| 188 | def entryExists(self, row, site): |
---|
| 189 | if not self.parentsExist(row, site): |
---|
| 190 | return False |
---|
| 191 | parent = self.getParent(row, site) |
---|
| 192 | return row['code'] in parent.keys() |
---|
| 193 | |
---|
| 194 | def getParent(self, row, site): |
---|
| 195 | return site['faculties'][row['faculty_code']] |
---|
| 196 | |
---|
| 197 | def getEntry(self, row, site): |
---|
| 198 | if not self.entryExists(row, site): |
---|
| 199 | return None |
---|
| 200 | parent = self.getParent(row, site) |
---|
| 201 | return parent.get(row['code']) |
---|
[6628] | 202 | |
---|
[5009] | 203 | def addEntry(self, obj, row, site): |
---|
| 204 | parent = self.getParent(row, site) |
---|
| 205 | parent.addDepartment(obj) |
---|
| 206 | return |
---|
| 207 | |
---|
| 208 | def delEntry(self, row, site): |
---|
| 209 | parent = self.getParent(row, site) |
---|
| 210 | del parent[row['code']] |
---|
| 211 | return |
---|
| 212 | |
---|
[8996] | 213 | class CertificateProcessor(FacultyProcessor): |
---|
[5009] | 214 | """A batch processor for ICertificate objects. |
---|
| 215 | """ |
---|
[6628] | 216 | grok.implements(IBatchProcessor) |
---|
[5009] | 217 | grok.provides(IBatchProcessor) |
---|
| 218 | grok.context(Interface) |
---|
[7933] | 219 | util_name = 'certificateprocessor' |
---|
[5009] | 220 | grok.name(util_name) |
---|
| 221 | |
---|
[7933] | 222 | name = u'Certificate Processor' |
---|
[5009] | 223 | iface = ICertificate |
---|
[8996] | 224 | allowed_roles = Certificate.local_roles |
---|
[5009] | 225 | |
---|
[8302] | 226 | location_fields = ['code'] |
---|
[5009] | 227 | factory_name = 'waeup.Certificate' |
---|
| 228 | |
---|
[6628] | 229 | mode = None |
---|
| 230 | |
---|
[8302] | 231 | @property |
---|
| 232 | def available_fields(self): |
---|
| 233 | fields = getFields(self.iface) |
---|
| 234 | return sorted(list(set( |
---|
[8996] | 235 | ['faculty_code','department_code'] + fields.keys() |
---|
| 236 | + ['local_roles']))) |
---|
[8302] | 237 | |
---|
| 238 | def checkHeaders(self, headerfields, mode='create'): |
---|
[9333] | 239 | super(CertificateProcessor, self).checkHeaders(headerfields, mode) |
---|
[8302] | 240 | if mode == 'create': |
---|
| 241 | if not 'faculty_code' in headerfields \ |
---|
| 242 | and not 'department_code' in headerfields : |
---|
| 243 | raise FatalCSVError( |
---|
| 244 | "Need at least columns faculty_code and department_code") |
---|
| 245 | return True |
---|
| 246 | |
---|
[5009] | 247 | def parentsExist(self, row, site): |
---|
[8302] | 248 | return self.getParent(row,site) is not None |
---|
[5009] | 249 | |
---|
| 250 | def entryExists(self, row, site): |
---|
| 251 | parent = self.getParent(row, site) |
---|
[8302] | 252 | if parent is not None: |
---|
| 253 | return row['code'] in parent.keys() |
---|
| 254 | return False |
---|
[5009] | 255 | |
---|
| 256 | def getParent(self, row, site): |
---|
[8302] | 257 | if not 'faculties' in site.keys(): |
---|
| 258 | return None |
---|
| 259 | # If both faculty and department codes are provided, use |
---|
| 260 | # these to get parent. |
---|
| 261 | if row.get('faculty_code',None) not in (None, IGNORE_MARKER) and \ |
---|
| 262 | row.get('department_code',None) not in (None, IGNORE_MARKER): |
---|
| 263 | if not row['faculty_code'] in site['faculties'].keys(): |
---|
| 264 | return None |
---|
| 265 | faculty = site['faculties'][row['faculty_code']] |
---|
| 266 | if not row['department_code'] in faculty.keys(): |
---|
| 267 | return None |
---|
| 268 | dept = faculty[row['department_code']] |
---|
| 269 | return dept.certificates |
---|
| 270 | # If department code or faculty code is missing, |
---|
| 271 | # use catalog to get parent. Makes only sense in update mode but |
---|
| 272 | # does also work in create mode. |
---|
| 273 | cat = queryUtility(ICatalog, name='certificates_catalog') |
---|
| 274 | results = list( |
---|
| 275 | cat.searchResults(code=(row['code'], row['code']))) |
---|
| 276 | if results: |
---|
| 277 | return results[0].__parent__ |
---|
| 278 | return None |
---|
[5009] | 279 | |
---|
| 280 | def getEntry(self, row, site): |
---|
| 281 | parent = self.getParent(row, site) |
---|
[8302] | 282 | if parent is not None: |
---|
| 283 | return parent.get(row['code']) |
---|
| 284 | return None |
---|
[5009] | 285 | |
---|
| 286 | def addEntry(self, obj, row, site): |
---|
| 287 | parent = self.getParent(row, site) |
---|
[6243] | 288 | parent.addCertificate(obj) |
---|
| 289 | return |
---|
[5009] | 290 | |
---|
| 291 | def delEntry(self, row, site): |
---|
| 292 | parent = self.getParent(row, site) |
---|
| 293 | del parent[row['code']] |
---|
| 294 | return |
---|
| 295 | |
---|
[9333] | 296 | class CourseProcessor(CertificateProcessor): |
---|
| 297 | """A batch processor for ICourse objects. |
---|
| 298 | """ |
---|
| 299 | grok.implements(IBatchProcessor) |
---|
| 300 | grok.provides(IBatchProcessor) |
---|
| 301 | grok.context(Interface) |
---|
| 302 | util_name = 'courseprocessor' |
---|
| 303 | grok.name(util_name) |
---|
| 304 | |
---|
| 305 | name = u'Course Processor' |
---|
| 306 | iface = ICourse |
---|
| 307 | allowed_roles = Course.local_roles |
---|
| 308 | |
---|
| 309 | location_fields = ['code'] |
---|
| 310 | factory_name = 'waeup.Course' |
---|
| 311 | |
---|
| 312 | mode = None |
---|
| 313 | |
---|
| 314 | def getParent(self, row, site): |
---|
| 315 | if not 'faculties' in site.keys(): |
---|
| 316 | return None |
---|
| 317 | # If both faculty and department codes are provided, use |
---|
| 318 | # these to get parent. |
---|
| 319 | if row.get('faculty_code',None) not in (None, IGNORE_MARKER) and \ |
---|
| 320 | row.get('department_code',None) not in (None, IGNORE_MARKER): |
---|
| 321 | if not row['faculty_code'] in site['faculties'].keys(): |
---|
| 322 | return None |
---|
| 323 | faculty = site['faculties'][row['faculty_code']] |
---|
| 324 | if not row['department_code'] in faculty.keys(): |
---|
| 325 | return None |
---|
| 326 | dept = faculty[row['department_code']] |
---|
| 327 | return dept.courses |
---|
| 328 | # If department code or faculty code is missing, |
---|
| 329 | # use catalog to get parent. Makes only sense in update mode but |
---|
| 330 | # does also work in create mode. |
---|
| 331 | cat = queryUtility(ICatalog, name='courses_catalog') |
---|
| 332 | results = list( |
---|
| 333 | cat.searchResults(code=(row['code'], row['code']))) |
---|
| 334 | if results: |
---|
| 335 | return results[0].__parent__ |
---|
| 336 | return None |
---|
| 337 | |
---|
| 338 | def addEntry(self, obj, row, site): |
---|
| 339 | parent = self.getParent(row, site) |
---|
| 340 | parent.addCourse(obj) |
---|
| 341 | return |
---|
| 342 | |
---|
[8996] | 343 | class CertificateCourseProcessor(FacultyProcessor): |
---|
[5009] | 344 | """A batch processor for ICertificateCourse objects. |
---|
| 345 | """ |
---|
[6628] | 346 | grok.implements(IBatchProcessor) |
---|
[5009] | 347 | grok.provides(IBatchProcessor) |
---|
| 348 | grok.context(Interface) |
---|
[7933] | 349 | util_name = 'certificatecourseprocessor' |
---|
[5009] | 350 | grok.name(util_name) |
---|
| 351 | |
---|
[7933] | 352 | name = u'CertificateCourse Processor' |
---|
[5009] | 353 | iface = ICertificateCourse |
---|
| 354 | |
---|
| 355 | location_fields = ['course', 'level', 'faculty_code', 'department_code', |
---|
| 356 | 'certificate_code',] |
---|
| 357 | factory_name = 'waeup.CertificateCourse' |
---|
| 358 | |
---|
[6628] | 359 | mode = None |
---|
| 360 | |
---|
[5009] | 361 | def parentsExist(self, row, site): |
---|
| 362 | if not 'faculties' in site.keys(): |
---|
| 363 | return False |
---|
| 364 | if not row['faculty_code'] in site['faculties'].keys(): |
---|
| 365 | return False |
---|
| 366 | faculty = site['faculties'][row['faculty_code']] |
---|
| 367 | if not row['department_code'] in faculty.keys(): |
---|
| 368 | return False |
---|
| 369 | dept = faculty[row['department_code']] |
---|
| 370 | return row['certificate_code'] in dept.certificates.keys() |
---|
| 371 | |
---|
| 372 | def entryExists(self, row, site): |
---|
| 373 | if not self.parentsExist(row, site): |
---|
| 374 | return False |
---|
| 375 | parent = self.getParent(row, site) |
---|
| 376 | code = "%s_%s" % (row['course'].code, row['level']) |
---|
| 377 | return code in parent.keys() |
---|
| 378 | |
---|
| 379 | def getParent(self, row, site): |
---|
| 380 | dept = site['faculties'][row['faculty_code']][row['department_code']] |
---|
| 381 | return dept.certificates[row['certificate_code']] |
---|
| 382 | |
---|
| 383 | def getEntry(self, row, site): |
---|
| 384 | if not self.entryExists(row, site): |
---|
| 385 | return None |
---|
| 386 | parent = self.getParent(row, site) |
---|
[6628] | 387 | return parent.get("%s_%s" % (row['course'].code, row['level'])) |
---|
[5009] | 388 | |
---|
| 389 | def addEntry(self, obj, row, site): |
---|
| 390 | parent = self.getParent(row, site) |
---|
[8920] | 391 | parent.addCertCourse(row['course'], |
---|
[7665] | 392 | row['level'], row['mandatory']) |
---|
[5009] | 393 | return |
---|
| 394 | |
---|
| 395 | def delEntry(self, row, site): |
---|
| 396 | parent = self.getParent(row, site) |
---|
[9826] | 397 | parent.delCertCourses(row['course'].code, row['level']) |
---|
[5009] | 398 | return |
---|