Ignore:
Timestamp:
4 Dec 2011, 17:40:41 (13 years ago)
Author:
Henrik Bettermann
Message:

Use reg_no as locator to find applicants for updating and removal.

Location:
main/waeup.sirp/trunk/src/waeup/sirp/applicants
Files:
2 edited

Legend:

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

    r7264 r7268  
    1818"""Batch processing for applicants.
    1919"""
     20import csv
    2021import grok
    2122from zope.interface import Interface
    22 from waeup.sirp.interfaces import IBatchProcessor
     23from zope.component import queryUtility
     24from zope.catalog.interfaces import ICatalog
     25from waeup.sirp.interfaces import IBatchProcessor, IObjectConverter
    2326from waeup.sirp.utils.batching import BatchProcessor
    2427from waeup.sirp.applicants.interfaces import (
    25     IApplicantsContainer, IApplicant)
     28    IApplicantsContainer, IApplicant, IApplicantUpdateByRegNo)
    2629
    2730class ApplicantsContainerImporter(BatchProcessor):
     
    7881    location_fields = []
    7982    factory_name = 'waeup.Applicant'
    80     location_fields = ['container_code', 'application_number']
     83    location_fields = ['container_code']
    8184
    8285    mode = None
    8386
    8487    @property
    85     def req(self):
    86         result = dict(
    87             create = ['container_code'] + self.required_fields,
    88             update = self.location_fields,
    89             remove = self.location_fields,
    90         )
    91         return result
     88    def available_fields(self):
     89        return sorted(list(set(
     90            ['application_number','reg_no'] + getFields(
     91                self.iface).keys())))
     92
     93    def checkHeaders(self, headerfields, mode='create'):
     94        if not 'reg_no' in headerfields and not 'application_number' \
     95            in headerfields:
     96            raise FatalCSVError(
     97                "Need at least columns application_number or reg_no ")
     98        if mode == 'create':
     99            for field in self.required_fields:
     100                if not field in headerfields:
     101                    raise FatalCSVError(
     102                        "Need at least columns %s for import!" %
     103                        ', '.join(["'%s'" % x for x in self.required_fields]))
     104        # Check for fields to be ignored...
     105        not_ignored_fields = [x for x in headerfields
     106                              if not x.startswith('--')]
     107        if len(set(not_ignored_fields)) < len(not_ignored_fields):
     108            raise FatalCSVError(
     109                "Double headers: each column name may only appear once.")
     110        return True
     111
     112    def getLocator(self, row):
     113        #if 'application_number' in row.keys() and row['application_number']:
     114        if row.get('application_number',None):
     115            return 'application_number'
     116        elif 'reg_no' in row.keys() and row['reg_no']:
     117            return 'reg_no'
     118        else:
     119            return None
     120
     121    def getParent(self, row, site):
     122        if not 'applicants' in site.keys():
     123            return False
     124        return site['applicants'][row['container_code']]
    92125
    93126    def parentsExist(self, row, site):
    94         if not 'applicants' in site.keys():
    95             return False
    96         return row['container_code'] in site['applicants'].keys()
    97 
    98     def entryExists(self, row, site):
    99         return self.getEntry(row, site) is not None
    100 
    101     def getParent(self, row, site):
    102         return site['applicants'][row['container_code']]
     127        return self.getParent(row, site) is not None
    103128
    104129    def getEntry(self, row, site):
     
    106131            return None
    107132        parent = self.getParent(row, site)
    108         return parent.get(row.get('application_number', None), None)
     133        if self.getLocator(row) == 'application_number':
     134            if row['application_number'] in parent:
     135                applicant = parent[row['application_number']]
     136                return applicant
     137        elif self.getLocator(row) == 'reg_no':
     138            reg_no = row['reg_no']
     139            cat = queryUtility(ICatalog, name='applicants_catalog')
     140            results = list(
     141                cat.searchResults(reg_no=(reg_no, reg_no)))
     142            if results:
     143                return results[0]
     144        return None
     145
     146    def entryExists(self, row, site):
     147        return self.getEntry(row, site) is not None
    109148
    110149    def addEntry(self, obj, row, site):
     
    114153
    115154    def delEntry(self, row, site):
    116         if self.entryExists(row, site):
     155        applicant = self.getEntry(row, site)
     156        if applicant is not None:
    117157            parent = self.getParent(row, site)
    118             del parent[row['application_number']]
     158            del parent[applicant.application_number]
    119159        pass
     160
     161    def getMapping(self, path, headerfields, mode):
     162        """Get a mapping from CSV file headerfields to actually used fieldnames.
     163        """
     164        result = dict()
     165        reader = csv.reader(open(path, 'rb'))
     166        raw_header = reader.next()
     167        for num, field in enumerate(headerfields):
     168            if field not in ['container_code',
     169                'application_number', 'reg_no'] and mode == 'remove':
     170                continue
     171            if field == u'--IGNORE--':
     172                # Skip ignored columns in failed and finished data files.
     173                continue
     174            result[raw_header[num]] = field
     175        return result
     176
     177    def checkConversion(self, row, mode='create'):
     178        """Validates all values in row.
     179        """
     180        if mode in ['update', 'remove']:
     181            if self.getLocator(row) == 'reg_no':
     182                iface = IApplicantUpdateByRegNo
     183        else:
     184            iface = self.iface
     185        converter = IObjectConverter(iface)
     186        errs, inv_errs, conv_dict =  converter.fromStringDict(
     187            row, self.factory_name)
     188        return errs, inv_errs, conv_dict
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/interfaces.py

    r7263 r7268  
    443443        )
    444444
     445class IApplicantUpdateByRegNo(IApplicant):
     446    """Representation of an applicant.
     447
     448    Skip regular reg_no validation if reg_no is used for finding
     449    the applicant object.
     450    """
     451    reg_no = schema.TextLine(
     452        title = u'Registration Number',
     453        default = None,
     454        required = False,
     455        )
     456
    445457class IApplicantOnlinePayment(IOnlinePayment):
    446458    """An applicant payment via payment gateways.
Note: See TracChangeset for help on using the changeset viewer.