source: main/waeup.kofa/branches/uli-rm-bootstrap/src/waeup/kofa/hostels/batching.py @ 17952

Last change on this file since 17952 was 13434, checked in by Henrik Bettermann, 9 years ago

Update documentation.

Show button also on all data center pages.

  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1## $Id: batching.py 13434 2015-11-11 07:54:44Z 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 components for hostels.
19
20"""
21import grok
22from zope.interface import Interface
23from zope.component import getUtility
24from zope.catalog.interfaces import ICatalog
25from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER
26from waeup.kofa.utils.batching import BatchProcessor
27from waeup.kofa.hostels.interfaces import IHostel, IBed
28from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED
29from waeup.kofa.interfaces import MessageFactory as _
30
31class HostelProcessor(BatchProcessor):
32    """The Hostel Procesor imports hostels, i.e. the container objects of
33    beds. It does not import beds. There is nothing special about this
34    processor.
35    """
36    grok.implements(IBatchProcessor)
37    grok.provides(IBatchProcessor)
38    grok.context(Interface)
39    util_name = 'hostelprocessor'
40    grok.name(util_name)
41
42    name = _('Hostel Processor')
43    iface = IHostel
44
45    location_fields = ['hostel_id',]
46    factory_name = 'waeup.Hostel'
47
48    mode = None
49
50    def parentsExist(self, row, site):
51        return 'hostels' in site.keys()
52
53    def entryExists(self, row, site):
54        return row['hostel_id'] in site['hostels'].keys()
55
56    def getParent(self, row, site):
57        return site['hostels']
58
59    def getEntry(self, row, site):
60        if not self.entryExists(row, site):
61            return None
62        parent = self.getParent(row, site)
63        return parent.get(row['hostel_id'])
64
65    def addEntry(self, obj, row, site):
66        parent = self.getParent(row, site)
67        parent.addHostel(obj)
68        return
69
70    def updateEntry(self, obj, row, site, filename):
71        """Update obj to the values given in row.
72        """
73        items_changed = super(HostelProcessor, self).updateEntry(
74            obj, row, site, filename)
75        # Log actions...
76        location_field = self.location_fields[0]
77        grok.getSite()['hostels'].logger.info(
78            '%s - %s - %s - updated: %s'
79            % (self.name, filename, row[location_field], items_changed))
80        return
81
82
83class BedProcessor(BatchProcessor):
84    """The Bed Procesor update beds. It allocates students
85    to empty beds and switches the reservation status of beds. ``1``
86    means reserved and ``0`` unreserved. Beds cannot be released
87    by import.
88    """
89    grok.implements(IBatchProcessor)
90    grok.provides(IBatchProcessor)
91    grok.context(Interface)
92    util_name = 'bedupdater'
93    grok.name(util_name)
94
95    name = _('Bed Processor (update only)')
96    iface = IBed
97
98    location_fields = ['hostel_id', 'bed_id']
99    factory_name = None
100
101    mode = None
102
103    @property
104    def available_fields(self):
105        return self.location_fields + ['reserved', 'owner']
106
107    def parentsExist(self, row, site):
108        if not 'hostels' in site.keys():
109            return False
110        return row['hostel_id'] in site['hostels']
111
112    def entryExists(self, row, site):
113        if not self.parentsExist(row, site):
114            return False
115        parent = self.getParent(row, site)
116        return row['bed_id'] in parent.keys()
117
118    def getParent(self, row, site):
119        return site['hostels'][row['hostel_id']]
120
121    def getEntry(self, row, site):
122        if not self.entryExists(row, site):
123            return None
124        parent = self.getParent(row, site)
125        return parent.get(row['bed_id'])
126
127    def checkUpdateRequirements(self, obj, row, site):
128        """Checks requirements the bed must fulfill
129        before being updated.
130        """
131        # Check if bed is occupied
132        if row.get('owner') and obj.owner != NOT_OCCUPIED:
133            return 'Bed is occupied.'
134
135    def checkConversion(self, row, mode='ignore'):
136        """Validates all values in row.
137        """
138        inv_errs = ''
139        conv_dict = {}
140        errs = []
141        reserved = row.get('reserved')
142        if reserved not in (None, IGNORE_MARKER, '', '0', '1'):
143            errs.append(('reserved','invalid value'))
144        owner = row.get('owner')
145        if owner not in (None, '', IGNORE_MARKER):
146            if owner == NOT_OCCUPIED:
147                errs.append(('owner','bed cannot be released by import'))
148                return errs, inv_errs, conv_dict
149            beds_cat = getUtility(ICatalog, name='beds_catalog')
150            results = list(beds_cat.searchResults(owner=(owner, owner)))
151            if len(results) > 0:
152                errs.append((
153                    'owner','student already resides in %s'
154                    % results[0].bed_id))
155                return errs, inv_errs, conv_dict
156            students_cat = getUtility(ICatalog, name='students_catalog')
157            results = list(students_cat.searchResults(student_id=(owner, owner)))
158            if len(results) != 1:
159                errs.append(('owner','student does not exist'))
160        return errs, inv_errs, conv_dict
161
162    def updateEntry(self, obj, row, site, filename):
163        """Update obj to the values given in row.
164        """
165        changed = []
166        owner = row.get('owner')
167        if owner not in (None, '', IGNORE_MARKER):
168            obj.bookBed(owner)
169            changed.append('owner=%s' % owner)
170        reserved = row.get('reserved')
171        sh, sex, bt = obj.bed_type.split('_')
172        if (reserved == '1' and bt != 'reserved') or \
173            (reserved == '0'and bt == 'reserved'):
174            message = obj.switchReservation()
175            changed.append(message)
176        # Log actions...
177        if changed:
178            items_changed = ', '.join(changed)
179        else:
180            items_changed = 'nothing'
181        location_field = self.location_fields[1]
182        grok.getSite()['hostels'].logger.info(
183            '%s - %s - %s - updated: %s'
184            % (self.name, filename, row[location_field], items_changed))
185        return
Note: See TracBrowser for help on using the repository browser.