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

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

Implement bed booking algorithm (work in progress!).

  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
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
20from datetime import datetime
21from grok import index
22from waeup.sirp.utils.helpers import attrs_to_fields
23from waeup.sirp.hostels.interfaces import IHostel, IBed
24
25NOT_OCCUPIED = u'not occupied'
26
27class Hostel(grok.Container):
28    """This is a hostel.
29    """
30    grok.implements(IHostel)
31    grok.provides(IHostel)
32
33    def loggerInfo(self, ob_class, comment=None):
34        target = self.__name__
35        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
36
37    def addBed(self, bed):
38        """Add a bed.
39        """
40        if not IBed.providedBy(bed):
41            raise TypeError(
42                'Hostels contain only IBed instances')
43        self[bed.bed_id] = bed
44        return
45
46    def updateBeds(self):
47        """Fill hostel with beds or update beds.
48        """
49        added_counter = 0
50        modified_counter = 0
51        removed_counter = 0
52        modified_beds = u''
53
54        # Remove all empty beds. Occupied beds remain in hostel!
55        keys = list(self.keys()) # create list copy
56        for key in keys:
57            if self[key].owner == NOT_OCCUPIED:
58                del self[key]
59                self._p_changed = True
60                removed_counter += 1
61
62        blocks_for_female = getattr(self,'blocks_for_female',[])
63        blocks_for_male = getattr(self,'blocks_for_male',[])
64        beds_for_fresh = getattr(self,'beds_for_fresh',[])
65        beds_for_pre = getattr(self,'beds_for_pre',[])
66        beds_for_returning = getattr(self,'beds_for_returning',[])
67        beds_for_final = getattr(self,'beds_for_final',[])
68        beds_for_all = getattr(self,'beds_for_all',[])
69        beds_reserved = getattr(self,'beds_reserved',[])
70        all_blocks = blocks_for_female + blocks_for_male
71        all_beds = (beds_for_pre + beds_for_fresh +
72            beds_for_returning + beds_for_final + beds_for_all)
73        for block in all_blocks:
74            sex = 'male'
75            if block in blocks_for_female:
76                sex = 'female'
77            for floor in range(1,int(self.floors_per_block)+1):
78                for room in range(1,int(self.rooms_per_floor)+1):
79                    for bed in all_beds:
80                        room_nr = floor*100 + room
81                        bt = 'all'
82                        if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved:
83                            bt = "reserved"
84                        elif bed in beds_for_fresh:
85                            bt = 'fr'
86                        elif bed in beds_for_pre:
87                            bt = 'pr'
88                        elif bed in beds_for_final:
89                            bt = 'fi'
90                        elif bed in beds_for_returning:
91                            bt = 're'
92                        bt = u'%s_%s_%s' % (self.special_handling,sex,bt)
93                        uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed)
94                        if self.has_key(uid):
95                            bed = self[uid]
96                            if bed.bed_type != bt:
97                                bed.bed_type = bt
98                                modified_counter += 1
99                                modified_beds += '%s ' % uid
100                        else:
101                            bed = Bed()
102                            bed.bed_id = uid
103                            bed.bed_type = bt
104                            bed.bed_number = len(self) + 1
105                            bed.owner = NOT_OCCUPIED
106                            self.addBed(bed)
107                            added_counter +=1
108        return removed_counter, added_counter, modified_counter, modified_beds
109
110Hostel = attrs_to_fields(Hostel)
111
112class Bed(grok.Container):
113    """This is a bed.
114    """
115    grok.implements(IBed)
116    grok.provides(IBed)
117
118    def getBedCoordinates(self):
119        """Determine the coordinates from the bed_id.
120        """
121        return self.bed_id.split('_')
122
123    def bookBed(self, student_id):
124        self.owner = student_id
125        return
126
127    def switchReservation(self):
128        """Reserves a bed or unreserve bed respectively.
129        """
130        sh, sex, bt = self.bed_type.split('_')
131        hostel_id, block, room_nr, bed = self.getBedCoordinates()
132        hostel = self.__parent__
133        beds_for_fresh = getattr(hostel,'beds_for_fresh',[])
134        beds_for_pre = getattr(hostel,'beds_for_pre',[])
135        beds_for_returning = getattr(hostel,'beds_for_returning',[])
136        beds_for_final = getattr(hostel,'beds_for_final',[])
137        bed_string = u'%s_%s_%s' % (block, room_nr, bed)
138        if bt == 'reserved':
139            bt = 'all'
140            if bed in beds_for_fresh:
141                bt = 'fr'
142            elif bed in beds_for_pre:
143                bt = 'pr'
144            elif bed in beds_for_final:
145                bt = 'fi'
146            elif bed in beds_for_returning:
147                bt = 're'
148            bt = u'%s_%s_%s' % (sh, sex, bt)
149            hostel.beds_reserved.remove(bed_string)
150            message = u'unreserved'
151        else:
152            bt = u'%s_%s_reserved' % (sh, sex)
153            hostel.beds_reserved.append(bed_string)
154            # Comment of Martijn:
155            # If you have a non-Persistent subobject (like a list) and you change it,
156            # you need to manually flag the persistence machinery on the object that
157            # its subobject changed, with _p_changed. This is only necessary if some
158            # of the objects are not sublclasses of Persistent. For common built-in
159            # collections in Python such as list and dictionary there are replacements
160            # (PersistentList, PersistentMapping), and more advanced building blocks
161            # for indexes (BTrees), that don't have this issue.
162            hostel._p_changed = True
163            message = u'reserved'
164        self.bed_type = bt
165        return message
166
167    def loggerInfo(self, ob_class, comment=None):
168        target = self.__name__
169        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
170
171Bed = attrs_to_fields(Bed)
Note: See TracBrowser for help on using the repository browser.