Ignore:
Timestamp:
4 Jun 2011, 02:29:21 (13 years ago)
Author:
uli
Message:

Finally make the new converter work. API-wise it is as good as the old one (can import everyting, the old one could),
but design-wise it might be much more powerfull. Basically it can handle/convert all content-types for which one can
create an Add- or EditForm? successfully. In other words: if you manage to write an edit form for some content type,
then you can also create an importer for that content-type. Still finetuning needed (for dates, bool data, etc.) but
the main things work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/batching.py

    r6259 r6273  
    1515from zope.schema import getFields
    1616from waeup.sirp.interfaces import (
    17     IBatchProcessor, ISchemaTypeConverter, FatalCSVError, DuplicationError)
     17    IBatchProcessor, ISchemaTypeConverter, FatalCSVError, DuplicationError,
     18    IObjectConverter)
    1819
    1920class BatchProcessor(grok.GlobalUtility):
     
    149150                    warnings.append(msg)
    150151        return (row, warnings)
     152
     153    def stringFromErrs(self, errors, inv_errors):
     154        result = []
     155        for err in errors:
     156            fieldname, message = err
     157            result.append("%s: %s" % (fieldname, message))
     158        for err in inv_errors:
     159            result.append("invariant: %s" % err)
     160        return '; '.join(result)
    151161
    152162    def callFactory(self, *args, **kw):
     
    239249        temp_dir = tempfile.mkdtemp()
    240250
    241         (base, ext) = os.path.splitext(path)
     251        base = os.path.basename(path)
     252        (base, ext) = os.path.splitext(base)
    242253        failed_path = os.path.join(temp_dir, "%s.pending%s" % (base, ext))
    243254        failed_headers = mapping.keys()
     
    258269        num_warns = 0
    259270        site = grok.getSite()
     271        converter = IObjectConverter(self.iface)
    260272        for raw_row in reader:
    261273            num += 1
    262274            string_row = self.applyMapping(raw_row, mapping)
    263             row, conv_warnings = self.convertToTypes(
    264                 copy.deepcopy(string_row), converters)
    265             if len(conv_warnings):
     275            row = dict(string_row.items()) # create deep copy
     276            errs, inv_errs, conv_dict =  converter.fromStringDict(
     277                string_row, self.factory_name)
     278            if errs or inv_errs:
    266279                num_warns += 1
    267                 self.writeFailedRow(failed_writer, raw_row, conv_warnings)
     280                conv_warnings = self.stringFromErrs(errs, inv_errs)
     281                self.writeFailedRow(
     282                    failed_writer, raw_row, conv_warnings)
    268283                continue
     284            row.update(conv_dict)
    269285
    270286            if mode == 'create':
     
    286302                try:
    287303                    self.addEntry(obj, row, site)
    288                 except DuplicationError, error:
    289                     num_warns += 1
    290                     self.writeFailedRow(
    291                         failed_writer, raw_row,
    292                         "%s Skipping." % error.msg)
     304                except KeyError, error:
     305                    num_warns += 1
     306                    self.writeFailedRow(
     307                        failed_writer, raw_row,
     308                        "%s Skipping." % error.message)
    293309                    continue
    294310            elif mode == 'remove':
Note: See TracChangeset for help on using the changeset viewer.