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

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

Renumber and sort remaining beds correctly.

  • Property svn:keywords set to Id
File size: 7.1 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
20from datetime import datetime
21from grok import index
22from waeup.sirp.utils.helpers import attrs_to_fields
[6963]23from waeup.sirp.hostels.interfaces import IHostel, IBed
[6951]24
[6970]25NOT_OCCUPIED = u'not occupied'
26
[6951]27class Hostel(grok.Container):
28    """This is a hostel.
29    """
30    grok.implements(IHostel)
31    grok.provides(IHostel)
32
[6952]33    def loggerInfo(self, ob_class, comment=None):
34        target = self.__name__
35        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
36
[6963]37    def addBed(self, bed):
[6970]38        """Add a bed.
[6963]39        """
40        if not IBed.providedBy(bed):
41            raise TypeError(
42                'Hostels contain only IBed instances')
[6970]43        self[bed.bed_id] = bed
[6963]44        return
45
[6970]46    def updateBeds(self):
47        """Fill hostel with beds or update beds.
48        """
49        added_counter = 0
50        modified_counter = 0
[6978]51        removed_counter = 0
[6988]52        modified_beds = u''
[6978]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
[6998]61            else:
62                self[key].bed_number = 9999
63        remaining = len(keys) - removed_counter
[6978]64
[6970]65        blocks_for_female = getattr(self,'blocks_for_female',[])
66        blocks_for_male = getattr(self,'blocks_for_male',[])
67        beds_for_fresh = getattr(self,'beds_for_fresh',[])
68        beds_for_pre = getattr(self,'beds_for_pre',[])
69        beds_for_returning = getattr(self,'beds_for_returning',[])
70        beds_for_final = getattr(self,'beds_for_final',[])
[6971]71        beds_for_all = getattr(self,'beds_for_all',[])
[6976]72        beds_reserved = getattr(self,'beds_reserved',[])
[6970]73        all_blocks = blocks_for_female + blocks_for_male
74        all_beds = (beds_for_pre + beds_for_fresh +
[6971]75            beds_for_returning + beds_for_final + beds_for_all)
[6970]76        for block in all_blocks:
77            sex = 'male'
78            if block in blocks_for_female:
79                sex = 'female'
80            for floor in range(1,int(self.floors_per_block)+1):
81                for room in range(1,int(self.rooms_per_floor)+1):
82                    for bed in all_beds:
83                        room_nr = floor*100 + room
84                        bt = 'all'
[6976]85                        if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved:
[6970]86                            bt = "reserved"
87                        elif bed in beds_for_fresh:
88                            bt = 'fr'
89                        elif bed in beds_for_pre:
90                            bt = 'pr'
91                        elif bed in beds_for_final:
92                            bt = 'fi'
93                        elif bed in beds_for_returning:
94                            bt = 're'
[6973]95                        bt = u'%s_%s_%s' % (self.special_handling,sex,bt)
[6970]96                        uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed)
97                        if self.has_key(uid):
98                            bed = self[uid]
[6998]99                            # Renumber remaining beds
100                            bed.bed_number = len(self) + 1 - remaining
101                            remaining -= 1
[6970]102                            if bed.bed_type != bt:
103                                bed.bed_type = bt
104                                modified_counter += 1
[6988]105                                modified_beds += '%s ' % uid
[6970]106                        else:
107                            bed = Bed()
108                            bed.bed_id = uid
109                            bed.bed_type = bt
[6998]110                            bed.bed_number = len(self) + 1 - remaining
[6970]111                            bed.owner = NOT_OCCUPIED
112                            self.addBed(bed)
113                            added_counter +=1
[6988]114        return removed_counter, added_counter, modified_counter, modified_beds
[6970]115
[6951]116Hostel = attrs_to_fields(Hostel)
[6963]117
118class Bed(grok.Container):
119    """This is a bed.
120    """
121    grok.implements(IBed)
122    grok.provides(IBed)
123
124    def getBedCoordinates(self):
[6974]125        """Determine the coordinates from the bed_id.
[6963]126        """
[6974]127        return self.bed_id.split('_')
[6963]128
[6996]129    def bookBed(self, student_id):
[6998]130        if self.owner == NOT_OCCUPIED:
131            self.owner = student_id
132            return None
133        else:
134            return self.owner
[6996]135
[6974]136    def switchReservation(self):
[6988]137        """Reserves a bed or unreserve bed respectively.
[6974]138        """
139        sh, sex, bt = self.bed_type.split('_')
140        hostel_id, block, room_nr, bed = self.getBedCoordinates()
[6975]141        hostel = self.__parent__
[6988]142        beds_for_fresh = getattr(hostel,'beds_for_fresh',[])
143        beds_for_pre = getattr(hostel,'beds_for_pre',[])
144        beds_for_returning = getattr(hostel,'beds_for_returning',[])
145        beds_for_final = getattr(hostel,'beds_for_final',[])
[6976]146        bed_string = u'%s_%s_%s' % (block, room_nr, bed)
[6974]147        if bt == 'reserved':
148            bt = 'all'
149            if bed in beds_for_fresh:
150                bt = 'fr'
151            elif bed in beds_for_pre:
152                bt = 'pr'
153            elif bed in beds_for_final:
154                bt = 'fi'
155            elif bed in beds_for_returning:
156                bt = 're'
157            bt = u'%s_%s_%s' % (sh, sex, bt)
[6976]158            hostel.beds_reserved.remove(bed_string)
[6988]159            message = u'unreserved'
[6974]160        else:
161            bt = u'%s_%s_reserved' % (sh, sex)
[6976]162            hostel.beds_reserved.append(bed_string)
[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.
[6988]171            hostel._p_changed = True
172            message = u'reserved'
[6974]173        self.bed_type = bt
[6988]174        return message
[6974]175
[6963]176    def loggerInfo(self, ob_class, comment=None):
177        target = self.__name__
178        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
179
180Bed = attrs_to_fields(Bed)
Note: See TracBrowser for help on using the repository browser.