Ignore:
Timestamp:
14 Oct 2012, 04:54:11 (12 years ago)
Author:
Henrik Bettermann
Message:

Reorganize course batch processing. Use courses_catalog for getting entry in update mode.

File:
1 edited

Legend:

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

    r9087 r9333  
    211211        return
    212212
    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 
    265213class CertificateProcessor(FacultyProcessor):
    266214    """A batch processor for ICertificate objects.
     
    289237
    290238    def checkHeaders(self, headerfields, mode='create'):
    291         req = self.req[mode]
    292         # Check for required fields...
    293         for field in req:
    294             if not field in headerfields:
    295                 raise FatalCSVError(
    296                     "Need at least columns %s for import!" %
    297                     ', '.join(["'%s'" % x for x in req]))
    298         # Check for double fields. Cannot happen because this error is
    299         # already catched in views
    300         not_ignored_fields = [x for x in headerfields
    301                               if not x.startswith('--')]
    302         if len(set(not_ignored_fields)) < len(not_ignored_fields):
    303             raise FatalCSVError(
    304                 "Double headers: each column name may only appear once.")
     239        super(CertificateProcessor, self).checkHeaders(headerfields, mode)
    305240        if mode == 'create':
    306241            if not 'faculty_code' in headerfields \
     
    359294        return
    360295
     296class CourseProcessor(CertificateProcessor):
     297    """A batch processor for ICourse objects.
     298    """
     299    grok.implements(IBatchProcessor)
     300    grok.provides(IBatchProcessor)
     301    grok.context(Interface)
     302    util_name = 'courseprocessor'
     303    grok.name(util_name)
     304
     305    name = u'Course Processor'
     306    iface = ICourse
     307    allowed_roles = Course.local_roles
     308
     309    location_fields = ['code']
     310    factory_name = 'waeup.Course'
     311
     312    mode = None
     313
     314    def getParent(self, row, site):
     315        if not 'faculties' in site.keys():
     316            return None
     317        # If both faculty and department codes are provided, use
     318        # these to get parent.
     319        if row.get('faculty_code',None) not in (None, IGNORE_MARKER) and \
     320            row.get('department_code',None) not in (None, IGNORE_MARKER):
     321            if not row['faculty_code'] in site['faculties'].keys():
     322                return None
     323            faculty = site['faculties'][row['faculty_code']]
     324            if not row['department_code'] in faculty.keys():
     325                return None
     326            dept = faculty[row['department_code']]
     327            return dept.courses
     328        # If department code or faculty code is missing,
     329        # use catalog to get parent. Makes only sense in update mode but
     330        # does also work in create mode.
     331        cat = queryUtility(ICatalog, name='courses_catalog')
     332        results = list(
     333            cat.searchResults(code=(row['code'], row['code'])))
     334        if results:
     335            return results[0].__parent__
     336        return None
     337
     338    def addEntry(self, obj, row, site):
     339        parent = self.getParent(row, site)
     340        parent.addCourse(obj)
     341        return
     342
    361343class CertificateCourseProcessor(FacultyProcessor):
    362344    """A batch processor for ICertificateCourse objects.
Note: See TracChangeset for help on using the changeset viewer.