- Timestamp:
- 20 Sep 2012, 08:49:37 (12 years ago)
- Location:
- main/waeup.kofa/branches/uli-zc-async
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/branches/uli-zc-async
- Property svn:mergeinfo changed
-
main/waeup.kofa/branches/uli-zc-async/src/waeup/kofa/university/batching.py
r8302 r9209 29 29 from zope.schema import getFields 30 30 from zope.catalog.interfaces import ICatalog 31 from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, FatalCSVError 31 from zope.event import notify 32 from zope.securitypolicy.interfaces import ( 33 IPrincipalRoleManager, IPrincipalRoleMap) 34 from waeup.kofa.authentication import LocalRoleSetEvent 35 from waeup.kofa.interfaces import ( 36 IBatchProcessor, IGNORE_MARKER, DELETION_MARKER, FatalCSVError) 32 37 from waeup.kofa.university.interfaces import ( 33 38 IFacultiesContainer, IFaculty, ICourse, IDepartment, ICertificate, 34 39 ICertificateCourse) 40 from waeup.kofa.university import ( 41 Faculty, Department, Course, Certificate) 35 42 from waeup.kofa.utils.batching import BatchProcessor 36 43 … … 46 53 name = u'Faculty Processor' 47 54 iface = IFaculty 55 allowed_roles = Faculty.local_roles 48 56 49 57 location_fields = ['code',] … … 54 62 def parentsExist(self, row, site): 55 63 return 'faculties' in site.keys() 64 65 @property 66 def available_fields(self): 67 fields = getFields(self.iface) 68 return sorted(list(set( 69 self.location_fields + fields.keys() + ['local_roles'] 70 ))) 56 71 57 72 def entryExists(self, row, site): … … 77 92 pass 78 93 79 class DepartmentProcessor(BatchProcessor): 94 def updateEntry(self, obj, row, site): 95 """Update obj to the values given in row. 96 """ 97 items_changed = '' 98 99 if row.has_key('local_roles') and row['local_roles'] not in ( 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))) 118 row.pop('local_roles') 119 120 # apply other values... 121 items_changed += super(FacultyProcessor, self).updateEntry( 122 obj, row, site) 123 124 # Log actions... 125 location_field = self.location_fields[0] 126 grok.getSite().logger.info('%s - %s - Record updated: %s' 127 % (self.name, row[location_field], items_changed)) 128 return items_changed 129 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) 135 if row.has_key('local_roles'): 136 if row['local_roles'] in (None, DELETION_MARKER, IGNORE_MARKER): 137 return errs, inv_errs, conv_dict 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 165 class DepartmentProcessor(FacultyProcessor): 80 166 """A batch processor for IDepartment objects. 81 167 """ … … 88 174 name = u'Department Processor' 89 175 iface = IDepartment 176 allowed_roles = Department.local_roles 90 177 91 178 location_fields = ['code', 'faculty_code'] … … 124 211 return 125 212 126 class CourseProcessor( BatchProcessor):213 class CourseProcessor(FacultyProcessor): 127 214 """A batch processor for ICourse objects. 128 215 """ … … 135 222 name = u'Course Processor' 136 223 iface = ICourse 224 allowed_roles = Course.local_roles 137 225 138 226 location_fields = ['code', 'faculty_code', 'department_code'] … … 175 263 return 176 264 177 class CertificateProcessor( BatchProcessor):265 class CertificateProcessor(FacultyProcessor): 178 266 """A batch processor for ICertificate objects. 179 267 """ … … 186 274 name = u'Certificate Processor' 187 275 iface = ICertificate 276 allowed_roles = Certificate.local_roles 188 277 189 278 location_fields = ['code'] … … 196 285 fields = getFields(self.iface) 197 286 return sorted(list(set( 198 ['faculty_code','department_code'] + fields.keys()))) 287 ['faculty_code','department_code'] + fields.keys() 288 + ['local_roles']))) 199 289 200 290 def checkHeaders(self, headerfields, mode='create'): … … 269 359 return 270 360 271 class CertificateCourseProcessor( BatchProcessor):361 class CertificateCourseProcessor(FacultyProcessor): 272 362 """A batch processor for ICertificateCourse objects. 273 363 """ … … 317 407 def addEntry(self, obj, row, site): 318 408 parent = self.getParent(row, site) 319 parent.addC ourseRef(row['course'],409 parent.addCertCourse(row['course'], 320 410 row['level'], row['mandatory']) 321 411 return … … 323 413 def delEntry(self, row, site): 324 414 parent = self.getParent(row, site) 325 parent.delC ourseRef(row['course'].code, row['level'])326 return 415 parent.delCertCourse(row['course'].code, row['level']) 416 return
Note: See TracChangeset for help on using the changeset viewer.