Ignore:
Timestamp:
24 May 2009, 10:12:58 (16 years ago)
Author:
uli
Message:

Add facultyimporter, use new faculty factory, fix container counter.

File:
1 edited

Legend:

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

    r4132 r4161  
    33from zope.component.factory import Factory
    44from zope.component.interfaces import Invalid, IFactory
     5from zope.component import createObject
    56from zope.exceptions import DuplicationError
    67from zope.interface import implementedBy
    7 from waeup.interfaces import IFacultyContainer, IFaculty
     8from waeup.interfaces import IFacultyContainer, IFaculty, IWAeUPCSVImporter
     9from waeup.utils.importexport import CSVImporter
    810from waeup.viewlets import MainArea, LeftSidebar, Index, Add, FormWrapMixin
    911
     
    2224        id = str(self.next_id)
    2325        self[id] = faculty
    24         while self.next_id in self.keys():
     26        while str(self.next_id) in self.keys():
    2527            # Look for next unused int...
    2628            self.next_id += 1
    2729        return id
     30
     31    def clear(self):
     32        keys = self.keys()
     33        for key in keys:
     34            del self[key]
     35        next_id = 0L
    2836
    2937class FacultyContainerFactory(grok.GlobalUtility):
     
    4048    def getInterfaces(self):
    4149        return implementedBy(FacultyContainer)
     50
     51class FacultyCSVImporter(CSVImporter):
     52    """Importer to import CSV data into faculty containers.
     53    """
     54    grok.context(FacultyContainer)
     55    grok.implements(IWAeUPCSVImporter)
     56    datatype = u'Faculty importer'
     57    column_terms = ['code', 'title', 'title_prefix']
     58
     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))
     66            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)
     70        return
     71
    4272
    4373#
     
    6797    @grok.action('Add faculty')
    6898    def addFaculty(self, **data):
    69         faculty = getUtility(IFaculty)
     99        faculty = createObject(u'waeup.Faculty')
    70100        self.applyData(faculty, **data)
    71101        try:
     
    104134    def render(self):
    105135        return u'<div class="portlet"><a href="add">Add faculty</a></div>'
     136
     137
Note: See TracChangeset for help on using the changeset viewer.