Changeset 8302


Ignore:
Timestamp:
28 Apr 2012, 07:58:39 (13 years ago)
Author:
Henrik Bettermann
Message:

Use certificates_catalog when importing certificates in update mode.

The certificate updater is untested because batching tests in the university package are unit tests and catalogs are not available.
I suggest to rewrite w.k.university.tests.test_batching and remove w.k.browser.batchprocessing.

File:
1 edited

Legend:

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

    r7933 r8302  
    2626import grok
    2727from zope.interface import Interface
    28 from waeup.kofa.interfaces import IBatchProcessor
     28from zope.component import queryUtility
     29from zope.schema import getFields
     30from zope.catalog.interfaces import ICatalog
     31from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, FatalCSVError
    2932from waeup.kofa.university.interfaces import (
    3033    IFacultiesContainer, IFaculty, ICourse, IDepartment, ICertificate,
     
    184187    iface = ICertificate
    185188
    186     location_fields = ['code', 'faculty_code', 'department_code']
     189    location_fields = ['code']
    187190    factory_name = 'waeup.Certificate'
    188191
    189192    mode = None
    190193
    191     def parentsExist(self, row, site):
     194    @property
     195    def available_fields(self):
     196        fields = getFields(self.iface)
     197        return sorted(list(set(
     198            ['faculty_code','department_code'] + fields.keys())))
     199
     200    def checkHeaders(self, headerfields, mode='create'):
     201        req = self.req[mode]
     202        # Check for required fields...
     203        for field in req:
     204            if not field in headerfields:
     205                raise FatalCSVError(
     206                    "Need at least columns %s for import!" %
     207                    ', '.join(["'%s'" % x for x in req]))
     208        # Check for double fields. Cannot happen because this error is
     209        # already catched in views
     210        not_ignored_fields = [x for x in headerfields
     211                              if not x.startswith('--')]
     212        if len(set(not_ignored_fields)) < len(not_ignored_fields):
     213            raise FatalCSVError(
     214                "Double headers: each column name may only appear once.")
     215        if mode == 'create':
     216            if not 'faculty_code' in headerfields \
     217                and not 'department_code' in headerfields :
     218                raise FatalCSVError(
     219                    "Need at least columns faculty_code and department_code")
     220        return True
     221
     222    def parentsExist(self, row, site):
     223        return self.getParent(row,site) is not None
     224
     225    def entryExists(self, row, site):
     226        parent = self.getParent(row, site)
     227        if parent is not None:
     228            return row['code'] in parent.keys()
     229        return False
     230
     231    def getParent(self, row, site):
    192232        if not 'faculties' in site.keys():
    193             return False
    194         if not row['faculty_code'] in site['faculties'].keys():
    195             return False
    196         faculty = site['faculties'][row['faculty_code']]
    197         return row['department_code'] in faculty.keys()
    198 
    199     def entryExists(self, row, site):
    200         if not self.parentsExist(row, site):
    201             return False
    202         parent = self.getParent(row, site)
    203         return row['code'] in parent.keys()
    204 
    205     def getParent(self, row, site):
    206         dept = site['faculties'][row['faculty_code']][row['department_code']]
    207         return dept.certificates
    208 
    209     def getEntry(self, row, site):
    210         if not self.entryExists(row, site):
    211             return None
    212         parent = self.getParent(row, site)
    213         return parent.get(row['code'])
     233            return None
     234        # If both faculty and department codes are provided, use
     235        # these to get parent.
     236        if row.get('faculty_code',None) not in (None, IGNORE_MARKER) and \
     237            row.get('department_code',None) not in (None, IGNORE_MARKER):
     238            if not row['faculty_code'] in site['faculties'].keys():
     239                return None
     240            faculty = site['faculties'][row['faculty_code']]
     241            if not row['department_code'] in faculty.keys():
     242                return None
     243            dept = faculty[row['department_code']]
     244            return dept.certificates
     245        # If department code or faculty code is missing,
     246        # use catalog to get parent. Makes only sense in update mode but
     247        # does also work in create mode.
     248        cat = queryUtility(ICatalog, name='certificates_catalog')
     249        results = list(
     250            cat.searchResults(code=(row['code'], row['code'])))
     251        if results:
     252            return results[0].__parent__
     253        return None
     254
     255    def getEntry(self, row, site):
     256        parent = self.getParent(row, site)
     257        if parent is not None:
     258            return parent.get(row['code'])
     259        return None
    214260
    215261    def addEntry(self, obj, row, site):
Note: See TracChangeset for help on using the changeset viewer.