source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/batching.py @ 8261

Last change on this file since 8261 was 8236, checked in by uli, 13 years ago

Support IGNORE_MARKER.

  • Property svn:keywords set to Id
File size: 6.6 KB
RevLine 
[7192]1## $Id: batching.py 8236 2012-04-20 22:07:46Z uli $
[5321]2##
[7192]3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
[5321]4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
[7192]8##
[5321]9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
[7192]13##
[5321]14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Batch processing for applicants.
19"""
[7268]20import csv
[5321]21import grok
[7271]22from zope.schema import getFields
[5321]23from zope.interface import Interface
[7268]24from zope.component import queryUtility
25from zope.catalog.interfaces import ICatalog
[7811]26from waeup.kofa.interfaces import (
[8236]27    IBatchProcessor, IObjectConverter, FatalCSVError, IGNORE_MARKER)
[7811]28from waeup.kofa.utils.batching import BatchProcessor
29from waeup.kofa.applicants.interfaces import (
[7268]30    IApplicantsContainer, IApplicant, IApplicantUpdateByRegNo)
[5321]31
[7933]32class ApplicantsContainerProcessor(BatchProcessor):
33    """A processor for applicants containers.
[5321]34    """
[5474]35    grok.implements(IBatchProcessor)
[5321]36    grok.provides(IBatchProcessor)
37    grok.context(Interface)
[7933]38    util_name = 'applicants container processor'
[5321]39    grok.name(util_name)
40
[7933]41    name = u'Applicants Container Processor'
[5475]42    mode = u'create'
[6251]43    iface = IApplicantsContainer
[5321]44
[6251]45    location_fields = ['code',]
[6282]46    factory_name = 'waeup.ApplicantsContainer'
[5321]47
48    def parentsExist(self, row, site):
[6251]49        return 'applicants' in site.keys()
[5321]50
51    def entryExists(self, row, site):
[6251]52        return row['code'] in site['applicants'].keys()
[5321]53
54    def getParent(self, row, site):
[6251]55        return site['applicants']
[5321]56
57    def getEntry(self, row, site):
58        if not self.entryExists(row, site):
59            return None
60        parent = self.getParent(row, site)
[6251]61        return parent.get(row['code'])
[5321]62
63    def addEntry(self, obj, row, site):
64        parent = self.getParent(row, site)
[6251]65        parent[row['code']] = obj
[5321]66        return
67
68    def delEntry(self, row, site):
69        parent = self.getParent(row, site)
[6251]70        del parent[row['code']]
[5321]71        return
[7262]72
[7933]73class ApplicantProcessor(BatchProcessor):
[7262]74    """A batch processor for IApplicant objects.
75    """
76    grok.implements(IBatchProcessor)
77    grok.provides(IBatchProcessor)
78    grok.context(Interface)
[7933]79    util_name = 'applicantprocessor'
[7262]80    grok.name(util_name)
[7933]81    name = u'Applicant Processor'
[7262]82    iface = IApplicant
[8008]83    location_fields = ['container_code']
[7262]84    factory_name = 'waeup.Applicant'
85
86    mode = None
87
88    @property
[7268]89    def available_fields(self):
90        return sorted(list(set(
[7271]91            ['application_number','reg_number','container_code'] + getFields(
[7268]92                self.iface).keys())))
[7262]93
[7268]94    def checkHeaders(self, headerfields, mode='create'):
[7270]95        if not 'reg_number' in headerfields and not 'application_number' \
[7268]96            in headerfields:
97            raise FatalCSVError(
[7270]98                "Need at least columns application_number or reg_number ")
[7268]99        if mode == 'create':
100            for field in self.required_fields:
101                if not field in headerfields:
102                    raise FatalCSVError(
103                        "Need at least columns %s for import!" %
104                        ', '.join(["'%s'" % x for x in self.required_fields]))
105        # Check for fields to be ignored...
106        not_ignored_fields = [x for x in headerfields
107                              if not x.startswith('--')]
108        if len(set(not_ignored_fields)) < len(not_ignored_fields):
109            raise FatalCSVError(
110                "Double headers: each column name may only appear once.")
111        return True
[7262]112
[7268]113    def getLocator(self, row):
[8236]114        if row.get('application_number', None) not in (IGNORE_MARKER, None):
[7268]115            return 'application_number'
[8236]116        elif row.get('reg_number', None) not in (IGNORE_MARKER, None):
[7270]117            return 'reg_number'
[7268]118        else:
119            return None
[7262]120
121    def getParent(self, row, site):
[7268]122        if not 'applicants' in site.keys():
123            return False
[7262]124        return site['applicants'][row['container_code']]
125
[7268]126    def parentsExist(self, row, site):
127        return self.getParent(row, site) is not None
128
[7262]129    def getEntry(self, row, site):
[7264]130        if not self.parentsExist(row, site):
131            return None
132        parent = self.getParent(row, site)
[7268]133        if self.getLocator(row) == 'application_number':
134            if row['application_number'] in parent:
135                applicant = parent[row['application_number']]
136                return applicant
[7270]137        elif self.getLocator(row) == 'reg_number':
138            reg_number = row['reg_number']
[7268]139            cat = queryUtility(ICatalog, name='applicants_catalog')
140            results = list(
[7270]141                cat.searchResults(reg_number=(reg_number, reg_number)))
[7268]142            if results:
143                return results[0]
144        return None
[7262]145
[7268]146    def entryExists(self, row, site):
147        return self.getEntry(row, site) is not None
148
[7262]149    def addEntry(self, obj, row, site):
150        parent = self.getParent(row, site)
151        parent.addApplicant(obj)
152        return
153
154    def delEntry(self, row, site):
[7268]155        applicant = self.getEntry(row, site)
156        if applicant is not None:
[7262]157            parent = self.getParent(row, site)
[7268]158            del parent[applicant.application_number]
[7262]159        pass
[7268]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',
[7270]169                'application_number', 'reg_number'] and mode == 'remove':
[7268]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']:
[7270]181            if self.getLocator(row) == 'reg_number':
[7268]182                iface = IApplicantUpdateByRegNo
183        else:
184            iface = self.iface
185        converter = IObjectConverter(iface)
186        errs, inv_errs, conv_dict =  converter.fromStringDict(
[8223]187            row, self.factory_name, mode=mode)
[7268]188        return errs, inv_errs, conv_dict
Note: See TracBrowser for help on using the repository browser.