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

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

Support IGNORE_MARKER.

  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1## $Id: batching.py 8236 2012-04-20 22:07:46Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
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.
8##
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.
13##
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"""
20import csv
21import grok
22from zope.schema import getFields
23from zope.interface import Interface
24from zope.component import queryUtility
25from zope.catalog.interfaces import ICatalog
26from waeup.kofa.interfaces import (
27    IBatchProcessor, IObjectConverter, FatalCSVError, IGNORE_MARKER)
28from waeup.kofa.utils.batching import BatchProcessor
29from waeup.kofa.applicants.interfaces import (
30    IApplicantsContainer, IApplicant, IApplicantUpdateByRegNo)
31
32class ApplicantsContainerProcessor(BatchProcessor):
33    """A processor for applicants containers.
34    """
35    grok.implements(IBatchProcessor)
36    grok.provides(IBatchProcessor)
37    grok.context(Interface)
38    util_name = 'applicants container processor'
39    grok.name(util_name)
40
41    name = u'Applicants Container Processor'
42    mode = u'create'
43    iface = IApplicantsContainer
44
45    location_fields = ['code',]
46    factory_name = 'waeup.ApplicantsContainer'
47
48    def parentsExist(self, row, site):
49        return 'applicants' in site.keys()
50
51    def entryExists(self, row, site):
52        return row['code'] in site['applicants'].keys()
53
54    def getParent(self, row, site):
55        return site['applicants']
56
57    def getEntry(self, row, site):
58        if not self.entryExists(row, site):
59            return None
60        parent = self.getParent(row, site)
61        return parent.get(row['code'])
62
63    def addEntry(self, obj, row, site):
64        parent = self.getParent(row, site)
65        parent[row['code']] = obj
66        return
67
68    def delEntry(self, row, site):
69        parent = self.getParent(row, site)
70        del parent[row['code']]
71        return
72
73class ApplicantProcessor(BatchProcessor):
74    """A batch processor for IApplicant objects.
75    """
76    grok.implements(IBatchProcessor)
77    grok.provides(IBatchProcessor)
78    grok.context(Interface)
79    util_name = 'applicantprocessor'
80    grok.name(util_name)
81    name = u'Applicant Processor'
82    iface = IApplicant
83    location_fields = ['container_code']
84    factory_name = 'waeup.Applicant'
85
86    mode = None
87
88    @property
89    def available_fields(self):
90        return sorted(list(set(
91            ['application_number','reg_number','container_code'] + getFields(
92                self.iface).keys())))
93
94    def checkHeaders(self, headerfields, mode='create'):
95        if not 'reg_number' in headerfields and not 'application_number' \
96            in headerfields:
97            raise FatalCSVError(
98                "Need at least columns application_number or reg_number ")
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
112
113    def getLocator(self, row):
114        if row.get('application_number', None) not in (IGNORE_MARKER, None):
115            return 'application_number'
116        elif row.get('reg_number', None) not in (IGNORE_MARKER, None):
117            return 'reg_number'
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']]
125
126    def parentsExist(self, row, site):
127        return self.getParent(row, site) is not None
128
129    def getEntry(self, row, site):
130        if not self.parentsExist(row, site):
131            return None
132        parent = self.getParent(row, site)
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_number':
138            reg_number = row['reg_number']
139            cat = queryUtility(ICatalog, name='applicants_catalog')
140            results = list(
141                cat.searchResults(reg_number=(reg_number, reg_number)))
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
148
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):
155        applicant = self.getEntry(row, site)
156        if applicant is not None:
157            parent = self.getParent(row, site)
158            del parent[applicant.application_number]
159        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_number'] 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_number':
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, mode=mode)
188        return errs, inv_errs, conv_dict
Note: See TracBrowser for help on using the repository browser.