Ignore:
Timestamp:
16 Jan 2010, 13:07:56 (15 years ago)
Author:
uli
Message:
  • Add batch processor for certificates.
  • Fix required_fields: if a field provides a meaningfull default (i.e. != missing_value), the field is not considered 'required' anymore.
  • Add some docstring.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-importers/src/waeup/utils/batching.py

    r4821 r4829  
    1414from zope.schema.interfaces import IText
    1515from waeup.interfaces import (IImporter, IFacultyContainer, IFaculty, ICourse,
    16                               IDepartment, ISchemaTypeConverter, FatalCSVError)
     16                              IDepartment, ICertificate, ISchemaTypeConverter,
     17                              FatalCSVError)
    1718
    1819class BatchProcessor(grok.GlobalUtility):
     
    4647    @property
    4748    def required_fields(self):
     49        """Required fields that have no default.
     50
     51        A list of names of field, whose value cannot be set if not
     52        given during creation. Therefore these fields must exist in
     53        input.
     54
     55        Fields with a default != missing_value do not belong to this
     56        category.
     57        """
    4858        result = []
    4959        for key, field in getFields(self.iface).items():
    5060            if key in self.location_fields:
     61                continue
     62            if field.default is not field.missing_value:
    5163                continue
    5264            if field.required:
     
    162174
    163175    def updateEntry(self, obj, row, site):
    164         """Update entry given by ``row`` with data delivered therein.
    165         """
    166         raise NotImplementedError('method not implemented')
     176        for key, value in row.items():
     177            setattr(obj, key, value)
     178        return
    167179
    168180    def createLogfile(self, path, fail_path, num, warnings, mode, user):
     
    252264
    253265class FacultyImporter(BatchProcessor):
     266    """A batch processor for IFaculty objects.
     267    """
    254268    grok.provides(IImporter)
    255269    grok.context(Interface)
     
    288302        pass
    289303
    290     def updateEntry(self, obj, row, site):
    291         for key, value in row.items():
    292             setattr(obj, key, value)
    293         pass
    294 
    295304class DepartmentImporter(BatchProcessor):
     305    """A batch processor for IDepartment objects.
     306    """
    296307    grok.provides(IImporter)
    297308    grok.context(Interface)
     
    335346        return
    336347
    337     def updateEntry(self, obj, row, site):
    338         for key, value in row.items():
    339             setattr(obj, key, value)
    340         return
    341 
    342348class CourseImporter(BatchProcessor):
     349    """A batch processor for ICourse objects.
     350    """
    343351    grok.provides(IImporter)
    344352    grok.context(Interface)
     
    386394        return
    387395
    388     def updateEntry(self, obj, row, site):
    389         for key, value in row.items():
    390             setattr(obj, key, value)
    391         return
    392 
     396class CertificateImporter(BatchProcessor):
     397    """A batch processor for ICertificate objects.
     398    """
     399    grok.provides(IImporter)
     400    grok.context(Interface)
     401    util_name = 'certificateimporter'
     402    grok.name(util_name)
     403
     404    name = u'Certificate importer'
     405    iface = ICertificate
     406
     407    location_fields = ['code', 'faculty_code', 'department_code']
     408    factory_name = 'waeup.Certificate'
     409
     410    def parentsExist(self, row, site):
     411        if not 'faculties' in site.keys():
     412            return False
     413        if not row['faculty_code'] in site['faculties'].keys():
     414            return False
     415        faculty = site['faculties'][row['faculty_code']]
     416        return row['department_code'] in faculty.keys()
     417
     418    def entryExists(self, row, site):
     419        if not self.parentsExist(row, site):
     420            return False
     421        parent = self.getParent(row, site)
     422        return row['code'] in parent.keys()
     423
     424    def getParent(self, row, site):
     425        dept = site['faculties'][row['faculty_code']][row['department_code']]
     426        return dept.certificates
     427
     428    def getEntry(self, row, site):
     429        if not self.entryExists(row, site):
     430            return None
     431        parent = self.getParent(row, site)
     432        return parent.get(row['code'])
     433
     434    def addEntry(self, obj, row, site):
     435        parent = self.getParent(row, site)
     436        parent.addCertificate(obj)
     437        return
     438
     439    def delEntry(self, row, site):
     440        parent = self.getParent(row, site)
     441        del parent[row['code']]
     442        return
Note: See TracChangeset for help on using the changeset viewer.