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

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

Add missing handler.

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