Ignore:
Timestamp:
20 Sep 2012, 08:49:37 (12 years ago)
Author:
uli
Message:

Merge changes from update branch (includes trunk changes until r9107).

Location:
main/waeup.kofa/branches/uli-zc-async
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/branches/uli-zc-async

  • main/waeup.kofa/branches/uli-zc-async/src/waeup/kofa/university/batching.py

    r8302 r9209  
    2929from zope.schema import getFields
    3030from zope.catalog.interfaces import ICatalog
    31 from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, FatalCSVError
     31from zope.event import notify
     32from zope.securitypolicy.interfaces import (
     33    IPrincipalRoleManager, IPrincipalRoleMap)
     34from waeup.kofa.authentication import LocalRoleSetEvent
     35from waeup.kofa.interfaces import (
     36    IBatchProcessor, IGNORE_MARKER, DELETION_MARKER, FatalCSVError)
    3237from waeup.kofa.university.interfaces import (
    3338    IFacultiesContainer, IFaculty, ICourse, IDepartment, ICertificate,
    3439    ICertificateCourse)
     40from waeup.kofa.university import (
     41    Faculty, Department, Course, Certificate)
    3542from waeup.kofa.utils.batching import BatchProcessor
    3643
     
    4653    name = u'Faculty Processor'
    4754    iface = IFaculty
     55    allowed_roles = Faculty.local_roles
    4856
    4957    location_fields = ['code',]
     
    5462    def parentsExist(self, row, site):
    5563        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            )))
    5671
    5772    def entryExists(self, row, site):
     
    7792        pass
    7893
    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
     165class DepartmentProcessor(FacultyProcessor):
    80166    """A batch processor for IDepartment objects.
    81167    """
     
    88174    name = u'Department Processor'
    89175    iface = IDepartment
     176    allowed_roles = Department.local_roles
    90177
    91178    location_fields = ['code', 'faculty_code']
     
    124211        return
    125212
    126 class CourseProcessor(BatchProcessor):
     213class CourseProcessor(FacultyProcessor):
    127214    """A batch processor for ICourse objects.
    128215    """
     
    135222    name = u'Course Processor'
    136223    iface = ICourse
     224    allowed_roles = Course.local_roles
    137225
    138226    location_fields = ['code', 'faculty_code', 'department_code']
     
    175263        return
    176264
    177 class CertificateProcessor(BatchProcessor):
     265class CertificateProcessor(FacultyProcessor):
    178266    """A batch processor for ICertificate objects.
    179267    """
     
    186274    name = u'Certificate Processor'
    187275    iface = ICertificate
     276    allowed_roles = Certificate.local_roles
    188277
    189278    location_fields = ['code']
     
    196285        fields = getFields(self.iface)
    197286        return sorted(list(set(
    198             ['faculty_code','department_code'] + fields.keys())))
     287            ['faculty_code','department_code'] + fields.keys()
     288            + ['local_roles'])))
    199289
    200290    def checkHeaders(self, headerfields, mode='create'):
     
    269359        return
    270360
    271 class CertificateCourseProcessor(BatchProcessor):
     361class CertificateCourseProcessor(FacultyProcessor):
    272362    """A batch processor for ICertificateCourse objects.
    273363    """
     
    317407    def addEntry(self, obj, row, site):
    318408        parent = self.getParent(row, site)
    319         parent.addCourseRef(row['course'],
     409        parent.addCertCourse(row['course'],
    320410                            row['level'], row['mandatory'])
    321411        return
     
    323413    def delEntry(self, row, site):
    324414        parent = self.getParent(row, site)
    325         parent.delCourseRef(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.