Ignore:
Timestamp:
21 Sep 2012, 08:19:35 (12 years ago)
Author:
uli
Message:

Rollback r9209. Looks like multiple merges from trunk confuse svn when merging back into trunk.

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

    r9209 r9211  
    2929from zope.schema import getFields
    3030from zope.catalog.interfaces import ICatalog
    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)
     31from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, FatalCSVError
    3732from waeup.kofa.university.interfaces import (
    3833    IFacultiesContainer, IFaculty, ICourse, IDepartment, ICertificate,
    3934    ICertificateCourse)
    40 from waeup.kofa.university import (
    41     Faculty, Department, Course, Certificate)
    4235from waeup.kofa.utils.batching import BatchProcessor
    4336
     
    5346    name = u'Faculty Processor'
    5447    iface = IFaculty
    55     allowed_roles = Faculty.local_roles
    5648
    5749    location_fields = ['code',]
     
    6254    def parentsExist(self, row, site):
    6355        return 'faculties' in site.keys()
     56
     57    def entryExists(self, row, site):
     58        return row['code'] in site['faculties'].keys()
     59
     60    def getParent(self, row, site):
     61        return site['faculties']
     62
     63    def getEntry(self, row, site):
     64        if not self.entryExists(row, site):
     65            return None
     66        parent = self.getParent(row, site)
     67        return parent.get(row['code'])
     68
     69    def addEntry(self, obj, row, site):
     70        parent = self.getParent(row, site)
     71        parent.addFaculty(obj)
     72        return
     73
     74    def delEntry(self, row, site):
     75        parent = self.getParent(row, site)
     76        del parent[row['code']]
     77        pass
     78
     79class DepartmentProcessor(BatchProcessor):
     80    """A batch processor for IDepartment objects.
     81    """
     82    grok.implements(IBatchProcessor)
     83    grok.provides(IBatchProcessor)
     84    grok.context(Interface)
     85    util_name = 'departmentprocessor'
     86    grok.name(util_name)
     87
     88    name = u'Department Processor'
     89    iface = IDepartment
     90
     91    location_fields = ['code', 'faculty_code']
     92    factory_name = 'waeup.Department'
     93
     94    mode = None
     95
     96    def parentsExist(self, row, site):
     97        if not 'faculties' in site.keys():
     98            return False
     99        return row['faculty_code'] in site['faculties']
     100
     101    def entryExists(self, row, site):
     102        if not self.parentsExist(row, site):
     103            return False
     104        parent = self.getParent(row, site)
     105        return row['code'] in parent.keys()
     106
     107    def getParent(self, row, site):
     108        return site['faculties'][row['faculty_code']]
     109
     110    def getEntry(self, row, site):
     111        if not self.entryExists(row, site):
     112            return None
     113        parent = self.getParent(row, site)
     114        return parent.get(row['code'])
     115
     116    def addEntry(self, obj, row, site):
     117        parent = self.getParent(row, site)
     118        parent.addDepartment(obj)
     119        return
     120
     121    def delEntry(self, row, site):
     122        parent = self.getParent(row, site)
     123        del parent[row['code']]
     124        return
     125
     126class CourseProcessor(BatchProcessor):
     127    """A batch processor for ICourse objects.
     128    """
     129    grok.implements(IBatchProcessor)
     130    grok.provides(IBatchProcessor)
     131    grok.context(Interface)
     132    util_name = 'courseprocessor'
     133    grok.name(util_name)
     134
     135    name = u'Course Processor'
     136    iface = ICourse
     137
     138    location_fields = ['code', 'faculty_code', 'department_code']
     139    factory_name = 'waeup.Course'
     140
     141    mode = None
     142
     143    def parentsExist(self, row, site):
     144        if not 'faculties' in site.keys():
     145            return False
     146        if not row['faculty_code'] in site['faculties'].keys():
     147            return False
     148        faculty = site['faculties'][row['faculty_code']]
     149        return row['department_code'] in faculty.keys()
     150
     151    def entryExists(self, row, site):
     152        if not self.parentsExist(row, site):
     153            return False
     154        parent = self.getParent(row, site)
     155        return row['code'] in parent.keys()
     156
     157    def getParent(self, row, site):
     158        dept = site['faculties'][row['faculty_code']][row['department_code']]
     159        return dept.courses
     160
     161    def getEntry(self, row, site):
     162        if not self.entryExists(row, site):
     163            return None
     164        parent = self.getParent(row, site)
     165        return parent.get(row['code'])
     166
     167    def addEntry(self, obj, row, site):
     168        parent = self.getParent(row, site)
     169        parent.addCourse(obj)
     170        return
     171
     172    def delEntry(self, row, site):
     173        parent = self.getParent(row, site)
     174        del parent[row['code']]
     175        return
     176
     177class CertificateProcessor(BatchProcessor):
     178    """A batch processor for ICertificate objects.
     179    """
     180    grok.implements(IBatchProcessor)
     181    grok.provides(IBatchProcessor)
     182    grok.context(Interface)
     183    util_name = 'certificateprocessor'
     184    grok.name(util_name)
     185
     186    name = u'Certificate Processor'
     187    iface = ICertificate
     188
     189    location_fields = ['code']
     190    factory_name = 'waeup.Certificate'
     191
     192    mode = None
    64193
    65194    @property
     
    67196        fields = getFields(self.iface)
    68197        return sorted(list(set(
    69             self.location_fields + fields.keys() + ['local_roles']
    70             )))
    71 
    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'])
    83 
    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 
    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):
    166     """A batch processor for IDepartment objects.
    167     """
    168     grok.implements(IBatchProcessor)
    169     grok.provides(IBatchProcessor)
    170     grok.context(Interface)
    171     util_name = 'departmentprocessor'
    172     grok.name(util_name)
    173 
    174     name = u'Department Processor'
    175     iface = IDepartment
    176     allowed_roles = Department.local_roles
    177 
    178     location_fields = ['code', 'faculty_code']
    179     factory_name = 'waeup.Department'
    180 
    181     mode = None
    182 
    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'])
    202 
    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 
    213 class CourseProcessor(FacultyProcessor):
    214     """A batch processor for ICourse objects.
    215     """
    216     grok.implements(IBatchProcessor)
    217     grok.provides(IBatchProcessor)
    218     grok.context(Interface)
    219     util_name = 'courseprocessor'
    220     grok.name(util_name)
    221 
    222     name = u'Course Processor'
    223     iface = ICourse
    224     allowed_roles = Course.local_roles
    225 
    226     location_fields = ['code', 'faculty_code', 'department_code']
    227     factory_name = 'waeup.Course'
    228 
    229     mode = None
    230 
    231     def parentsExist(self, row, site):
    232         if not 'faculties' in site.keys():
    233             return False
    234         if not row['faculty_code'] in site['faculties'].keys():
    235             return False
    236         faculty = site['faculties'][row['faculty_code']]
    237         return row['department_code'] in faculty.keys()
    238 
    239     def entryExists(self, row, site):
    240         if not self.parentsExist(row, site):
    241             return False
    242         parent = self.getParent(row, site)
    243         return row['code'] in parent.keys()
    244 
    245     def getParent(self, row, site):
    246         dept = site['faculties'][row['faculty_code']][row['department_code']]
    247         return dept.courses
    248 
    249     def getEntry(self, row, site):
    250         if not self.entryExists(row, site):
    251             return None
    252         parent = self.getParent(row, site)
    253         return parent.get(row['code'])
    254 
    255     def addEntry(self, obj, row, site):
    256         parent = self.getParent(row, site)
    257         parent.addCourse(obj)
    258         return
    259 
    260     def delEntry(self, row, site):
    261         parent = self.getParent(row, site)
    262         del parent[row['code']]
    263         return
    264 
    265 class CertificateProcessor(FacultyProcessor):
    266     """A batch processor for ICertificate objects.
    267     """
    268     grok.implements(IBatchProcessor)
    269     grok.provides(IBatchProcessor)
    270     grok.context(Interface)
    271     util_name = 'certificateprocessor'
    272     grok.name(util_name)
    273 
    274     name = u'Certificate Processor'
    275     iface = ICertificate
    276     allowed_roles = Certificate.local_roles
    277 
    278     location_fields = ['code']
    279     factory_name = 'waeup.Certificate'
    280 
    281     mode = None
    282 
    283     @property
    284     def available_fields(self):
    285         fields = getFields(self.iface)
    286         return sorted(list(set(
    287             ['faculty_code','department_code'] + fields.keys()
    288             + ['local_roles'])))
     198            ['faculty_code','department_code'] + fields.keys())))
    289199
    290200    def checkHeaders(self, headerfields, mode='create'):
     
    359269        return
    360270
    361 class CertificateCourseProcessor(FacultyProcessor):
     271class CertificateCourseProcessor(BatchProcessor):
    362272    """A batch processor for ICertificateCourse objects.
    363273    """
     
    407317    def addEntry(self, obj, row, site):
    408318        parent = self.getParent(row, site)
    409         parent.addCertCourse(row['course'],
     319        parent.addCourseRef(row['course'],
    410320                            row['level'], row['mandatory'])
    411321        return
     
    413323    def delEntry(self, row, site):
    414324        parent = self.getParent(row, site)
    415         parent.delCertCourse(row['course'].code, row['level'])
    416         return
     325        parent.delCourseRef(row['course'].code, row['level'])
     326        return
Note: See TracChangeset for help on using the changeset viewer.