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

Last change on this file since 9414 was 9414, checked in by Henrik Bettermann, 12 years ago

Reorganize allocation of students to beds. We can't use the StudentSource? in live systems. The select box would be filled with ten thousands of students.

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