1 | ## $Id: hostel.py 9197 2012-09-18 16:24:21Z 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 | """ |
---|
19 | These are the hostels. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from zope.event import notify |
---|
23 | from zope.component import getUtility |
---|
24 | from datetime import datetime |
---|
25 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
26 | from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED |
---|
27 | from waeup.kofa.hostels.interfaces import IHostel, IBed, IBedAllocateStudent |
---|
28 | from waeup.kofa.students.interfaces import IBedTicket |
---|
29 | from waeup.kofa.interfaces import IKofaUtils |
---|
30 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
31 | from waeup.kofa.utils.helpers import now |
---|
32 | |
---|
33 | class Hostel(grok.Container): |
---|
34 | """This is a hostel. |
---|
35 | """ |
---|
36 | grok.implements(IHostel) |
---|
37 | grok.provides(IHostel) |
---|
38 | |
---|
39 | def loggerInfo(self, ob_class, comment=None): |
---|
40 | target = self.__name__ |
---|
41 | return grok.getSite()['hostels'].logger_info(ob_class,target,comment) |
---|
42 | |
---|
43 | @property |
---|
44 | def bed_statistics(self): |
---|
45 | total = len(self.keys()) |
---|
46 | booked = 0 |
---|
47 | for value in self.values(): |
---|
48 | if value.owner != NOT_OCCUPIED: |
---|
49 | booked += 1 |
---|
50 | return {'booked':booked, 'total':total} |
---|
51 | |
---|
52 | def clearHostel(self): |
---|
53 | """Remove all beds |
---|
54 | """ |
---|
55 | keys = [i for i in self.keys()] # create deep copy |
---|
56 | for bed in keys: |
---|
57 | del self[bed] |
---|
58 | return |
---|
59 | |
---|
60 | def addBed(self, bed): |
---|
61 | """Add a bed. |
---|
62 | """ |
---|
63 | if not IBed.providedBy(bed): |
---|
64 | raise TypeError( |
---|
65 | 'Hostels contain only IBed instances') |
---|
66 | self[bed.bed_id] = bed |
---|
67 | return |
---|
68 | |
---|
69 | def updateBeds(self): |
---|
70 | """Fill hostel with beds or update beds. |
---|
71 | """ |
---|
72 | added_counter = 0 |
---|
73 | modified_counter = 0 |
---|
74 | removed_counter = 0 |
---|
75 | modified_beds = u'' |
---|
76 | |
---|
77 | # Remove all empty beds. Occupied beds remain in hostel! |
---|
78 | keys = list(self.keys()) # create list copy |
---|
79 | for key in keys: |
---|
80 | if self[key].owner == NOT_OCCUPIED: |
---|
81 | del self[key] |
---|
82 | self._p_changed = True |
---|
83 | removed_counter += 1 |
---|
84 | else: |
---|
85 | self[key].bed_number = 9999 |
---|
86 | remaining = len(keys) - removed_counter |
---|
87 | |
---|
88 | blocks_for_female = getattr(self,'blocks_for_female',[]) |
---|
89 | blocks_for_male = getattr(self,'blocks_for_male',[]) |
---|
90 | beds_for_fresh = getattr(self,'beds_for_fresh',[]) |
---|
91 | beds_for_pre = getattr(self,'beds_for_pre',[]) |
---|
92 | beds_for_returning = getattr(self,'beds_for_returning',[]) |
---|
93 | beds_for_final = getattr(self,'beds_for_final',[]) |
---|
94 | beds_for_all = getattr(self,'beds_for_all',[]) |
---|
95 | beds_reserved = getattr(self,'beds_reserved',[]) |
---|
96 | all_blocks = blocks_for_female + blocks_for_male |
---|
97 | all_beds = (beds_for_pre + beds_for_fresh + |
---|
98 | beds_for_returning + beds_for_final + beds_for_all) |
---|
99 | for block in all_blocks: |
---|
100 | sex = 'male' |
---|
101 | if block in blocks_for_female: |
---|
102 | sex = 'female' |
---|
103 | for floor in range(1,int(self.floors_per_block)+1): |
---|
104 | for room in range(1,int(self.rooms_per_floor)+1): |
---|
105 | for bed in all_beds: |
---|
106 | room_nr = floor*100 + room |
---|
107 | bt = 'all' |
---|
108 | if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved: |
---|
109 | bt = "reserved" |
---|
110 | elif bed in beds_for_fresh: |
---|
111 | bt = 'fr' |
---|
112 | elif bed in beds_for_pre: |
---|
113 | bt = 'pr' |
---|
114 | elif bed in beds_for_final: |
---|
115 | bt = 'fi' |
---|
116 | elif bed in beds_for_returning: |
---|
117 | bt = 're' |
---|
118 | bt = u'%s_%s_%s' % (self.special_handling,sex,bt) |
---|
119 | uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed) |
---|
120 | if self.has_key(uid): |
---|
121 | bed = self[uid] |
---|
122 | # Renumber remaining beds |
---|
123 | bed.bed_number = len(self) + 1 - remaining |
---|
124 | remaining -= 1 |
---|
125 | if bed.bed_type != bt: |
---|
126 | bed.bed_type = bt |
---|
127 | modified_counter += 1 |
---|
128 | modified_beds += '%s ' % uid |
---|
129 | else: |
---|
130 | bed = Bed() |
---|
131 | bed.bed_id = uid |
---|
132 | bed.bed_type = bt |
---|
133 | bed.bed_number = len(self) + 1 - remaining |
---|
134 | bed.owner = NOT_OCCUPIED |
---|
135 | self.addBed(bed) |
---|
136 | added_counter +=1 |
---|
137 | return removed_counter, added_counter, modified_counter, modified_beds |
---|
138 | |
---|
139 | Hostel = attrs_to_fields(Hostel) |
---|
140 | |
---|
141 | class Bed(grok.Container): |
---|
142 | """This is a bed. |
---|
143 | """ |
---|
144 | grok.implements(IBed, IBedAllocateStudent) |
---|
145 | grok.provides(IBed) |
---|
146 | |
---|
147 | def getBedCoordinates(self): |
---|
148 | """Determine the coordinates from the bed_id. |
---|
149 | """ |
---|
150 | return self.bed_id.split('_') |
---|
151 | |
---|
152 | def bookBed(self, student_id): |
---|
153 | if self.owner == NOT_OCCUPIED: |
---|
154 | self.owner = student_id |
---|
155 | notify(grok.ObjectModifiedEvent(self)) |
---|
156 | return None |
---|
157 | else: |
---|
158 | return self.owner |
---|
159 | |
---|
160 | def switchReservation(self): |
---|
161 | """Reserves or unreserve bed respectively. |
---|
162 | """ |
---|
163 | sh, sex, bt = self.bed_type.split('_') |
---|
164 | hostel_id, block, room_nr, bed = self.getBedCoordinates() |
---|
165 | hostel = self.__parent__ |
---|
166 | beds_for_fresh = getattr(hostel,'beds_for_fresh',[]) |
---|
167 | beds_for_pre = getattr(hostel,'beds_for_pre',[]) |
---|
168 | beds_for_returning = getattr(hostel,'beds_for_returning',[]) |
---|
169 | beds_for_final = getattr(hostel,'beds_for_final',[]) |
---|
170 | bed_string = u'%s_%s_%s' % (block, room_nr, bed) |
---|
171 | if bt == 'reserved': |
---|
172 | bt = 'all' |
---|
173 | if bed in beds_for_fresh: |
---|
174 | bt = 'fr' |
---|
175 | elif bed in beds_for_pre: |
---|
176 | bt = 'pr' |
---|
177 | elif bed in beds_for_final: |
---|
178 | bt = 'fi' |
---|
179 | elif bed in beds_for_returning: |
---|
180 | bt = 're' |
---|
181 | bt = u'%s_%s_%s' % (sh, sex, bt) |
---|
182 | |
---|
183 | # Comment of Martijn: |
---|
184 | # If you have a non-Persistent subobject (like a list) and |
---|
185 | # you change it, you need to manually flag the persistence |
---|
186 | # machinery on the object that its subobject changed, with |
---|
187 | # _p_changed. This is only necessary if some of the |
---|
188 | # objects are not sublclasses of Persistent. For common |
---|
189 | # built-in collections in Python such as list and |
---|
190 | # dictionary there are replacements (PersistentList, |
---|
191 | # PersistentMapping), and more advanced building blocks |
---|
192 | # for indexes (BTrees), that don't have this issue. |
---|
193 | #hostel._p_changed = True |
---|
194 | |
---|
195 | # Henrik: Unfortunately, this doesn't work in our case. |
---|
196 | # After restarting the portal, |
---|
197 | # added or removed list items are gone. The only way to ensure |
---|
198 | # persistance is to reassign the list attribute. |
---|
199 | br = hostel.beds_reserved |
---|
200 | br.remove(bed_string) |
---|
201 | hostel.beds_reserved = br |
---|
202 | message = _(u'unreserved') |
---|
203 | else: |
---|
204 | bt = u'%s_%s_reserved' % (sh, sex) |
---|
205 | # See comment above |
---|
206 | br = hostel.beds_reserved |
---|
207 | br.append(bed_string) |
---|
208 | hostel.beds_reserved = br |
---|
209 | message = _(u'reserved') |
---|
210 | self.bed_type = bt |
---|
211 | return message |
---|
212 | |
---|
213 | def releaseBed(self): |
---|
214 | if self.owner == NOT_OCCUPIED: |
---|
215 | return |
---|
216 | else: |
---|
217 | old_owner = self.owner |
---|
218 | self.owner = NOT_OCCUPIED |
---|
219 | notify(grok.ObjectModifiedEvent(self)) |
---|
220 | accommodation_session = grok.getSite()[ |
---|
221 | 'hostels'].accommodation_session |
---|
222 | try: |
---|
223 | bedticket = grok.getSite()['students'][old_owner][ |
---|
224 | 'accommodation'][str(accommodation_session)] |
---|
225 | except KeyError: |
---|
226 | return '%s without bed ticket' % old_owner |
---|
227 | bedticket.bed = None |
---|
228 | tz = getUtility(IKofaUtils).tzinfo |
---|
229 | timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
230 | bedticket.bed_coordinates = u'-- booking cancelled on %s --' % ( |
---|
231 | timestamp,) |
---|
232 | return old_owner |
---|
233 | |
---|
234 | def loggerInfo(self, ob_class, comment=None): |
---|
235 | target = self.__name__ |
---|
236 | return grok.getSite()['hostels'].logger_info(ob_class,target,comment) |
---|
237 | |
---|
238 | Bed = attrs_to_fields(Bed) |
---|
239 | |
---|
240 | @grok.subscribe(IBedTicket, grok.IObjectRemovedEvent) |
---|
241 | def handle_bedticket_removed(bedticket, event): |
---|
242 | """If a bed ticket is deleted, we make sure that also the owner attribute |
---|
243 | of the bed is cleared (set to NOT_OCCUPIED). |
---|
244 | """ |
---|
245 | if bedticket.bed != None: |
---|
246 | bedticket.bed.owner = NOT_OCCUPIED |
---|
247 | notify(grok.ObjectModifiedEvent(bedticket.bed)) |
---|