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