source: main/waeup.kofa/trunk/src/waeup/kofa/hostels/interfaces.py @ 13443

Last change on this file since 13443 was 13442, checked in by Henrik Bettermann, 9 years ago

Only hostel sort ids below 100 are allowed.

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