1 | ## $Id: interfaces.py 13167 2015-07-13 08:37:27Z 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 | from grok import getSite |
---|
19 | from datetime import datetime |
---|
20 | from zope.component import getUtility |
---|
21 | from zope.catalog.interfaces import ICatalog |
---|
22 | from zope.interface import invariant, Invalid, Attribute |
---|
23 | from zope import schema |
---|
24 | from waeup.kofa.interfaces import ( |
---|
25 | IKofaObject, academic_sessions_vocab, registration_states_vocab) |
---|
26 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
27 | from waeup.kofa.hostels.vocabularies import ( |
---|
28 | bed_letters, blocks, SpecialHandlingSource, |
---|
29 | NOT_OCCUPIED) |
---|
30 | |
---|
31 | class IHostelsContainer(IKofaObject): |
---|
32 | """A container for hostel objects. |
---|
33 | """ |
---|
34 | |
---|
35 | expired = Attribute('True if current datetime is in application period.') |
---|
36 | |
---|
37 | startdate = schema.Datetime( |
---|
38 | title = _(u'Hostel Allocation Start Date'), |
---|
39 | required = False, |
---|
40 | description = _('Example:') + u'2011-12-01 18:30:00+01:00', |
---|
41 | ) |
---|
42 | |
---|
43 | enddate = schema.Datetime( |
---|
44 | title = _(u'Hostel Allocation Closing Date'), |
---|
45 | required = False, |
---|
46 | description = _('Example:') + u'2011-12-31 23:59:59+01:00', |
---|
47 | ) |
---|
48 | |
---|
49 | accommodation_session = schema.Choice( |
---|
50 | title = _(u'Booking Session'), |
---|
51 | source = academic_sessions_vocab, |
---|
52 | default = datetime.now().year, |
---|
53 | required = False, |
---|
54 | readonly = False, |
---|
55 | ) |
---|
56 | |
---|
57 | accommodation_states = schema.List( |
---|
58 | title = _(u'Allowed States'), |
---|
59 | value_type = schema.Choice( |
---|
60 | vocabulary = registration_states_vocab, |
---|
61 | ), |
---|
62 | default = [], |
---|
63 | ) |
---|
64 | |
---|
65 | def clearAllHostels(): |
---|
66 | """Clear all hostels. |
---|
67 | """ |
---|
68 | |
---|
69 | def addHostel(hostel): |
---|
70 | """Add a hostel. |
---|
71 | """ |
---|
72 | |
---|
73 | def writeLogMessage(view, message): |
---|
74 | """Add an INFO message to hostels.log. |
---|
75 | """ |
---|
76 | |
---|
77 | class IHostel(IKofaObject): |
---|
78 | """Representation of a hostel. |
---|
79 | """ |
---|
80 | |
---|
81 | bed_statistics = Attribute('Number of booked and total beds') |
---|
82 | |
---|
83 | def clearHostel(): |
---|
84 | """Remove all beds. |
---|
85 | """ |
---|
86 | |
---|
87 | def updateBeds(): |
---|
88 | """Fill hostel with beds or update beds. |
---|
89 | """ |
---|
90 | |
---|
91 | hostel_id = schema.TextLine( |
---|
92 | title = _(u'Hostel Id'), |
---|
93 | ) |
---|
94 | |
---|
95 | sort_id = schema.Int( |
---|
96 | title = _(u'Sort Id'), |
---|
97 | required = True, |
---|
98 | default = 10, |
---|
99 | ) |
---|
100 | |
---|
101 | hostel_name = schema.TextLine( |
---|
102 | title = _(u'Hostel Name'), |
---|
103 | required = True, |
---|
104 | default = u'Hall 1', |
---|
105 | ) |
---|
106 | |
---|
107 | floors_per_block = schema.Int( |
---|
108 | title = _(u'Floors per Block'), |
---|
109 | required = True, |
---|
110 | default = 1, |
---|
111 | ) |
---|
112 | |
---|
113 | rooms_per_floor = schema.Int( |
---|
114 | title = _(u'Rooms per Floor'), |
---|
115 | required = True, |
---|
116 | default = 2, |
---|
117 | ) |
---|
118 | |
---|
119 | beds_reserved = schema.List( |
---|
120 | title = _(u'Reserved Beds'), |
---|
121 | value_type = schema.TextLine( |
---|
122 | default = u'', |
---|
123 | required = False, |
---|
124 | ), |
---|
125 | required = True, |
---|
126 | readonly = False, |
---|
127 | default = [], |
---|
128 | ) |
---|
129 | |
---|
130 | blocks_for_female = schema.List( |
---|
131 | title = _(u'Blocks for Female Students'), |
---|
132 | value_type = schema.Choice( |
---|
133 | vocabulary = blocks |
---|
134 | ), |
---|
135 | ) |
---|
136 | |
---|
137 | blocks_for_male = schema.List( |
---|
138 | title = _(u'Blocks for Male Students'), |
---|
139 | value_type = schema.Choice( |
---|
140 | vocabulary = blocks |
---|
141 | ), |
---|
142 | ) |
---|
143 | |
---|
144 | beds_for_pre= schema.List( |
---|
145 | title = _(u'Beds for Pre-Study Students'), |
---|
146 | value_type = schema.Choice( |
---|
147 | vocabulary = bed_letters |
---|
148 | ), |
---|
149 | ) |
---|
150 | |
---|
151 | beds_for_fresh = schema.List( |
---|
152 | title = _(u'Beds for Fresh Students'), |
---|
153 | value_type = schema.Choice( |
---|
154 | vocabulary = bed_letters |
---|
155 | ), |
---|
156 | ) |
---|
157 | |
---|
158 | beds_for_returning = schema.List( |
---|
159 | title = _(u'Beds for Returning Students'), |
---|
160 | value_type = schema.Choice( |
---|
161 | vocabulary = bed_letters |
---|
162 | ), |
---|
163 | ) |
---|
164 | |
---|
165 | beds_for_final = schema.List( |
---|
166 | title = _(u'Beds for Final Year Students'), |
---|
167 | value_type = schema.Choice( |
---|
168 | vocabulary = bed_letters |
---|
169 | ), |
---|
170 | ) |
---|
171 | |
---|
172 | beds_for_all = schema.List( |
---|
173 | title = _(u'Beds without category'), |
---|
174 | value_type = schema.Choice( |
---|
175 | vocabulary = bed_letters |
---|
176 | ), |
---|
177 | ) |
---|
178 | |
---|
179 | special_handling = schema.Choice( |
---|
180 | title = _(u'Special Handling'), |
---|
181 | source = SpecialHandlingSource(), |
---|
182 | required = True, |
---|
183 | default = u'regular', |
---|
184 | ) |
---|
185 | |
---|
186 | maint_fee = schema.Float( |
---|
187 | title = _(u'Rent'), |
---|
188 | default = 0.0, |
---|
189 | required = False, |
---|
190 | ) |
---|
191 | |
---|
192 | @invariant |
---|
193 | def blocksOverlap(hostel): |
---|
194 | bfe = hostel.blocks_for_female |
---|
195 | bma = hostel.blocks_for_male |
---|
196 | if set(bfe).intersection(set(bma)): |
---|
197 | raise Invalid(_('Female and male blocks overlap.')) |
---|
198 | |
---|
199 | @invariant |
---|
200 | def bedsOverlap(hostel): |
---|
201 | beds = (hostel.beds_for_fresh + |
---|
202 | hostel.beds_for_returning + |
---|
203 | hostel.beds_for_final + |
---|
204 | hostel.beds_for_pre + |
---|
205 | hostel.beds_for_all) |
---|
206 | if len(beds) != len(set(beds)): |
---|
207 | raise Invalid(_('Bed categories overlap.')) |
---|
208 | |
---|
209 | def writeLogMessage(view, message): |
---|
210 | """Add an INFO message to hostels.log. |
---|
211 | """ |
---|
212 | |
---|
213 | class IBed(IKofaObject): |
---|
214 | """Representation of a bed. |
---|
215 | """ |
---|
216 | |
---|
217 | coordinates = Attribute('The coordinates of the bed from bed_id') |
---|
218 | |
---|
219 | def bookBed(student_id): |
---|
220 | """Book a bed for a student. |
---|
221 | """ |
---|
222 | |
---|
223 | def switchReservation(): |
---|
224 | """Reserves bed or relases reserved bed respectively. |
---|
225 | """ |
---|
226 | |
---|
227 | bed_id = schema.TextLine( |
---|
228 | title = _(u'Bed Id'), |
---|
229 | required = True, |
---|
230 | default = u'', |
---|
231 | ) |
---|
232 | |
---|
233 | bed_type = schema.TextLine( |
---|
234 | title = _(u'Bed Type'), |
---|
235 | required = True, |
---|
236 | default = u'', |
---|
237 | ) |
---|
238 | |
---|
239 | bed_number = schema.Int( |
---|
240 | title = _(u'Bed Number'), |
---|
241 | required = True, |
---|
242 | ) |
---|
243 | |
---|
244 | owner = schema.TextLine( |
---|
245 | title = _(u'Owner (Student)'), |
---|
246 | description = _('Enter valid student id.'), |
---|
247 | required = True, |
---|
248 | default = u'', |
---|
249 | ) |
---|
250 | |
---|
251 | @invariant |
---|
252 | def allowed_owners(bed): |
---|
253 | if bed.owner == NOT_OCCUPIED: |
---|
254 | return |
---|
255 | catalog = getUtility(ICatalog, name='students_catalog') |
---|
256 | accommodation_session = getSite()['hostels'].accommodation_session |
---|
257 | students = catalog.searchResults(current_session=( |
---|
258 | accommodation_session,accommodation_session)) |
---|
259 | student_ids = [student.student_id for student in students] |
---|
260 | if not bed.owner in student_ids: |
---|
261 | raise Invalid(_( |
---|
262 | "Either student does not exist or student " |
---|
263 | "is not in accommodation session.")) |
---|
264 | catalog = getUtility(ICatalog, name='beds_catalog') |
---|
265 | beds = catalog.searchResults(owner=(bed.owner,bed.owner)) |
---|
266 | if len(beds): |
---|
267 | allocated_bed = [bed.bed_id for bed in beds][0] |
---|
268 | raise Invalid(_( |
---|
269 | "This student resides in bed ${a}.", mapping = {'a':allocated_bed})) |
---|
270 | |
---|
271 | def writeLogMessage(view, message): |
---|
272 | """Add an INFO message to hostels.log. |
---|
273 | """ |
---|