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

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

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 8.4 KB
RevLine 
[7195]1## $Id: hostel.py 7195 2011-11-25 07:34:07Z henrik $
2##
[6951]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
[7003]22from zope.event import notify
[6951]23from datetime import datetime
24from grok import index
25from waeup.sirp.utils.helpers import attrs_to_fields
[7068]26from waeup.sirp.hostels.vocabularies import NOT_OCCUPIED
27from waeup.sirp.hostels.interfaces import IHostel, IBed, IBedAllocateStudent
[7006]28from waeup.sirp.students.interfaces import IBedTicket
[6951]29
30class Hostel(grok.Container):
31    """This is a hostel.
32    """
33    grok.implements(IHostel)
34    grok.provides(IHostel)
35
[6952]36    def loggerInfo(self, ob_class, comment=None):
37        target = self.__name__
38        return grok.getSite()['hostels'].logger_info(ob_class,target,comment)
39
[6963]40    def addBed(self, bed):
[6970]41        """Add a bed.
[6963]42        """
43        if not IBed.providedBy(bed):
44            raise TypeError(
45                'Hostels contain only IBed instances')
[6970]46        self[bed.bed_id] = bed
[6963]47        return
48
[6970]49    def updateBeds(self):
50        """Fill hostel with beds or update beds.
51        """
52        added_counter = 0
53        modified_counter = 0
[6978]54        removed_counter = 0
[6988]55        modified_beds = u''
[6978]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
[6998]64            else:
65                self[key].bed_number = 9999
66        remaining = len(keys) - removed_counter
[6978]67
[6970]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',[])
[6971]74        beds_for_all = getattr(self,'beds_for_all',[])
[6976]75        beds_reserved = getattr(self,'beds_reserved',[])
[6970]76        all_blocks = blocks_for_female + blocks_for_male
77        all_beds = (beds_for_pre + beds_for_fresh +
[6971]78            beds_for_returning + beds_for_final + beds_for_all)
[6970]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'
[6976]88                        if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved:
[6970]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'
[6973]98                        bt = u'%s_%s_%s' % (self.special_handling,sex,bt)
[6970]99                        uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed)
100                        if self.has_key(uid):
101                            bed = self[uid]
[6998]102                            # Renumber remaining beds
103                            bed.bed_number = len(self) + 1 - remaining
104                            remaining -= 1
[6970]105                            if bed.bed_type != bt:
106                                bed.bed_type = bt
107                                modified_counter += 1
[6988]108                                modified_beds += '%s ' % uid
[6970]109                        else:
110                            bed = Bed()
111                            bed.bed_id = uid
112                            bed.bed_type = bt
[6998]113                            bed.bed_number = len(self) + 1 - remaining
[6970]114                            bed.owner = NOT_OCCUPIED
115                            self.addBed(bed)
116                            added_counter +=1
[6988]117        return removed_counter, added_counter, modified_counter, modified_beds
[6970]118
[6951]119Hostel = attrs_to_fields(Hostel)
[6963]120
121class Bed(grok.Container):
122    """This is a bed.
123    """
[7068]124    grok.implements(IBed, IBedAllocateStudent)
[6963]125    grok.provides(IBed)
126
127    def getBedCoordinates(self):
[6974]128        """Determine the coordinates from the bed_id.
[6963]129        """
[6974]130        return self.bed_id.split('_')
[6963]131
[6996]132    def bookBed(self, student_id):
[6998]133        if self.owner == NOT_OCCUPIED:
134            self.owner = student_id
[7003]135            notify(grok.ObjectModifiedEvent(self))
[6998]136            return None
137        else:
138            return self.owner
[6996]139
[6974]140    def switchReservation(self):
[7003]141        """Reserves or unreserve bed respectively.
[6974]142        """
143        sh, sex, bt = self.bed_type.split('_')
144        hostel_id, block, room_nr, bed = self.getBedCoordinates()
[6975]145        hostel = self.__parent__
[6988]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',[])
[6976]150        bed_string = u'%s_%s_%s' % (block, room_nr, bed)
[6974]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)
[6976]162            hostel.beds_reserved.remove(bed_string)
[6988]163            message = u'unreserved'
[6974]164        else:
165            bt = u'%s_%s_reserved' % (sh, sex)
[6976]166            hostel.beds_reserved.append(bed_string)
[6991]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.
[6988]175            hostel._p_changed = True
176            message = u'reserved'
[6974]177        self.bed_type = bt
[6988]178        return message
[6974]179
[7042]180    def releaseBed(self):
181        if self.owner == NOT_OCCUPIED:
[7070]182            return
[7042]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
[6963]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)
[7006]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    """
[7068]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.