Changeset 4222 for waeup


Ignore:
Timestamp:
8 Jun 2009, 16:58:41 (16 years ago)
Author:
uli
Message:

Use new CSV file framework.

File:
1 edited

Legend:

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

    r4161 r4222  
    66from zope.exceptions import DuplicationError
    77from zope.interface import implementedBy
     8from waeup.csvfile import CSVFile
     9from waeup.csvfile.interfaces import ICSVFile
    810from waeup.interfaces import IFacultyContainer, IFaculty, IWAeUPCSVImporter
    911from waeup.utils.importexport import CSVImporter
    1012from waeup.viewlets import MainArea, LeftSidebar, Index, Add, FormWrapMixin
     13
    1114
    1215class FacultyContainer(grok.Container):
     
    4952        return implementedBy(FacultyContainer)
    5053
     54
     55#
     56# CSV import stuff
     57#
     58class IFacultyCSVFile(ICSVFile):
     59    """A CSV file that contains faculty data.
     60    """
     61
     62class FacultyCSVFile(CSVFile):
     63    """An abstraction of a CSV file containing faculties.
     64    """
     65    grok.implements(IFacultyCSVFile)
     66    grok.provides(IFacultyCSVFile)
     67    required_fields = ['code', 'title', 'title_prefix']
     68
    5169class FacultyCSVImporter(CSVImporter):
    52     """Importer to import CSV data into faculty containers.
     70    """Shuffle data from faculty CSV files into faculty containers.
    5371    """
    54     grok.context(FacultyContainer)
     72    # Tell, what kinds of objects we connect...
     73    grok.adapts(IFacultyCSVFile, IFacultyContainer)
     74    # Tell the world, that we are an importer...
    5575    grok.implements(IWAeUPCSVImporter)
    56     datatype = u'Faculty importer'
    57     column_terms = ['code', 'title', 'title_prefix']
     76    grok.provides(IWAeUPCSVImporter)
    5877
    59     def doImport(self, filepath, clear_old_data=True, overwrite=True):
    60         headers, data = self.getData(filepath)
    61         if clear_old_data:
    62             self.context.clear()
    63         keys = self.context.keys()
    64         for item in data:
    65             data_dict = dict(map(None, headers, item))
     78    datatype = u'Faculty Importer'
     79
     80    def doImport(self, clear_old_data=True, overwrite=True):
     81        # CSVImporter instances have a `csvfile` and a `receiver`
     82        # object defined which refer to the CSV file and the container.
     83        for row in self.csvfile.getData():
    6684            new_item = createObject(u'waeup.Faculty')
    67             for key, val in data_dict.items():
    68                setattr(new_item, key, val)
    69             self.context.addFaculty(new_item)
     85            for key, val in row.items():
     86                setattr(new_item, key, val)
     87            self.receiver.addFaculty(new_item)
    7088        return
     89
     90
    7191
    7292
Note: See TracChangeset for help on using the changeset viewer.