1 | ## $Id: interfaces.py 13346 2015-10-26 08:57:30Z 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 releaseExpiredAllocations(n): |
---|
74 | """Release bed if bed allocation has expired. Allocation expires |
---|
75 | after `n` days if maintenance fee has not been paid. |
---|
76 | """ |
---|
77 | |
---|
78 | def writeLogMessage(view, message): |
---|
79 | """Add an INFO message to hostels.log. |
---|
80 | """ |
---|
81 | |
---|
82 | class IHostel(IKofaObject): |
---|
83 | """Representation of a hostel. |
---|
84 | """ |
---|
85 | |
---|
86 | bed_statistics = Attribute('Number of booked and total beds') |
---|
87 | |
---|
88 | def clearHostel(): |
---|
89 | """Remove all beds. |
---|
90 | """ |
---|
91 | |
---|
92 | def updateBeds(): |
---|
93 | """Fill hostel with beds or update beds. |
---|
94 | """ |
---|
95 | |
---|
96 | hostel_id = schema.TextLine( |
---|
97 | title = _(u'Hostel Id'), |
---|
98 | ) |
---|
99 | |
---|
100 | sort_id = schema.Int( |
---|
101 | title = _(u'Sort Id'), |
---|
102 | required = True, |
---|
103 | default = 10, |
---|
104 | ) |
---|
105 | |
---|
106 | hostel_name = schema.TextLine( |
---|
107 | title = _(u'Hostel Name'), |
---|
108 | required = True, |
---|
109 | default = u'Hall 1', |
---|
110 | ) |
---|
111 | |
---|
112 | floors_per_block = schema.Int( |
---|
113 | title = _(u'Floors per Block'), |
---|
114 | required = True, |
---|
115 | default = 1, |
---|
116 | ) |
---|
117 | |
---|
118 | rooms_per_floor = schema.Int( |
---|
119 | title = _(u'Rooms per Floor'), |
---|
120 | required = True, |
---|
121 | default = 2, |
---|
122 | ) |
---|
123 | |
---|
124 | blocks_for_female = schema.List( |
---|
125 | title = _(u'Blocks for Female Students'), |
---|
126 | value_type = schema.Choice( |
---|
127 | vocabulary = blocks |
---|
128 | ), |
---|
129 | default = [], |
---|
130 | ) |
---|
131 | |
---|
132 | blocks_for_male = schema.List( |
---|
133 | title = _(u'Blocks for Male Students'), |
---|
134 | value_type = schema.Choice( |
---|
135 | vocabulary = blocks |
---|
136 | ), |
---|
137 | default = [], |
---|
138 | ) |
---|
139 | |
---|
140 | beds_for_pre= schema.List( |
---|
141 | title = _(u'Beds for Pre-Study Students'), |
---|
142 | value_type = schema.Choice( |
---|
143 | vocabulary = bed_letters |
---|
144 | ), |
---|
145 | default = [], |
---|
146 | ) |
---|
147 | |
---|
148 | beds_for_fresh = schema.List( |
---|
149 | title = _(u'Beds for Fresh Students'), |
---|
150 | value_type = schema.Choice( |
---|
151 | vocabulary = bed_letters |
---|
152 | ), |
---|
153 | default = [], |
---|
154 | ) |
---|
155 | |
---|
156 | beds_for_returning = schema.List( |
---|
157 | title = _(u'Beds for Returning Students'), |
---|
158 | value_type = schema.Choice( |
---|
159 | vocabulary = bed_letters |
---|
160 | ), |
---|
161 | default = [], |
---|
162 | ) |
---|
163 | |
---|
164 | beds_for_final = schema.List( |
---|
165 | title = _(u'Beds for Final Year Students'), |
---|
166 | value_type = schema.Choice( |
---|
167 | vocabulary = bed_letters |
---|
168 | ), |
---|
169 | default = [], |
---|
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 | default = [], |
---|
178 | ) |
---|
179 | |
---|
180 | special_handling = schema.Choice( |
---|
181 | title = _(u'Special Handling'), |
---|
182 | source = SpecialHandlingSource(), |
---|
183 | required = True, |
---|
184 | default = u'regular', |
---|
185 | ) |
---|
186 | |
---|
187 | maint_fee = schema.Float( |
---|
188 | title = _(u'Rent'), |
---|
189 | default = 0.0, |
---|
190 | required = False, |
---|
191 | ) |
---|
192 | |
---|
193 | @invariant |
---|
194 | def blocksOverlap(hostel): |
---|
195 | bfe = hostel.blocks_for_female |
---|
196 | bma = hostel.blocks_for_male |
---|
197 | if set(bfe).intersection(set(bma)): |
---|
198 | raise Invalid(_('Female and male blocks overlap.')) |
---|
199 | |
---|
200 | @invariant |
---|
201 | def bedsOverlap(hostel): |
---|
202 | beds = (hostel.beds_for_fresh + |
---|
203 | hostel.beds_for_returning + |
---|
204 | hostel.beds_for_final + |
---|
205 | hostel.beds_for_pre + |
---|
206 | hostel.beds_for_all) |
---|
207 | if len(beds) != len(set(beds)): |
---|
208 | raise Invalid(_('Bed categories overlap.')) |
---|
209 | |
---|
210 | def writeLogMessage(view, message): |
---|
211 | """Add an INFO message to hostels.log. |
---|
212 | """ |
---|
213 | |
---|
214 | class IBed(IKofaObject): |
---|
215 | """Representation of a bed. |
---|
216 | """ |
---|
217 | |
---|
218 | coordinates = Attribute('Coordinates tuple derived from bed_id') |
---|
219 | hall = Attribute('Hall id, for exporter only') |
---|
220 | block = Attribute('Block letter, for exporter only') |
---|
221 | room = Attribute('Room number, for exporter only') |
---|
222 | bed = Attribute('Bed letter, for exporter only') |
---|
223 | special_handling = Attribute('Special handling code, for exporter only') |
---|
224 | sex = Attribute('Sex, for exporter only') |
---|
225 | bt = Attribute('Last part of bed type, for exporter only') |
---|
226 | |
---|
227 | def bookBed(student_id): |
---|
228 | """Book a bed for a student. |
---|
229 | """ |
---|
230 | |
---|
231 | def switchReservation(): |
---|
232 | """Reserves bed or relases reserved bed respectively. |
---|
233 | """ |
---|
234 | |
---|
235 | def releaseBedIfMaintenanceNotPaid(): |
---|
236 | """Release bed if maintenance fee has not been paid on time. |
---|
237 | """ |
---|
238 | |
---|
239 | bed_id = schema.TextLine( |
---|
240 | title = _(u'Bed Id'), |
---|
241 | required = True, |
---|
242 | default = u'', |
---|
243 | ) |
---|
244 | |
---|
245 | bed_type = schema.TextLine( |
---|
246 | title = _(u'Bed Type'), |
---|
247 | required = True, |
---|
248 | default = u'', |
---|
249 | ) |
---|
250 | |
---|
251 | bed_number = schema.Int( |
---|
252 | title = _(u'Bed Number'), |
---|
253 | required = True, |
---|
254 | ) |
---|
255 | |
---|
256 | owner = schema.TextLine( |
---|
257 | title = _(u'Owner (Student)'), |
---|
258 | description = _('Enter valid student id.'), |
---|
259 | required = True, |
---|
260 | default = u'', |
---|
261 | ) |
---|
262 | |
---|
263 | @invariant |
---|
264 | def allowed_owners(bed): |
---|
265 | if bed.owner == NOT_OCCUPIED: |
---|
266 | return |
---|
267 | catalog = getUtility(ICatalog, name='students_catalog') |
---|
268 | accommodation_session = getSite()['hostels'].accommodation_session |
---|
269 | students = catalog.searchResults(current_session=( |
---|
270 | accommodation_session,accommodation_session)) |
---|
271 | student_ids = [student.student_id for student in students] |
---|
272 | if not bed.owner in student_ids: |
---|
273 | raise Invalid(_( |
---|
274 | "Either student does not exist or student " |
---|
275 | "is not in accommodation session.")) |
---|
276 | catalog = getUtility(ICatalog, name='beds_catalog') |
---|
277 | beds = catalog.searchResults(owner=(bed.owner,bed.owner)) |
---|
278 | if len(beds): |
---|
279 | allocated_bed = [bed.bed_id for bed in beds][0] |
---|
280 | raise Invalid(_( |
---|
281 | "This student resides in bed ${a}.", |
---|
282 | mapping = {'a':allocated_bed})) |
---|
283 | |
---|
284 | def writeLogMessage(view, message): |
---|
285 | """Add an INFO message to hostels.log. |
---|
286 | """ |
---|