source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/batching.py @ 7262

Last change on this file since 7262 was 7262, checked in by Henrik Bettermann, 13 years ago

Add Applicant Importer (tests will follow).

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1## $Id: batching.py 7262 2011-12-04 08:36:14Z henrik $
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 grok
21from zope.interface import Interface
22from waeup.sirp.interfaces import IBatchProcessor
23from waeup.sirp.utils.batching import BatchProcessor
24from waeup.sirp.applicants.interfaces import (
25    IApplicantsContainer, IApplicant)
26
27class ApplicantsContainerImporter(BatchProcessor):
28    """An importer for applicants containers.
29    """
30    grok.implements(IBatchProcessor)
31    grok.provides(IBatchProcessor)
32    grok.context(Interface)
33    util_name = 'applicants container importer'
34    grok.name(util_name)
35
36    name = u'Applicants Container Importer'
37    mode = u'create'
38    iface = IApplicantsContainer
39
40    location_fields = ['code',]
41    factory_name = 'waeup.ApplicantsContainer'
42
43    def parentsExist(self, row, site):
44        return 'applicants' in site.keys()
45
46    def entryExists(self, row, site):
47        return row['code'] in site['applicants'].keys()
48
49    def getParent(self, row, site):
50        return site['applicants']
51
52    def getEntry(self, row, site):
53        if not self.entryExists(row, site):
54            return None
55        parent = self.getParent(row, site)
56        return parent.get(row['code'])
57
58    def addEntry(self, obj, row, site):
59        parent = self.getParent(row, site)
60        parent[row['code']] = obj
61        return
62
63    def delEntry(self, row, site):
64        parent = self.getParent(row, site)
65        del parent[row['code']]
66        return
67
68class ApplicantProcessor(BatchProcessor):
69    """A batch processor for IApplicant objects.
70    """
71    grok.implements(IBatchProcessor)
72    grok.provides(IBatchProcessor)
73    grok.context(Interface)
74    util_name = 'applicantimporter'
75    grok.name(util_name)
76    name = u'Applicant Importer'
77    iface = IApplicant
78    location_fields = []
79    factory_name = 'waeup.Applicant'
80    location_fields = ['container_code', 'application_number']
81
82    mode = None
83
84    @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
92
93    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        if not self.parentsExist(row, site):
100            return None
101        parent = self.getParent(row, site)
102        if 'application_number' in row.keys() and row['application_number']:
103            if row['application_number'] in parent.keys():
104                return parent[row['application_number']]
105        return None
106
107    def getParent(self, row, site):
108        return site['applicants'][row['container_code']]
109
110    def getEntry(self, row, site):
111        return self.entryExists(row, site)
112
113    def addEntry(self, obj, row, site):
114        parent = self.getParent(row, site)
115        parent.addApplicant(obj)
116        return
117
118    def delEntry(self, row, site):
119        applicant = self.entryExists(row, site)
120        if applicant:
121            parent = self.getParent(row, site)
122            del parent[row['application_number']]
123        pass
Note: See TracBrowser for help on using the repository browser.