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

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

Add some methods to release expired bed allocations. View
components have to be added in custom packages.

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