source: main/waeup.sirp/trunk/src/waeup/sirp/hostels/hostel.py @ 7167

Last change on this file since 7167 was 7070, checked in by Henrik Bettermann, 13 years ago

Add more tests and increase test coverage.

  • Property svn:keywords set to Id
File size: 8.4 KB
RevLine 
[6951]1## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
2## This program is free software; you can redistribute it and/or modify
3## it under the terms of the GNU General Public License as published by
4## the Free Software Foundation; either version 2 of the License, or
5## (at your option) any later version.
6##
7## This program is distributed in the hope that it will be useful,
8## but WITHOUT ANY WARRANTY; without even the implied warranty of
9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10## GNU General Public License for more details.
11##
12## You should have received a copy of the GNU General Public License
13## along with this program; if not, write to the Free Software
14## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15##
16"""
17These are the hostels.
18"""
19import grok
[7003]20from zope.event import notify
[6951]21from datetime import datetime
22from grok import index
23from waeup.sirp.utils.helpers import attrs_to_fields
[7068]24from waeup.sirp.hostels.vocabularies import NOT_OCCUPIED
25from waeup.sirp.hostels.interfaces import IHostel, IBed, IBedAllocateStudent
[7006]26from waeup.sirp.students.interfaces import IBedTicket
[6951]27
28class Hostel(grok.Container):
29    """This is a hostel.
30    """
31    grok.implements(IHostel)
32    grok.provides(IHostel)
33
[6952]34    def loggerInfo(self, ob_class, comment=None):
35        target = self.__name__
36        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
37
[6963]38    def addBed(self, bed):
[6970]39        """Add a bed.
[6963]40        """
41        if not IBed.providedBy(bed):
42            raise TypeError(
43                'Hostels contain only IBed instances')
[6970]44        self[bed.bed_id] = bed
[6963]45        return
46
[6970]47    def updateBeds(self):
48        """Fill hostel with beds or update beds.
49        """
50        added_counter = 0
51        modified_counter = 0
[6978]52        removed_counter = 0
[6988]53        modified_beds = u''
[6978]54
55        # Remove all empty beds. Occupied beds remain in hostel!
56        keys = list(self.keys()) # create list copy
57        for key in keys:
58            if self[key].owner == NOT_OCCUPIED:
59                del self[key]
60                self._p_changed = True
61                removed_counter += 1
[6998]62            else:
63                self[key].bed_number = 9999
64        remaining = len(keys) - removed_counter
[6978]65
[6970]66        blocks_for_female = getattr(self,'blocks_for_female',[])
67        blocks_for_male = getattr(self,'blocks_for_male',[])
68        beds_for_fresh = getattr(self,'beds_for_fresh',[])
69        beds_for_pre = getattr(self,'beds_for_pre',[])
70        beds_for_returning = getattr(self,'beds_for_returning',[])
71        beds_for_final = getattr(self,'beds_for_final',[])
[6971]72        beds_for_all = getattr(self,'beds_for_all',[])
[6976]73        beds_reserved = getattr(self,'beds_reserved',[])
[6970]74        all_blocks = blocks_for_female + blocks_for_male
75        all_beds = (beds_for_pre + beds_for_fresh +
[6971]76            beds_for_returning + beds_for_final + beds_for_all)
[6970]77        for block in all_blocks:
78            sex = 'male'
79            if block in blocks_for_female:
80                sex = 'female'
81            for floor in range(1,int(self.floors_per_block)+1):
82                for room in range(1,int(self.rooms_per_floor)+1):
83                    for bed in all_beds:
84                        room_nr = floor*100 + room
85                        bt = 'all'
[6976]86                        if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved:
[6970]87                            bt = "reserved"
88                        elif bed in beds_for_fresh:
89                            bt = 'fr'
90                        elif bed in beds_for_pre:
91                            bt = 'pr'
92                        elif bed in beds_for_final:
93                            bt = 'fi'
94                        elif bed in beds_for_returning:
95                            bt = 're'
[6973]96                        bt = u'%s_%s_%s' % (self.special_handling,sex,bt)
[6970]97                        uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed)
98                        if self.has_key(uid):
99                            bed = self[uid]
[6998]100                            # Renumber remaining beds
101                            bed.bed_number = len(self) + 1 - remaining
102                            remaining -= 1
[6970]103                            if bed.bed_type != bt:
104                                bed.bed_type = bt
105                                modified_counter += 1
[6988]106                                modified_beds += '%s ' % uid
[6970]107                        else:
108                            bed = Bed()
109                            bed.bed_id = uid
110                            bed.bed_type = bt
[6998]111                            bed.bed_number = len(self) + 1 - remaining
[6970]112                            bed.owner = NOT_OCCUPIED
113                            self.addBed(bed)
114                            added_counter +=1
[6988]115        return removed_counter, added_counter, modified_counter, modified_beds
[6970]116
[6951]117Hostel = attrs_to_fields(Hostel)
[6963]118
119class Bed(grok.Container):
120    """This is a bed.
121    """
[7068]122    grok.implements(IBed, IBedAllocateStudent)
[6963]123    grok.provides(IBed)
124
125    def getBedCoordinates(self):
[6974]126        """Determine the coordinates from the bed_id.
[6963]127        """
[6974]128        return self.bed_id.split('_')
[6963]129
[6996]130    def bookBed(self, student_id):
[6998]131        if self.owner == NOT_OCCUPIED:
132            self.owner = student_id
[7003]133            notify(grok.ObjectModifiedEvent(self))
[6998]134            return None
135        else:
136            return self.owner
[6996]137
[6974]138    def switchReservation(self):
[7003]139        """Reserves or unreserve bed respectively.
[6974]140        """
141        sh, sex, bt = self.bed_type.split('_')
142        hostel_id, block, room_nr, bed = self.getBedCoordinates()
[6975]143        hostel = self.__parent__
[6988]144        beds_for_fresh = getattr(hostel,'beds_for_fresh',[])
145        beds_for_pre = getattr(hostel,'beds_for_pre',[])
146        beds_for_returning = getattr(hostel,'beds_for_returning',[])
147        beds_for_final = getattr(hostel,'beds_for_final',[])
[6976]148        bed_string = u'%s_%s_%s' % (block, room_nr, bed)
[6974]149        if bt == 'reserved':
150            bt = 'all'
151            if bed in beds_for_fresh:
152                bt = 'fr'
153            elif bed in beds_for_pre:
154                bt = 'pr'
155            elif bed in beds_for_final:
156                bt = 'fi'
157            elif bed in beds_for_returning:
158                bt = 're'
159            bt = u'%s_%s_%s' % (sh, sex, bt)
[6976]160            hostel.beds_reserved.remove(bed_string)
[6988]161            message = u'unreserved'
[6974]162        else:
163            bt = u'%s_%s_reserved' % (sh, sex)
[6976]164            hostel.beds_reserved.append(bed_string)
[6991]165            # Comment of Martijn:
166            # If you have a non-Persistent subobject (like a list) and you change it,
167            # you need to manually flag the persistence machinery on the object that
168            # its subobject changed, with _p_changed. This is only necessary if some
169            # of the objects are not sublclasses of Persistent. For common built-in
170            # collections in Python such as list and dictionary there are replacements
171            # (PersistentList, PersistentMapping), and more advanced building blocks
172            # for indexes (BTrees), that don't have this issue.
[6988]173            hostel._p_changed = True
174            message = u'reserved'
[6974]175        self.bed_type = bt
[6988]176        return message
[6974]177
[7042]178    def releaseBed(self):
179        if self.owner == NOT_OCCUPIED:
[7070]180            return
[7042]181        else:
182            old_owner = self.owner
183            self.owner = NOT_OCCUPIED
184            notify(grok.ObjectModifiedEvent(self))
185            accommodation_session = grok.getSite()[
186                'configuration'].accommodation_session
187            try:
188                bedticket = grok.getSite()['students'][old_owner][
189                              'accommodation'][str(accommodation_session)]
190            except KeyError:
191                return '%s without bed ticket' % old_owner
192            bedticket.bed = None
193            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
194            bedticket.bed_coordinates = u'-- booking cancelled on %s --' % timestamp
195            return old_owner
196
[6963]197    def loggerInfo(self, ob_class, comment=None):
198        target = self.__name__
199        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
200
201Bed = attrs_to_fields(Bed)
[7006]202
203@grok.subscribe(IBedTicket, grok.IObjectRemovedEvent)
204def handle_bedticket_removed(bedticket, event):
205    """If a bed ticket is deleted, we make sure that also the owner attribute
206    of the bed is cleared (set to NOT_OCCUPIED).
207    """
[7068]208    if bedticket.bed != None:
209        bedticket.bed.owner = NOT_OCCUPIED
210        notify(grok.ObjectModifiedEvent(bedticket.bed))
Note: See TracBrowser for help on using the repository browser.