1 | ## $Id: interfaces.py 17176 2022-11-25 11:54:59Z 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, Interface |
---|
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 | from waeup.kofa.students.interfaces import (IStudentsUtils) |
---|
31 | |
---|
32 | # Define a validation method for sort ids |
---|
33 | class NotASortId(schema.ValidationError): |
---|
34 | __doc__ = u"Invalid sort_id" |
---|
35 | |
---|
36 | def validate_sort_id(value): |
---|
37 | if not value < 1000: |
---|
38 | raise NotASortId(value) |
---|
39 | return True |
---|
40 | |
---|
41 | class IHostelsContainer(IKofaObject): |
---|
42 | """A container for hostel objects. |
---|
43 | """ |
---|
44 | |
---|
45 | expired = Attribute('True if current datetime is in application period.') |
---|
46 | statistics = Attribute('Bed category statistics') |
---|
47 | |
---|
48 | startdate = schema.Datetime( |
---|
49 | title = _(u'Hostel Allocation Start Date'), |
---|
50 | required = False, |
---|
51 | description = _('Example: ') + u'2011-12-01 18:30:00+01:00', |
---|
52 | ) |
---|
53 | |
---|
54 | enddate = schema.Datetime( |
---|
55 | title = _(u'Hostel Allocation Closing Date'), |
---|
56 | required = False, |
---|
57 | description = _('Example: ') + u'2011-12-31 23:59:59+01:00', |
---|
58 | ) |
---|
59 | |
---|
60 | accommodation_session = schema.Choice( |
---|
61 | title = _(u'Booking Session'), |
---|
62 | source = academic_sessions_vocab, |
---|
63 | #default = datetime.now().year, |
---|
64 | required = False, |
---|
65 | readonly = False, |
---|
66 | ) |
---|
67 | |
---|
68 | accommodation_states = schema.List( |
---|
69 | title = _(u'Allowed States'), |
---|
70 | value_type = schema.Choice( |
---|
71 | vocabulary = registration_states_vocab, |
---|
72 | ), |
---|
73 | defaultFactory=list, |
---|
74 | ) |
---|
75 | |
---|
76 | allocation_expiration = schema.Int( |
---|
77 | title = _(u'Allocation Expiration Time (days)'), |
---|
78 | description = _( |
---|
79 | 'Number of days after which allocation is being annulled'), |
---|
80 | required = False, |
---|
81 | ) |
---|
82 | |
---|
83 | def clearAllHostels(): |
---|
84 | """Clear all hostels. |
---|
85 | """ |
---|
86 | |
---|
87 | def addHostel(hostel): |
---|
88 | """Add a hostel. |
---|
89 | """ |
---|
90 | |
---|
91 | def releaseExpiredAllocations(n): |
---|
92 | """Release bed if bed allocation has expired. Allocation expires |
---|
93 | after `n` days if maintenance fee has not been paid. |
---|
94 | """ |
---|
95 | |
---|
96 | def writeLogMessage(view, message): |
---|
97 | """Add an INFO message to hostels.log. |
---|
98 | """ |
---|
99 | |
---|
100 | class IHostel(IKofaObject): |
---|
101 | """Representation of a hostel. |
---|
102 | """ |
---|
103 | |
---|
104 | bed_statistics = Attribute('Number of booked and total beds') |
---|
105 | |
---|
106 | def clearHostel(): |
---|
107 | """Remove all beds. |
---|
108 | """ |
---|
109 | |
---|
110 | def updateBeds(): |
---|
111 | """Fill hostel with beds or update beds. |
---|
112 | """ |
---|
113 | |
---|
114 | hostel_id = schema.TextLine( |
---|
115 | title = _(u'Hostel Id'), |
---|
116 | ) |
---|
117 | |
---|
118 | sort_id = schema.Int( |
---|
119 | title = _(u'Sort Id'), |
---|
120 | required = True, |
---|
121 | default = 10, |
---|
122 | constraint=validate_sort_id, |
---|
123 | ) |
---|
124 | |
---|
125 | hostel_name = schema.TextLine( |
---|
126 | title = _(u'Hostel Name'), |
---|
127 | required = True, |
---|
128 | default = u'Hall 1', |
---|
129 | ) |
---|
130 | |
---|
131 | floors_per_block = schema.Int( |
---|
132 | title = _(u'Floors per Block'), |
---|
133 | required = True, |
---|
134 | default = 1, |
---|
135 | ) |
---|
136 | |
---|
137 | rooms_per_floor = schema.Int( |
---|
138 | title = _(u'Rooms per Floor'), |
---|
139 | required = True, |
---|
140 | default = 2, |
---|
141 | ) |
---|
142 | |
---|
143 | blocks_for_female = schema.List( |
---|
144 | title = _(u'Blocks for Female Students'), |
---|
145 | value_type = schema.Choice( |
---|
146 | vocabulary = blocks |
---|
147 | ), |
---|
148 | defaultFactory=list, |
---|
149 | ) |
---|
150 | |
---|
151 | blocks_for_male = schema.List( |
---|
152 | title = _(u'Blocks for Male Students'), |
---|
153 | value_type = schema.Choice( |
---|
154 | vocabulary = blocks |
---|
155 | ), |
---|
156 | defaultFactory=list, |
---|
157 | ) |
---|
158 | |
---|
159 | beds_for_pre= schema.List( |
---|
160 | title = _(u'Beds for Pre-Study Students'), |
---|
161 | value_type = schema.Choice( |
---|
162 | vocabulary = bed_letters |
---|
163 | ), |
---|
164 | defaultFactory=list, |
---|
165 | ) |
---|
166 | |
---|
167 | beds_for_fresh = schema.List( |
---|
168 | title = _(u'Beds for Fresh Students'), |
---|
169 | value_type = schema.Choice( |
---|
170 | vocabulary = bed_letters |
---|
171 | ), |
---|
172 | defaultFactory=list, |
---|
173 | ) |
---|
174 | |
---|
175 | beds_for_returning = schema.List( |
---|
176 | title = _(u'Beds for Returning Students'), |
---|
177 | value_type = schema.Choice( |
---|
178 | vocabulary = bed_letters |
---|
179 | ), |
---|
180 | defaultFactory=list, |
---|
181 | ) |
---|
182 | |
---|
183 | beds_for_final = schema.List( |
---|
184 | title = _(u'Beds for Final Year Students'), |
---|
185 | value_type = schema.Choice( |
---|
186 | vocabulary = bed_letters |
---|
187 | ), |
---|
188 | defaultFactory=list, |
---|
189 | ) |
---|
190 | |
---|
191 | beds_for_all = schema.List( |
---|
192 | title = _(u'Beds without category'), |
---|
193 | value_type = schema.Choice( |
---|
194 | vocabulary = bed_letters |
---|
195 | ), |
---|
196 | defaultFactory=list, |
---|
197 | ) |
---|
198 | |
---|
199 | special_handling = schema.Choice( |
---|
200 | title = _(u'Special Handling'), |
---|
201 | source = SpecialHandlingSource(), |
---|
202 | required = True, |
---|
203 | default = u'regular', |
---|
204 | ) |
---|
205 | |
---|
206 | maint_fee = schema.Float( |
---|
207 | title = _(u'Rent'), |
---|
208 | default = 0.0, |
---|
209 | required = False, |
---|
210 | ) |
---|
211 | |
---|
212 | @invariant |
---|
213 | def blocksOverlap(hostel): |
---|
214 | bfe = hostel.blocks_for_female |
---|
215 | bma = hostel.blocks_for_male |
---|
216 | if set(bfe).intersection(set(bma)): |
---|
217 | raise Invalid(_('Female and male blocks overlap.')) |
---|
218 | |
---|
219 | @invariant |
---|
220 | def bedsOverlap(hostel): |
---|
221 | beds = (hostel.beds_for_fresh + |
---|
222 | hostel.beds_for_returning + |
---|
223 | hostel.beds_for_final + |
---|
224 | hostel.beds_for_pre + |
---|
225 | hostel.beds_for_all) |
---|
226 | if len(beds) != len(set(beds)): |
---|
227 | raise Invalid(_('Bed categories overlap.')) |
---|
228 | |
---|
229 | def writeLogMessage(view, message): |
---|
230 | """Add an INFO message to hostels.log. |
---|
231 | """ |
---|
232 | |
---|
233 | class IBed(IKofaObject): |
---|
234 | """Representation of a bed. |
---|
235 | """ |
---|
236 | |
---|
237 | coordinates = Attribute('Coordinates tuple derived from bed_id') |
---|
238 | hall = Attribute('Hall id, for exporter only') |
---|
239 | block = Attribute('Block letter, for exporter only') |
---|
240 | room = Attribute('Room number, for exporter only') |
---|
241 | bed = Attribute('Bed letter, for exporter only') |
---|
242 | special_handling = Attribute('Special handling code, for exporter only') |
---|
243 | sex = Attribute('Sex, for exporter only') |
---|
244 | bt = Attribute('Last part of bed type, for exporter only') |
---|
245 | |
---|
246 | def bookBed(student_id): |
---|
247 | """Book a bed for a student. |
---|
248 | """ |
---|
249 | |
---|
250 | def switchReservation(): |
---|
251 | """Reserves bed or relases reserved bed respectively. |
---|
252 | """ |
---|
253 | |
---|
254 | def releaseBedIfMaintenanceNotPaid(): |
---|
255 | """Release bed if maintenance fee has not been paid on time. |
---|
256 | Reserve bed so that it cannot be automatically booked by someone else. |
---|
257 | """ |
---|
258 | |
---|
259 | bed_id = schema.TextLine( |
---|
260 | title = _(u'Bed Id'), |
---|
261 | required = True, |
---|
262 | default = u'', |
---|
263 | ) |
---|
264 | |
---|
265 | bed_type = schema.TextLine( |
---|
266 | title = _(u'Bed Type'), |
---|
267 | required = True, |
---|
268 | default = u'', |
---|
269 | ) |
---|
270 | |
---|
271 | bed_number = schema.Int( |
---|
272 | title = _(u'Bed Number'), |
---|
273 | required = True, |
---|
274 | ) |
---|
275 | |
---|
276 | owner = schema.TextLine( |
---|
277 | title = _(u'Owner (Student)'), |
---|
278 | description = _('Enter valid student id.'), |
---|
279 | required = True, |
---|
280 | default = u'', |
---|
281 | ) |
---|
282 | |
---|
283 | @invariant |
---|
284 | def allowed_owners(bed): |
---|
285 | if bed.owner == NOT_OCCUPIED: |
---|
286 | return |
---|
287 | catalog = getUtility(ICatalog, name='students_catalog') |
---|
288 | accommodation_span = getUtility(IStudentsUtils).ACCOMMODATION_SPAN |
---|
289 | accommodation_session = getSite()['hostels'].accommodation_session |
---|
290 | students = catalog.searchResults(current_session=( |
---|
291 | accommodation_session-accommodation_span,accommodation_session)) |
---|
292 | student_ids = [student.student_id for student in students] |
---|
293 | if not bed.owner in student_ids: |
---|
294 | raise Invalid(_( |
---|
295 | "Either student does not exist or student " |
---|
296 | "is not eligible to book accommodation.")) |
---|
297 | catalog = getUtility(ICatalog, name='beds_catalog') |
---|
298 | beds = catalog.searchResults(owner=(bed.owner,bed.owner)) |
---|
299 | if len(beds): |
---|
300 | allocated_bed = [bed.bed_id for bed in beds][0] |
---|
301 | raise Invalid(_( |
---|
302 | "This student resides in bed ${a}.", |
---|
303 | mapping = {'a':allocated_bed})) |
---|
304 | |
---|
305 | def writeLogMessage(view, message): |
---|
306 | """Add an INFO message to hostels.log. |
---|
307 | """ |
---|
308 | |
---|
309 | class IHostelsUtils(Interface): |
---|
310 | """A collection of methods which are subject to customization. |
---|
311 | """ |
---|
312 | |
---|
313 | def getBedStatistics(): |
---|
314 | """Return bed statistics. |
---|
315 | """ |
---|