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 | """ |
---|
17 | These are the hostels. |
---|
18 | """ |
---|
19 | import grok |
---|
20 | from datetime import datetime |
---|
21 | from grok import index |
---|
22 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
23 | from waeup.sirp.hostels.interfaces import IHostel, IBed |
---|
24 | |
---|
25 | NOT_OCCUPIED = u'not occupied' |
---|
26 | |
---|
27 | class 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 | modified_beds = u'' |
---|
53 | |
---|
54 | # Remove all empty beds. Occupied beds remain in hostel! |
---|
55 | keys = list(self.keys()) # create list copy |
---|
56 | for key in keys: |
---|
57 | if self[key].owner == NOT_OCCUPIED: |
---|
58 | del self[key] |
---|
59 | self._p_changed = True |
---|
60 | removed_counter += 1 |
---|
61 | else: |
---|
62 | self[key].bed_number = 9999 |
---|
63 | remaining = len(keys) - removed_counter |
---|
64 | |
---|
65 | blocks_for_female = getattr(self,'blocks_for_female',[]) |
---|
66 | blocks_for_male = getattr(self,'blocks_for_male',[]) |
---|
67 | beds_for_fresh = getattr(self,'beds_for_fresh',[]) |
---|
68 | beds_for_pre = getattr(self,'beds_for_pre',[]) |
---|
69 | beds_for_returning = getattr(self,'beds_for_returning',[]) |
---|
70 | beds_for_final = getattr(self,'beds_for_final',[]) |
---|
71 | beds_for_all = getattr(self,'beds_for_all',[]) |
---|
72 | beds_reserved = getattr(self,'beds_reserved',[]) |
---|
73 | all_blocks = blocks_for_female + blocks_for_male |
---|
74 | all_beds = (beds_for_pre + beds_for_fresh + |
---|
75 | beds_for_returning + beds_for_final + beds_for_all) |
---|
76 | for block in all_blocks: |
---|
77 | sex = 'male' |
---|
78 | if block in blocks_for_female: |
---|
79 | sex = 'female' |
---|
80 | for floor in range(1,int(self.floors_per_block)+1): |
---|
81 | for room in range(1,int(self.rooms_per_floor)+1): |
---|
82 | for bed in all_beds: |
---|
83 | room_nr = floor*100 + room |
---|
84 | bt = 'all' |
---|
85 | if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved: |
---|
86 | bt = "reserved" |
---|
87 | elif bed in beds_for_fresh: |
---|
88 | bt = 'fr' |
---|
89 | elif bed in beds_for_pre: |
---|
90 | bt = 'pr' |
---|
91 | elif bed in beds_for_final: |
---|
92 | bt = 'fi' |
---|
93 | elif bed in beds_for_returning: |
---|
94 | bt = 're' |
---|
95 | bt = u'%s_%s_%s' % (self.special_handling,sex,bt) |
---|
96 | uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed) |
---|
97 | if self.has_key(uid): |
---|
98 | bed = self[uid] |
---|
99 | # Renumber remaining beds |
---|
100 | bed.bed_number = len(self) + 1 - remaining |
---|
101 | remaining -= 1 |
---|
102 | if bed.bed_type != bt: |
---|
103 | bed.bed_type = bt |
---|
104 | modified_counter += 1 |
---|
105 | modified_beds += '%s ' % uid |
---|
106 | else: |
---|
107 | bed = Bed() |
---|
108 | bed.bed_id = uid |
---|
109 | bed.bed_type = bt |
---|
110 | bed.bed_number = len(self) + 1 - remaining |
---|
111 | bed.owner = NOT_OCCUPIED |
---|
112 | self.addBed(bed) |
---|
113 | added_counter +=1 |
---|
114 | return removed_counter, added_counter, modified_counter, modified_beds |
---|
115 | |
---|
116 | Hostel = attrs_to_fields(Hostel) |
---|
117 | |
---|
118 | class Bed(grok.Container): |
---|
119 | """This is a bed. |
---|
120 | """ |
---|
121 | grok.implements(IBed) |
---|
122 | grok.provides(IBed) |
---|
123 | |
---|
124 | def getBedCoordinates(self): |
---|
125 | """Determine the coordinates from the bed_id. |
---|
126 | """ |
---|
127 | return self.bed_id.split('_') |
---|
128 | |
---|
129 | def bookBed(self, student_id): |
---|
130 | if self.owner == NOT_OCCUPIED: |
---|
131 | self.owner = student_id |
---|
132 | return None |
---|
133 | else: |
---|
134 | return self.owner |
---|
135 | |
---|
136 | def switchReservation(self): |
---|
137 | """Reserves a bed or unreserve bed respectively. |
---|
138 | """ |
---|
139 | sh, sex, bt = self.bed_type.split('_') |
---|
140 | hostel_id, block, room_nr, bed = self.getBedCoordinates() |
---|
141 | hostel = self.__parent__ |
---|
142 | beds_for_fresh = getattr(hostel,'beds_for_fresh',[]) |
---|
143 | beds_for_pre = getattr(hostel,'beds_for_pre',[]) |
---|
144 | beds_for_returning = getattr(hostel,'beds_for_returning',[]) |
---|
145 | beds_for_final = getattr(hostel,'beds_for_final',[]) |
---|
146 | bed_string = u'%s_%s_%s' % (block, room_nr, bed) |
---|
147 | if bt == 'reserved': |
---|
148 | bt = 'all' |
---|
149 | if bed in beds_for_fresh: |
---|
150 | bt = 'fr' |
---|
151 | elif bed in beds_for_pre: |
---|
152 | bt = 'pr' |
---|
153 | elif bed in beds_for_final: |
---|
154 | bt = 'fi' |
---|
155 | elif bed in beds_for_returning: |
---|
156 | bt = 're' |
---|
157 | bt = u'%s_%s_%s' % (sh, sex, bt) |
---|
158 | hostel.beds_reserved.remove(bed_string) |
---|
159 | message = u'unreserved' |
---|
160 | else: |
---|
161 | bt = u'%s_%s_reserved' % (sh, sex) |
---|
162 | hostel.beds_reserved.append(bed_string) |
---|
163 | # Comment of Martijn: |
---|
164 | # If you have a non-Persistent subobject (like a list) and you change it, |
---|
165 | # you need to manually flag the persistence machinery on the object that |
---|
166 | # its subobject changed, with _p_changed. This is only necessary if some |
---|
167 | # of the objects are not sublclasses of Persistent. For common built-in |
---|
168 | # collections in Python such as list and dictionary there are replacements |
---|
169 | # (PersistentList, PersistentMapping), and more advanced building blocks |
---|
170 | # for indexes (BTrees), that don't have this issue. |
---|
171 | hostel._p_changed = True |
---|
172 | message = u'reserved' |
---|
173 | self.bed_type = bt |
---|
174 | return message |
---|
175 | |
---|
176 | def loggerInfo(self, ob_class, comment=None): |
---|
177 | target = self.__name__ |
---|
178 | return grok.getSite()['hostels'].logger_info(ob_class,target,comment) |
---|
179 | |
---|
180 | Bed = attrs_to_fields(Bed) |
---|