Ignore:
Timestamp:
13 Jul 2012, 06:09:32 (12 years ago)
Author:
Henrik Bettermann
Message:

For (re-)importing local roles we first need a reliable conversion checker.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/university
Files:
2 edited

Legend:

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

    r8920 r8994  
    3333    IFacultiesContainer, IFaculty, ICourse, IDepartment, ICertificate,
    3434    ICertificateCourse)
     35from waeup.kofa.university.faculty import Faculty
    3536from waeup.kofa.utils.batching import BatchProcessor
    3637
     
    4647    name = u'Faculty Processor'
    4748    iface = IFaculty
     49    allowed_roles = Faculty.local_roles
    4850
    4951    location_fields = ['code',]
     
    5557        return 'faculties' in site.keys()
    5658
     59    @property
     60    def available_fields(self):
     61        fields = getFields(self.iface)
     62        return sorted(list(set(
     63            ['local_roles'] + fields.keys())))
     64
    5765    def entryExists(self, row, site):
    5866        return row['code'] in site['faculties'].keys()
     
    7684        del parent[row['code']]
    7785        pass
     86
     87    def checkConversion(self, row, mode='create'):
     88        """Validates all values in row.
     89        """
     90        errs, inv_errs, conv_dict =  super(
     91            FacultyProcessor, self).checkConversion(row, mode=mode)
     92        if row.has_key('local_roles'):
     93            try:
     94                local_roles = eval(row['local_roles'])
     95            except:
     96                errs.append(('local_roles','Error'))
     97                return errs, inv_errs, conv_dict
     98            if not isinstance(local_roles, list):
     99                errs.append(('local_roles','no list'))
     100                return errs, inv_errs, conv_dict
     101            for rolemap in local_roles:
     102                if not isinstance(rolemap, dict):
     103                    errs.append(('local_roles','no dicts'))
     104                    return errs, inv_errs, conv_dict
     105                if not 'user_name' in rolemap.keys() or not \
     106                    'local_role' in rolemap.keys():
     107                    errs.append(('local_roles','user_name or local_role missing'))
     108                    return errs, inv_errs, conv_dict
     109                local_role = rolemap['local_role']
     110                if not local_role in self.allowed_roles:
     111                    errs.append(('local_roles','%s not allowed' % local_role))
     112                    return errs, inv_errs, conv_dict
     113                user = rolemap['user_name']
     114                users = grok.getSite()['users']
     115                if not user in users.keys():
     116                    errs.append(('local_roles','%s does not exist' % user))
     117                    return errs, inv_errs, conv_dict
     118        return errs, inv_errs, conv_dict
    78119
    79120class DepartmentProcessor(BatchProcessor):
  • main/waeup.kofa/trunk/src/waeup/kofa/university/tests/test_batching.py

    r8920 r8994  
    1919# Tests for university related batching
    2020import unittest
    21 
     21import tempfile
     22import shutil
     23from zope.component.hooks import setSite, clearSite
    2224from zope.interface.verify import verifyClass, verifyObject
    2325from waeup.kofa.interfaces import IBatchProcessor
    2426from waeup.kofa.testing import FunctionalTestCase, FunctionalLayer
     27from waeup.kofa.app import University
    2528from waeup.kofa.university.batching import (
    2629    FacultyProcessor, DepartmentProcessor, CourseProcessor,
     
    2932from waeup.kofa.university.course import Course
    3033from waeup.kofa.university.department import Department
    31 
    32 
    33 class TestFacultyProcessor(unittest.TestCase):
     34from waeup.kofa.university.batching import FacultyProcessor
     35
     36
     37class TestFacultyProcessor(FunctionalTestCase):
     38
     39    layer = FunctionalLayer
    3440
    3541    def setUp(self):
     42        super(TestFacultyProcessor, self).setUp()
     43        self.dc_root = tempfile.mkdtemp()
     44        self.workdir = tempfile.mkdtemp()
     45        app = University()
     46        self.getRootFolder()['app'] = app
     47        self.app = self.getRootFolder()['app']
     48        setSite(app)
     49
     50        self.app['users'].addUser('bob', 'bobssecret')
     51
    3652        self.proc = FacultyProcessor()
    3753        self.site1 = dict(faculties=dict())
    3854        self.site2 = dict(faculties=dict(FAC='pseudo faculty'))
    3955        self.row = dict(code='FAC')
     56        self.processor = FacultyProcessor()
     57        return
     58
     59    def tearDown(self):
     60        super(TestFacultyProcessor, self).tearDown()
     61        shutil.rmtree(self.workdir)
     62        shutil.rmtree(self.dc_root)
     63        clearSite()
    4064        return
    4165
     
    5882        self.proc.delEntry(self.row, self.site2)
    5983        self.assertTrue('FAC' not in self.site2.keys())
     84        return
     85
     86    def test_checkConversion(self):
     87        # Make sure we can check conversions.
     88        errs, inv_errs, conv_dict = self.processor.checkConversion(
     89            dict(faculty_code='ABC', local_roles='[]'))
     90        self.assertEqual(len(errs),0)
     91
     92        errs, inv_errs, conv_dict = self.processor.checkConversion(
     93            dict(faculty_code='ABC',
     94            local_roles="['nonsense'"
     95            ))
     96        self.assertEqual(len(errs),1)
     97        self.assertEqual(errs, [('local_roles', 'Error')])
     98        errs, inv_errs, conv_dict = self.processor.checkConversion(
     99            dict(faculty_code='ABC',
     100            local_roles="('abc')"
     101            ))
     102        self.assertEqual(len(errs),1)
     103        self.assertEqual(errs, [('local_roles', 'no list')])
     104        errs, inv_errs, conv_dict = self.processor.checkConversion(
     105            dict(faculty_code='ABC',
     106            local_roles="[('ABC')]"
     107            ))
     108        self.assertEqual(len(errs),1)
     109        self.assertEqual(errs, [('local_roles', 'no dicts')])
     110        errs, inv_errs, conv_dict = self.processor.checkConversion(
     111            dict(faculty_code='ABC',
     112            local_roles="('abc')"
     113            ))
     114        self.assertEqual(len(errs),1)
     115        self.assertEqual(errs, [('local_roles', 'no list')])
     116        errs, inv_errs, conv_dict = self.processor.checkConversion(
     117            dict(faculty_code='ABC', local_roles=
     118            "[{'name':'bob','local_role':'waeup.local.DepartmentManager'},]"
     119            ))
     120        self.assertEqual(len(errs),1)
     121        self.assertEqual(errs, [('local_roles', 'user_name or local_role missing')])
     122        errs, inv_errs, conv_dict = self.processor.checkConversion(
     123            dict(faculty_code='ABC', local_roles=
     124            "[{'user_name':'bob','localrole':'waeup.local.DepartmentManager'},]"
     125            ))
     126        self.assertEqual(len(errs),1)
     127        self.assertEqual(errs, [('local_roles', 'user_name or local_role missing')])
     128        errs, inv_errs, conv_dict = self.processor.checkConversion(
     129            dict(faculty_code='ABC', local_roles=
     130            "[{'user_name':'bob','local_role':'waeup.local.Boss'},]"
     131            ))
     132        self.assertEqual(len(errs),1)
     133        self.assertEqual(errs, [('local_roles', 'waeup.local.Boss not allowed')])
     134        errs, inv_errs, conv_dict = self.processor.checkConversion(
     135            dict(faculty_code='ABC', local_roles=
     136            "[{'user_name':'john','local_role':'waeup.local.DepartmentManager'},]"
     137            ))
     138        self.assertEqual(len(errs),1)
     139        self.assertEqual(errs, [('local_roles', 'john does not exist')])
     140        errs, inv_errs, conv_dict = self.processor.checkConversion(
     141            dict(faculty_code='ABC', local_roles=
     142            "[{'user_name':'bob','local_role':'waeup.local.DepartmentManager'},]"
     143            ))
     144        self.assertEqual(len(errs),0)
    60145        return
    61146
Note: See TracChangeset for help on using the changeset viewer.