Changeset 8220


Ignore:
Timestamp:
19 Apr 2012, 14:19:45 (12 years ago)
Author:
uli
Message:

Clean up basic batch processor and add functionality for changed
interface, especially replacing empty values with IGNORE markers on
updates.

File:
1 edited

Legend:

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

    r8214 r8220  
    3939    This is a non-active baseclass.
    4040    """
    41     grok.provides(IBatchProcessor)
     41    grok.implements(IBatchProcessor)
    4242    grok.context(Interface)
    4343    grok.baseclass()
     
    200200    def updateEntry(self, obj, row, site):
    201201        """Update obj to the values given in row.
    202         """
     202
     203        Returns a string describing the fields changed.
     204        """
     205        changed = []
    203206        for key, value in row.items():
     207            # Skip fields to be ignored.
     208            if value == IGNORE_MARKER:
     209                continue
    204210            # Skip fields not declared in interface.
    205             if hasattr(obj, key):
    206                 # Set attribute to None if value is marked for deletion
    207                 if value != '<IGNORE>':
    208                     setattr(obj, key, value)
    209                 #if value == DELETIONMARKER:
    210                 #    setattr(obj, key, None)
    211                 #elif value is not None:
    212                 #    setattr(obj, key, value)
    213         return
     211            if not hasattr(obj, key):
     212                continue
     213            setattr(obj, key, value)
     214            changed.append('%s=%s' % (key, value))
     215        return ', '.join(changed)
    214216
    215217    def createLogfile(self, path, fail_path, num, warnings, mode, user,
     
    250252        return
    251253
    252     def checkConversion(self, row, mode='ignore'):
     254    def checkConversion(self, row, mode='ignore', ignore_empty=True):
    253255        """Validates all values in row.
    254256        """
    255257        converter = IObjectConverter(self.iface)
    256258        errs, inv_errs, conv_dict =  converter.fromStringDict(
    257             row, self.factory_name)
     259            row, self.factory_name, mode=mode)
    258260        return errs, inv_errs, conv_dict
    259261
    260262    def doImport(self, path, headerfields, mode='create', user='Unknown',
    261                  logger=None):
     263                 logger=None, ignore_empty=True):
    262264        """Perform actual import.
    263265        """
     
    292294            string_row = self.applyMapping(raw_row, mapping)
    293295            row = dict(string_row.items()) # create deep copy
     296            if ignore_empty and mode in ('update'):
     297                # replace empty strings with ignore-markers
     298                for key, val in row.items():
     299                    if val == '':
     300                        row.update(key=IGNORE_MARKER)
    294301            errs, inv_errs, conv_dict = self.checkConversion(string_row, mode)
    295302            if errs or inv_errs:
     
    312319                    self.writeFailedRow(
    313320                        failed_writer, string_row,
    314                         "This object already exists in the same container. Skipping.")
     321                        "This object already exists in the same container. "
     322                        "Skipping.")
    315323                    continue
    316324                obj = self.callFactory()
Note: See TracChangeset for help on using the changeset viewer.