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