[7192] | 1 | ## $Id: batching.py 7264 2011-12-04 13:34:26Z 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 | """ |
---|
| 20 | import grok |
---|
| 21 | from zope.interface import Interface |
---|
| 22 | from waeup.sirp.interfaces import IBatchProcessor |
---|
| 23 | from waeup.sirp.utils.batching import BatchProcessor |
---|
[7262] | 24 | from waeup.sirp.applicants.interfaces import ( |
---|
| 25 | IApplicantsContainer, IApplicant) |
---|
[5321] | 26 | |
---|
[6251] | 27 | class ApplicantsContainerImporter(BatchProcessor): |
---|
| 28 | """An importer for applicants containers. |
---|
[5321] | 29 | """ |
---|
[5474] | 30 | grok.implements(IBatchProcessor) |
---|
[5321] | 31 | grok.provides(IBatchProcessor) |
---|
| 32 | grok.context(Interface) |
---|
[6251] | 33 | util_name = 'applicants container importer' |
---|
[5321] | 34 | grok.name(util_name) |
---|
| 35 | |
---|
[6251] | 36 | name = u'Applicants Container Importer' |
---|
[5475] | 37 | mode = u'create' |
---|
[6251] | 38 | iface = IApplicantsContainer |
---|
[5321] | 39 | |
---|
[6251] | 40 | location_fields = ['code',] |
---|
[6282] | 41 | factory_name = 'waeup.ApplicantsContainer' |
---|
[5321] | 42 | |
---|
| 43 | def parentsExist(self, row, site): |
---|
[6251] | 44 | return 'applicants' in site.keys() |
---|
[5321] | 45 | |
---|
| 46 | def entryExists(self, row, site): |
---|
[6251] | 47 | return row['code'] in site['applicants'].keys() |
---|
[5321] | 48 | |
---|
| 49 | def getParent(self, row, site): |
---|
[6251] | 50 | return site['applicants'] |
---|
[5321] | 51 | |
---|
| 52 | def getEntry(self, row, site): |
---|
| 53 | if not self.entryExists(row, site): |
---|
| 54 | return None |
---|
| 55 | parent = self.getParent(row, site) |
---|
[6251] | 56 | return parent.get(row['code']) |
---|
[5321] | 57 | |
---|
| 58 | def addEntry(self, obj, row, site): |
---|
| 59 | parent = self.getParent(row, site) |
---|
[6251] | 60 | parent[row['code']] = obj |
---|
[5321] | 61 | return |
---|
| 62 | |
---|
| 63 | def delEntry(self, row, site): |
---|
| 64 | parent = self.getParent(row, site) |
---|
[6251] | 65 | del parent[row['code']] |
---|
[5321] | 66 | return |
---|
[7262] | 67 | |
---|
[7263] | 68 | class ApplicantImporter(BatchProcessor): |
---|
[7262] | 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): |
---|
[7264] | 99 | return self.getEntry(row, site) is not None |
---|
[7262] | 100 | |
---|
| 101 | def getParent(self, row, site): |
---|
| 102 | return site['applicants'][row['container_code']] |
---|
| 103 | |
---|
| 104 | def getEntry(self, row, site): |
---|
[7264] | 105 | if not self.parentsExist(row, site): |
---|
| 106 | return None |
---|
| 107 | parent = self.getParent(row, site) |
---|
| 108 | return parent.get(row.get('application_number', None), None) |
---|
[7262] | 109 | |
---|
| 110 | def addEntry(self, obj, row, site): |
---|
| 111 | parent = self.getParent(row, site) |
---|
| 112 | parent.addApplicant(obj) |
---|
| 113 | return |
---|
| 114 | |
---|
| 115 | def delEntry(self, row, site): |
---|
[7264] | 116 | if self.entryExists(row, site): |
---|
[7262] | 117 | parent = self.getParent(row, site) |
---|
| 118 | del parent[row['application_number']] |
---|
| 119 | pass |
---|