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

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

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1## $Id: hostel.py 7195 2011-11-25 07:34:07Z 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##
18"""
19These are the hostels.
20"""
21import grok
22from zope.event import notify
23from datetime import datetime
24from grok import index
25from waeup.sirp.utils.helpers import attrs_to_fields
26from waeup.sirp.hostels.vocabularies import NOT_OCCUPIED
27from waeup.sirp.hostels.interfaces import IHostel, IBed, IBedAllocateStudent
28from waeup.sirp.students.interfaces import IBedTicket
29
30class Hostel(grok.Container):
31    """This is a hostel.
32    """
33    grok.implements(IHostel)
34    grok.provides(IHostel)
35
36    def loggerInfo(self, ob_class, comment=None):
37        target = self.__name__
38        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
39
40    def addBed(self, bed):
41        """Add a bed.
42        """
43        if not IBed.providedBy(bed):
44            raise TypeError(
45                'Hostels contain only IBed instances')
46        self[bed.bed_id] = bed
47        return
48
49    def updateBeds(self):
50        """Fill hostel with beds or update beds.
51        """
52        added_counter = 0
53        modified_counter = 0
54        removed_counter = 0
55        modified_beds = u''
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
64            else:
65                self[key].bed_number = 9999
66        remaining = len(keys) - removed_counter
67
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',[])
74        beds_for_all = getattr(self,'beds_for_all',[])
75        beds_reserved = getattr(self,'beds_reserved',[])
76        all_blocks = blocks_for_female + blocks_for_male
77        all_beds = (beds_for_pre + beds_for_fresh +
78            beds_for_returning + beds_for_final + beds_for_all)
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'
88                        if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved:
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'
98                        bt = u'%s_%s_%s' % (self.special_handling,sex,bt)
99                        uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed)
100                        if self.has_key(uid):
101                            bed = self[uid]
102                            # Renumber remaining beds
103                            bed.bed_number = len(self) + 1 - remaining
104                            remaining -= 1
105                            if bed.bed_type != bt:
106                                bed.bed_type = bt
107                                modified_counter += 1
108                                modified_beds += '%s ' % uid
109                        else:
110                            bed = Bed()
111                            bed.bed_id = uid
112                            bed.bed_type = bt
113                            bed.bed_number = len(self) + 1 - remaining
114                            bed.owner = NOT_OCCUPIED
115                            self.addBed(bed)
116                            added_counter +=1
117        return removed_counter, added_counter, modified_counter, modified_beds
118
119Hostel = attrs_to_fields(Hostel)
120
121class Bed(grok.Container):
122    """This is a bed.
123    """
124    grok.implements(IBed, IBedAllocateStudent)
125    grok.provides(IBed)
126
127    def getBedCoordinates(self):
128        """Determine the coordinates from the bed_id.
129        """
130        return self.bed_id.split('_')
131
132    def bookBed(self, student_id):
133        if self.owner == NOT_OCCUPIED:
134            self.owner = student_id
135            notify(grok.ObjectModifiedEvent(self))
136            return None
137        else:
138            return self.owner
139
140    def switchReservation(self):
141        """Reserves or unreserve bed respectively.
142        """
143        sh, sex, bt = self.bed_type.split('_')
144        hostel_id, block, room_nr, bed = self.getBedCoordinates()
145        hostel = self.__parent__
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',[])
150        bed_string = u'%s_%s_%s' % (block, room_nr, bed)
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)
162            hostel.beds_reserved.remove(bed_string)
163            message = u'unreserved'
164        else:
165            bt = u'%s_%s_reserved' % (sh, sex)
166            hostel.beds_reserved.append(bed_string)
167            # Comment of Martijn:
168            # If you have a non-Persistent subobject (like a list) and you change it,
169            # you need to manually flag the persistence machinery on the object that
170            # its subobject changed, with _p_changed. This is only necessary if some
171            # of the objects are not sublclasses of Persistent. For common built-in
172            # collections in Python such as list and dictionary there are replacements
173            # (PersistentList, PersistentMapping), and more advanced building blocks
174            # for indexes (BTrees), that don't have this issue.
175            hostel._p_changed = True
176            message = u'reserved'
177        self.bed_type = bt
178        return message
179
180    def releaseBed(self):
181        if self.owner == NOT_OCCUPIED:
182            return
183        else:
184            old_owner = self.owner
185            self.owner = NOT_OCCUPIED
186            notify(grok.ObjectModifiedEvent(self))
187            accommodation_session = grok.getSite()[
188                'configuration'].accommodation_session
189            try:
190                bedticket = grok.getSite()['students'][old_owner][
191                              'accommodation'][str(accommodation_session)]
192            except KeyError:
193                return '%s without bed ticket' % old_owner
194            bedticket.bed = None
195            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
196            bedticket.bed_coordinates = u'-- booking cancelled on %s --' % timestamp
197            return old_owner
198
199    def loggerInfo(self, ob_class, comment=None):
200        target = self.__name__
201        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
202
203Bed = attrs_to_fields(Bed)
204
205@grok.subscribe(IBedTicket, grok.IObjectRemovedEvent)
206def handle_bedticket_removed(bedticket, event):
207    """If a bed ticket is deleted, we make sure that also the owner attribute
208    of the bed is cleared (set to NOT_OCCUPIED).
209    """
210    if bedticket.bed != None:
211        bedticket.bed.owner = NOT_OCCUPIED
212        notify(grok.ObjectModifiedEvent(bedticket.bed))
Note: See TracBrowser for help on using the repository browser.