Ignore:
Timestamp:
13 Jul 2012, 08:18:02 (12 years ago)
Author:
Henrik Bettermann
Message:

Customize updateEntry method of FacultyProcessor? to import local_roles.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/batching.py

    r8942 r8995  
    223223        parent = self.getParent(row, site)
    224224        if hasattr(obj,'student_id'):
    225             # Update mode: the student exists and we can get the student_id
     225            # Update mode: the student exists and we can get the student_id.
     226            # Create mode: the record contains the student_id
    226227            parent.logger.info(
    227228                '%s - Student record updated: %s'
  • main/waeup.kofa/trunk/src/waeup/kofa/university/batching.py

    r8994 r8995  
    2929from zope.schema import getFields
    3030from zope.catalog.interfaces import ICatalog
     31from zope.event import notify
     32from zope.securitypolicy.interfaces import IPrincipalRoleManager
     33from waeup.kofa.authentication import LocalRoleSetEvent
    3134from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, FatalCSVError
    3235from waeup.kofa.university.interfaces import (
     
    8487        del parent[row['code']]
    8588        pass
     89
     90    def updateEntry(self, obj, row, site):
     91        """Update obj to the values given in row.
     92        """
     93        items_changed = ''
     94
     95        if row.has_key('local_roles'):
     96            local_roles = eval(row['local_roles'])
     97            for rolemap in local_roles:
     98                user = rolemap['user_name']
     99                local_role = rolemap['local_role']
     100                role_manager = IPrincipalRoleManager(obj)
     101                role_manager.assignRoleToPrincipal(local_role, user)
     102                notify(LocalRoleSetEvent(obj, local_role, user, granted=True))
     103                items_changed += (
     104                    '%s=%s, ' % ('local_roles', '%s|%s' % (user,local_role)))
     105            row.pop('local_roles')
     106
     107        # apply other values...
     108        items_changed += super(FacultyProcessor, self).updateEntry(
     109            obj, row, site)
     110
     111        # Log actions...
     112        grok.getSite().logger.info('%s - %s - Record updated: %s'
     113            % (self.name, self.location_fields[0], items_changed))
     114        return items_changed
    86115
    87116    def checkConversion(self, row, mode='create'):
  • main/waeup.kofa/trunk/src/waeup/kofa/university/tests/test_batching.py

    r8994 r8995  
    2121import tempfile
    2222import shutil
     23import os
    2324from zope.component.hooks import setSite, clearSite
     25from zope.securitypolicy.interfaces import IPrincipalRoleMap
    2426from zope.interface.verify import verifyClass, verifyObject
    2527from waeup.kofa.interfaces import IBatchProcessor
     
    3436from waeup.kofa.university.batching import FacultyProcessor
    3537
     38FACULTY_SAMPLE_DATA = open(
     39    os.path.join(os.path.dirname(__file__), 'sample_faculty_data.csv'),
     40    'rb').read()
     41
     42FACULTY_HEADER_FIELDS = FACULTY_SAMPLE_DATA.split(
     43    '\n')[0].split(',')
    3644
    3745class TestFacultyProcessor(FunctionalTestCase):
     
    5462        self.site2 = dict(faculties=dict(FAC='pseudo faculty'))
    5563        self.row = dict(code='FAC')
     64
    5665        self.processor = FacultyProcessor()
     66        self.csv_file_faculty = os.path.join(self.workdir, 'sample_faculty_data.csv')
     67        open(self.csv_file_faculty, 'wb').write(FACULTY_SAMPLE_DATA)
    5768        return
    5869
     
    144155        self.assertEqual(len(errs),0)
    145156        return
     157
     158    def test_import(self):
     159        num, num_warns, fin_file, fail_file = self.processor.doImport(
     160            self.csv_file_faculty, FACULTY_HEADER_FIELDS)
     161        content = open(fail_file).read()
     162        self.assertEqual(num_warns,5)
     163        self.assertEqual(
     164            content,
     165            'code,local_roles,--ERRORS--\r\n'
     166            'CDE,"[{\'user_name\':\'alice\',\'local_role\':\'waeup.local.DepartmentManager\'}]",'
     167            'local_roles: alice does not exist\r\n'
     168            'DEF,"[{\'user_name\':\'bob\',\'local_role\':\'waeup.local.Boss\'}]",'
     169            'local_roles: waeup.local.Boss not allowed\r\n'
     170            'EFG,[(\'anything\')],local_roles: no dicts\r\n'
     171            'FGH,[,local_roles: Error\r\n'
     172            'GHI,"[{\'user\':\'bob\',\'local\':\'waeup.local.DepartmentManager\'}]",'
     173            'local_roles: user_name or local_role missing\r\n'
     174            )
     175        # Bob got a local role in faculty ABC.
     176        abc = self.app['faculties']['ABC']
     177        role_map = IPrincipalRoleMap(abc)
     178        local_role, user_name, setting = role_map.getPrincipalsAndRoles()[0]
     179        self.assertEqual(user_name, 'bob')
     180        self.assertEqual(local_role, 'waeup.local.DepartmentManager')
     181        shutil.rmtree(os.path.dirname(fin_file))
    146182
    147183class TestDepartmentProcessors(unittest.TestCase):
Note: See TracChangeset for help on using the changeset viewer.