source: main/waeup.sirp/trunk/src/waeup/sirp/hostels/hostel.py @ 6980

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

Remove all empty beds when updating a hostel and then refill hostel again. Occupied beds remain in hostel even if they are not part of the new configuration. Tests will follow.

  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
2## This program is free software; you can redistribute it and/or modify
3## it under the terms of the GNU General Public License as published by
4## the Free Software Foundation; either version 2 of the License, or
5## (at your option) any later version.
6##
7## This program is distributed in the hope that it will be useful,
8## but WITHOUT ANY WARRANTY; without even the implied warranty of
9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10## GNU General Public License for more details.
11##
12## You should have received a copy of the GNU General Public License
13## along with this program; if not, write to the Free Software
14## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15##
16"""
17These are the hostels.
18"""
19import grok
20from datetime import datetime
21from grok import index
22from waeup.sirp.utils.helpers import attrs_to_fields
23from waeup.sirp.hostels.interfaces import IHostel, IBed
24
25NOT_OCCUPIED = u'not occupied'
26
27class Hostel(grok.Container):
28    """This is a hostel.
29    """
30    grok.implements(IHostel)
31    grok.provides(IHostel)
32
33    def loggerInfo(self, ob_class, comment=None):
34        target = self.__name__
35        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
36
37    def addBed(self, bed):
38        """Add a bed.
39        """
40        if not IBed.providedBy(bed):
41            raise TypeError(
42                'Hostels contain only IBed instances')
43        self[bed.bed_id] = bed
44        return
45
46    def updateBeds(self):
47        """Fill hostel with beds or update beds.
48        """
49        added_counter = 0
50        modified_counter = 0
51        removed_counter = 0
52
53        # Remove all empty beds. Occupied beds remain in hostel!
54        keys = list(self.keys()) # create list copy
55        for key in keys:
56            if self[key].owner == NOT_OCCUPIED:
57                del self[key]
58                self._p_changed = True
59                removed_counter += 1
60
61        blocks_for_female = getattr(self,'blocks_for_female',[])
62        blocks_for_male = getattr(self,'blocks_for_male',[])
63        beds_for_fresh = getattr(self,'beds_for_fresh',[])
64        beds_for_pre = getattr(self,'beds_for_pre',[])
65        beds_for_returning = getattr(self,'beds_for_returning',[])
66        beds_for_final = getattr(self,'beds_for_final',[])
67        beds_for_all = getattr(self,'beds_for_all',[])
68        beds_reserved = getattr(self,'beds_reserved',[])
69        all_blocks = blocks_for_female + blocks_for_male
70        all_beds = (beds_for_pre + beds_for_fresh +
71            beds_for_returning + beds_for_final + beds_for_all)
72        for block in all_blocks:
73            sex = 'male'
74            if block in blocks_for_female:
75                sex = 'female'
76            for floor in range(1,int(self.floors_per_block)+1):
77                for room in range(1,int(self.rooms_per_floor)+1):
78                    for bed in all_beds:
79                        room_nr = floor*100 + room
80                        bt = 'all'
81                        if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved:
82                            bt = "reserved"
83                        elif bed in beds_for_fresh:
84                            bt = 'fr'
85                        elif bed in beds_for_pre:
86                            bt = 'pr'
87                        elif bed in beds_for_final:
88                            bt = 'fi'
89                        elif bed in beds_for_returning:
90                            bt = 're'
91                        bt = u'%s_%s_%s' % (self.special_handling,sex,bt)
92                        uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed)
93                        if self.has_key(uid):
94                            bed = self[uid]
95                            if bed.bed_type != bt:
96                                bed.bed_type = bt
97                                modified_counter += 1
98                        else:
99                            bed = Bed()
100                            bed.bed_id = uid
101                            bed.bed_type = bt
102                            bed.bed_number = len(self) + 1
103                            bed.owner = NOT_OCCUPIED
104                            self.addBed(bed)
105                            added_counter +=1
106        return removed_counter, added_counter, modified_counter
107
108Hostel = attrs_to_fields(Hostel)
109
110class Bed(grok.Container):
111    """This is a bed.
112    """
113    grok.implements(IBed)
114    grok.provides(IBed)
115
116    def getBedCoordinates(self):
117        """Determine the coordinates from the bed_id.
118        """
119        return self.bed_id.split('_')
120
121    def switchReservation(self):
122        """Reserves a bed or relases a reserved bed respectively.
123        """
124        sh, sex, bt = self.bed_type.split('_')
125        hostel_id, block, room_nr, bed = self.getBedCoordinates()
126        beds_for_fresh = getattr(self,'beds_for_fresh',[])
127        beds_for_pre = getattr(self,'beds_for_pre',[])
128        beds_for_returning = getattr(self,'beds_for_returning',[])
129        beds_for_final = getattr(self,'beds_for_final',[])
130        hostel = self.__parent__
131        bed_string = u'%s_%s_%s' % (block, room_nr, bed)
132        if bt == 'reserved':
133            bt = 'all'
134            if bed in beds_for_fresh:
135                bt = 'fr'
136            elif bed in beds_for_pre:
137                bt = 'pr'
138            elif bed in beds_for_final:
139                bt = 'fi'
140            elif bed in beds_for_returning:
141                bt = 're'
142            bt = u'%s_%s_%s' % (sh, sex, bt)
143            hostel.beds_reserved.remove(bed_string)
144        else:
145            bt = u'%s_%s_reserved' % (sh, sex)
146            hostel.beds_reserved.append(bed_string)
147        self.bed_type = bt
148        return
149
150    def loggerInfo(self, ob_class, comment=None):
151        target = self.__name__
152        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
153
154Bed = attrs_to_fields(Bed)
Note: See TracBrowser for help on using the repository browser.