Changeset 4329


Ignore:
Timestamp:
21 Jun 2009, 01:07:03 (16 years ago)
Author:
uli
Message:

Add importer for CSV certificate files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/university/facultycontainer.py

    r4279 r4329  
    158158                setattr(new_item, key, val)
    159159            dept.courses.addCourse(new_item)
     160        return
     161
     162class ICertificateCSVFile(ICSVFile):
     163    """A CSV file that contains certificate data.
     164    """
     165
     166class CertificateCSVFile(CSVFile):
     167    """An abstraction of a CSV file containing certificates.
     168    """
     169    grok.implements(ICertificateCSVFile)
     170    grok.provides(ICertificateCSVFile)
     171    required_fields = ['code', 'title', 'faculty_code', 'department_code',
     172                       'category', 'study_mode', 'start_level', 'end_level',
     173                       'm_prefix', 'max_pass', 'application_category']
     174
     175class CertificateCSVImporter(CSVImporter):
     176    """Shuffle data from certificate CSV files into faculty containers.
     177    """
     178    # Tell, what kinds of objects we connect...
     179    grok.adapts(ICertificateCSVFile, IFacultyContainer)
     180    # Tell the world, that we are an importer...
     181    grok.implements(IWAeUPCSVImporter)
     182    grok.provides(IWAeUPCSVImporter)
     183
     184    datatype = u'Certificate Importer'
     185
     186    def doImport(self, clear_old_data=True, overwrite=True):
     187        # CSVImporter instances have a `csvfile` and a `receiver`
     188        # object defined which refer to the CSV file and the container.
     189        for row in self.csvfile.getData():
     190
     191            new_item = createObject(u'waeup.Certificate')
     192           
     193            faculty_code = row['faculty_code']
     194            faculty = self.receiver[faculty_code]
     195            del row['faculty_code']
     196
     197            dept_code = row['department_code']
     198            dept = faculty[dept_code]
     199            del row['department_code']
     200
     201            for key, val in row.items():
     202                setattr(new_item, key, val)
     203            dept.certificates.addCertificate(new_item)
    160204        return
    161205
Note: See TracChangeset for help on using the changeset viewer.