source: main/waeup.kofa/trunk/src/waeup/kofa/hostels/hostel.py @ 7858

Last change on this file since 7858 was 7811, checked in by uli, 13 years ago

Rename all non-locales stuff from sirp to kofa.

  • Property svn:keywords set to Id
File size: 8.8 KB
RevLine 
[7195]1## $Id: hostel.py 7811 2012-03-08 19:00:51Z uli $
2##
[6951]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"""
19These are the hostels.
20"""
21import grok
[7003]22from zope.event import notify
[6951]23from datetime import datetime
[7811]24from waeup.kofa.utils.helpers import attrs_to_fields
25from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED
26from waeup.kofa.hostels.interfaces import IHostel, IBed, IBedAllocateStudent
27from waeup.kofa.students.interfaces import IBedTicket
28from waeup.kofa.interfaces import MessageFactory as _
[6951]29
30class Hostel(grok.Container):
31    """This is a hostel.
32    """
33    grok.implements(IHostel)
34    grok.provides(IHostel)
35
[6952]36    def loggerInfo(self, ob_class, comment=None):
37        target = self.__name__
38        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
39
[6963]40    def addBed(self, bed):
[6970]41        """Add a bed.
[6963]42        """
43        if not IBed.providedBy(bed):
44            raise TypeError(
45                'Hostels contain only IBed instances')
[6970]46        self[bed.bed_id] = bed
[6963]47        return
48
[6970]49    def updateBeds(self):
50        """Fill hostel with beds or update beds.
51        """
52        added_counter = 0
53        modified_counter = 0
[6978]54        removed_counter = 0
[6988]55        modified_beds = u''
[6978]56
57        # Remove all empty beds. Occupied beds remain in hostel!
58        keys = list(self.keys()) # create list copy
59        for key in keys:
60            if self[key].owner == NOT_OCCUPIED:
61                del self[key]
62                self._p_changed = True
63                removed_counter += 1
[6998]64            else:
65                self[key].bed_number = 9999
66        remaining = len(keys) - removed_counter
[6978]67
[6970]68        blocks_for_female = getattr(self,'blocks_for_female',[])
69        blocks_for_male = getattr(self,'blocks_for_male',[])
70        beds_for_fresh = getattr(self,'beds_for_fresh',[])
71        beds_for_pre = getattr(self,'beds_for_pre',[])
72        beds_for_returning = getattr(self,'beds_for_returning',[])
73        beds_for_final = getattr(self,'beds_for_final',[])
[6971]74        beds_for_all = getattr(self,'beds_for_all',[])
[6976]75        beds_reserved = getattr(self,'beds_reserved',[])
[6970]76        all_blocks = blocks_for_female + blocks_for_male
77        all_beds = (beds_for_pre + beds_for_fresh +
[6971]78            beds_for_returning + beds_for_final + beds_for_all)
[6970]79        for block in all_blocks:
80            sex = 'male'
81            if block in blocks_for_female:
82                sex = 'female'
83            for floor in range(1,int(self.floors_per_block)+1):
84                for room in range(1,int(self.rooms_per_floor)+1):
85                    for bed in all_beds:
86                        room_nr = floor*100 + room
87                        bt = 'all'
[6976]88                        if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved:
[6970]89                            bt = "reserved"
90                        elif bed in beds_for_fresh:
91                            bt = 'fr'
92                        elif bed in beds_for_pre:
93                            bt = 'pr'
94                        elif bed in beds_for_final:
95                            bt = 'fi'
96                        elif bed in beds_for_returning:
97                            bt = 're'
[6973]98                        bt = u'%s_%s_%s' % (self.special_handling,sex,bt)
[6970]99                        uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed)
100                        if self.has_key(uid):
101                            bed = self[uid]
[6998]102                            # Renumber remaining beds
103                            bed.bed_number = len(self) + 1 - remaining
104                            remaining -= 1
[6970]105                            if bed.bed_type != bt:
106                                bed.bed_type = bt
107                                modified_counter += 1
[6988]108                                modified_beds += '%s ' % uid
[6970]109                        else:
110                            bed = Bed()
111                            bed.bed_id = uid
112                            bed.bed_type = bt
[6998]113                            bed.bed_number = len(self) + 1 - remaining
[6970]114                            bed.owner = NOT_OCCUPIED
115                            self.addBed(bed)
116                            added_counter +=1
[6988]117        return removed_counter, added_counter, modified_counter, modified_beds
[6970]118
[6951]119Hostel = attrs_to_fields(Hostel)
[6963]120
121class Bed(grok.Container):
122    """This is a bed.
123    """
[7068]124    grok.implements(IBed, IBedAllocateStudent)
[6963]125    grok.provides(IBed)
126
127    def getBedCoordinates(self):
[6974]128        """Determine the coordinates from the bed_id.
[6963]129        """
[6974]130        return self.bed_id.split('_')
[6963]131
[6996]132    def bookBed(self, student_id):
[6998]133        if self.owner == NOT_OCCUPIED:
134            self.owner = student_id
[7003]135            notify(grok.ObjectModifiedEvent(self))
[6998]136            return None
137        else:
138            return self.owner
[6996]139
[6974]140    def switchReservation(self):
[7003]141        """Reserves or unreserve bed respectively.
[6974]142        """
143        sh, sex, bt = self.bed_type.split('_')
144        hostel_id, block, room_nr, bed = self.getBedCoordinates()
[6975]145        hostel = self.__parent__
[6988]146        beds_for_fresh = getattr(hostel,'beds_for_fresh',[])
147        beds_for_pre = getattr(hostel,'beds_for_pre',[])
148        beds_for_returning = getattr(hostel,'beds_for_returning',[])
149        beds_for_final = getattr(hostel,'beds_for_final',[])
[6976]150        bed_string = u'%s_%s_%s' % (block, room_nr, bed)
[6974]151        if bt == 'reserved':
152            bt = 'all'
153            if bed in beds_for_fresh:
154                bt = 'fr'
155            elif bed in beds_for_pre:
156                bt = 'pr'
157            elif bed in beds_for_final:
158                bt = 'fi'
159            elif bed in beds_for_returning:
160                bt = 're'
161            bt = u'%s_%s_%s' % (sh, sex, bt)
[7718]162
[6991]163            # Comment of Martijn:
164            # If you have a non-Persistent subobject (like a list) and you change it,
165            # you need to manually flag the persistence machinery on the object that
166            # its subobject changed, with _p_changed. This is only necessary if some
167            # of the objects are not sublclasses of Persistent. For common built-in
168            # collections in Python such as list and dictionary there are replacements
169            # (PersistentList, PersistentMapping), and more advanced building blocks
170            # for indexes (BTrees), that don't have this issue.
[7718]171            #hostel._p_changed = True
172
173            # Henrik: Unfortunately, this doesn't work in our case.
174            # After restarting the portal,
175            # added or removed list items are gone. The only way to ensure
176            # persistance is to reassign the list attribute.
177            br = hostel.beds_reserved
178            br.remove(bed_string)
179            hostel.beds_reserved = br
180            message = _(u'unreserved')
181        else:
182            bt = u'%s_%s_reserved' % (sh, sex)
183            # See comment above
184            br = hostel.beds_reserved
185            br.append(bed_string)
186            hostel.beds_reserved = br
187            message = _(u'reserved')
[6974]188        self.bed_type = bt
[6988]189        return message
[6974]190
[7042]191    def releaseBed(self):
192        if self.owner == NOT_OCCUPIED:
[7070]193            return
[7042]194        else:
195            old_owner = self.owner
196            self.owner = NOT_OCCUPIED
197            notify(grok.ObjectModifiedEvent(self))
198            accommodation_session = grok.getSite()[
199                'configuration'].accommodation_session
200            try:
201                bedticket = grok.getSite()['students'][old_owner][
202                              'accommodation'][str(accommodation_session)]
203            except KeyError:
204                return '%s without bed ticket' % old_owner
205            bedticket.bed = None
206            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
207            bedticket.bed_coordinates = u'-- booking cancelled on %s --' % timestamp
208            return old_owner
209
[6963]210    def loggerInfo(self, ob_class, comment=None):
211        target = self.__name__
212        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
213
214Bed = attrs_to_fields(Bed)
[7006]215
216@grok.subscribe(IBedTicket, grok.IObjectRemovedEvent)
217def handle_bedticket_removed(bedticket, event):
218    """If a bed ticket is deleted, we make sure that also the owner attribute
219    of the bed is cleared (set to NOT_OCCUPIED).
220    """
[7068]221    if bedticket.bed != None:
222        bedticket.bed.owner = NOT_OCCUPIED
223        notify(grok.ObjectModifiedEvent(bedticket.bed))
Note: See TracBrowser for help on using the repository browser.