[7195] | 1 | ## $Id: hostel.py 9211 2012-09-21 08:19:35Z uli $ |
---|
| 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 | """ |
---|
| 19 | These are the hostels. |
---|
| 20 | """ |
---|
| 21 | import grok |
---|
[7003] | 22 | from zope.event import notify |
---|
[8183] | 23 | from zope.component import getUtility |
---|
[6951] | 24 | from datetime import datetime |
---|
[7811] | 25 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
| 26 | from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED |
---|
| 27 | from waeup.kofa.hostels.interfaces import IHostel, IBed, IBedAllocateStudent |
---|
| 28 | from waeup.kofa.students.interfaces import IBedTicket |
---|
[8183] | 29 | from waeup.kofa.interfaces import IKofaUtils |
---|
[7811] | 30 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[8186] | 31 | from waeup.kofa.utils.helpers import now |
---|
[6951] | 32 | |
---|
| 33 | class Hostel(grok.Container): |
---|
| 34 | """This is a hostel. |
---|
| 35 | """ |
---|
| 36 | grok.implements(IHostel) |
---|
| 37 | grok.provides(IHostel) |
---|
| 38 | |
---|
[6952] | 39 | def loggerInfo(self, ob_class, comment=None): |
---|
| 40 | target = self.__name__ |
---|
| 41 | return grok.getSite()['hostels'].logger_info(ob_class,target,comment) |
---|
| 42 | |
---|
[6963] | 43 | def addBed(self, bed): |
---|
[6970] | 44 | """Add a bed. |
---|
[6963] | 45 | """ |
---|
| 46 | if not IBed.providedBy(bed): |
---|
| 47 | raise TypeError( |
---|
| 48 | 'Hostels contain only IBed instances') |
---|
[6970] | 49 | self[bed.bed_id] = bed |
---|
[6963] | 50 | return |
---|
| 51 | |
---|
[6970] | 52 | def updateBeds(self): |
---|
| 53 | """Fill hostel with beds or update beds. |
---|
| 54 | """ |
---|
| 55 | added_counter = 0 |
---|
| 56 | modified_counter = 0 |
---|
[6978] | 57 | removed_counter = 0 |
---|
[6988] | 58 | modified_beds = u'' |
---|
[6978] | 59 | |
---|
| 60 | # Remove all empty beds. Occupied beds remain in hostel! |
---|
| 61 | keys = list(self.keys()) # create list copy |
---|
| 62 | for key in keys: |
---|
| 63 | if self[key].owner == NOT_OCCUPIED: |
---|
| 64 | del self[key] |
---|
| 65 | self._p_changed = True |
---|
| 66 | removed_counter += 1 |
---|
[6998] | 67 | else: |
---|
| 68 | self[key].bed_number = 9999 |
---|
| 69 | remaining = len(keys) - removed_counter |
---|
[6978] | 70 | |
---|
[6970] | 71 | blocks_for_female = getattr(self,'blocks_for_female',[]) |
---|
| 72 | blocks_for_male = getattr(self,'blocks_for_male',[]) |
---|
| 73 | beds_for_fresh = getattr(self,'beds_for_fresh',[]) |
---|
| 74 | beds_for_pre = getattr(self,'beds_for_pre',[]) |
---|
| 75 | beds_for_returning = getattr(self,'beds_for_returning',[]) |
---|
| 76 | beds_for_final = getattr(self,'beds_for_final',[]) |
---|
[6971] | 77 | beds_for_all = getattr(self,'beds_for_all',[]) |
---|
[6976] | 78 | beds_reserved = getattr(self,'beds_reserved',[]) |
---|
[6970] | 79 | all_blocks = blocks_for_female + blocks_for_male |
---|
| 80 | all_beds = (beds_for_pre + beds_for_fresh + |
---|
[6971] | 81 | beds_for_returning + beds_for_final + beds_for_all) |
---|
[6970] | 82 | for block in all_blocks: |
---|
| 83 | sex = 'male' |
---|
| 84 | if block in blocks_for_female: |
---|
| 85 | sex = 'female' |
---|
| 86 | for floor in range(1,int(self.floors_per_block)+1): |
---|
| 87 | for room in range(1,int(self.rooms_per_floor)+1): |
---|
| 88 | for bed in all_beds: |
---|
| 89 | room_nr = floor*100 + room |
---|
| 90 | bt = 'all' |
---|
[6976] | 91 | if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved: |
---|
[6970] | 92 | bt = "reserved" |
---|
| 93 | elif bed in beds_for_fresh: |
---|
| 94 | bt = 'fr' |
---|
| 95 | elif bed in beds_for_pre: |
---|
| 96 | bt = 'pr' |
---|
| 97 | elif bed in beds_for_final: |
---|
| 98 | bt = 'fi' |
---|
| 99 | elif bed in beds_for_returning: |
---|
| 100 | bt = 're' |
---|
[6973] | 101 | bt = u'%s_%s_%s' % (self.special_handling,sex,bt) |
---|
[6970] | 102 | uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed) |
---|
| 103 | if self.has_key(uid): |
---|
| 104 | bed = self[uid] |
---|
[6998] | 105 | # Renumber remaining beds |
---|
| 106 | bed.bed_number = len(self) + 1 - remaining |
---|
| 107 | remaining -= 1 |
---|
[6970] | 108 | if bed.bed_type != bt: |
---|
| 109 | bed.bed_type = bt |
---|
| 110 | modified_counter += 1 |
---|
[6988] | 111 | modified_beds += '%s ' % uid |
---|
[6970] | 112 | else: |
---|
| 113 | bed = Bed() |
---|
| 114 | bed.bed_id = uid |
---|
| 115 | bed.bed_type = bt |
---|
[6998] | 116 | bed.bed_number = len(self) + 1 - remaining |
---|
[6970] | 117 | bed.owner = NOT_OCCUPIED |
---|
| 118 | self.addBed(bed) |
---|
| 119 | added_counter +=1 |
---|
[6988] | 120 | return removed_counter, added_counter, modified_counter, modified_beds |
---|
[6970] | 121 | |
---|
[6951] | 122 | Hostel = attrs_to_fields(Hostel) |
---|
[6963] | 123 | |
---|
| 124 | class Bed(grok.Container): |
---|
| 125 | """This is a bed. |
---|
| 126 | """ |
---|
[7068] | 127 | grok.implements(IBed, IBedAllocateStudent) |
---|
[6963] | 128 | grok.provides(IBed) |
---|
| 129 | |
---|
[9211] | 130 | def getBedCoordinates(self): |
---|
[6974] | 131 | """Determine the coordinates from the bed_id. |
---|
[6963] | 132 | """ |
---|
[6974] | 133 | return self.bed_id.split('_') |
---|
[6963] | 134 | |
---|
[6996] | 135 | def bookBed(self, student_id): |
---|
[6998] | 136 | if self.owner == NOT_OCCUPIED: |
---|
| 137 | self.owner = student_id |
---|
[7003] | 138 | notify(grok.ObjectModifiedEvent(self)) |
---|
[6998] | 139 | return None |
---|
| 140 | else: |
---|
| 141 | return self.owner |
---|
[6996] | 142 | |
---|
[6974] | 143 | def switchReservation(self): |
---|
[7003] | 144 | """Reserves or unreserve bed respectively. |
---|
[6974] | 145 | """ |
---|
| 146 | sh, sex, bt = self.bed_type.split('_') |
---|
[9211] | 147 | hostel_id, block, room_nr, bed = self.getBedCoordinates() |
---|
[6975] | 148 | hostel = self.__parent__ |
---|
[6988] | 149 | beds_for_fresh = getattr(hostel,'beds_for_fresh',[]) |
---|
| 150 | beds_for_pre = getattr(hostel,'beds_for_pre',[]) |
---|
| 151 | beds_for_returning = getattr(hostel,'beds_for_returning',[]) |
---|
| 152 | beds_for_final = getattr(hostel,'beds_for_final',[]) |
---|
[6976] | 153 | bed_string = u'%s_%s_%s' % (block, room_nr, bed) |
---|
[6974] | 154 | if bt == 'reserved': |
---|
| 155 | bt = 'all' |
---|
| 156 | if bed in beds_for_fresh: |
---|
| 157 | bt = 'fr' |
---|
| 158 | elif bed in beds_for_pre: |
---|
| 159 | bt = 'pr' |
---|
| 160 | elif bed in beds_for_final: |
---|
| 161 | bt = 'fi' |
---|
| 162 | elif bed in beds_for_returning: |
---|
| 163 | bt = 're' |
---|
| 164 | bt = u'%s_%s_%s' % (sh, sex, bt) |
---|
[7718] | 165 | |
---|
[6991] | 166 | # Comment of Martijn: |
---|
[8186] | 167 | # If you have a non-Persistent subobject (like a list) and |
---|
| 168 | # you change it, you need to manually flag the persistence |
---|
| 169 | # machinery on the object that its subobject changed, with |
---|
| 170 | # _p_changed. This is only necessary if some of the |
---|
| 171 | # objects are not sublclasses of Persistent. For common |
---|
| 172 | # built-in collections in Python such as list and |
---|
| 173 | # dictionary there are replacements (PersistentList, |
---|
| 174 | # PersistentMapping), and more advanced building blocks |
---|
[6991] | 175 | # for indexes (BTrees), that don't have this issue. |
---|
[7718] | 176 | #hostel._p_changed = True |
---|
| 177 | |
---|
| 178 | # Henrik: Unfortunately, this doesn't work in our case. |
---|
| 179 | # After restarting the portal, |
---|
| 180 | # added or removed list items are gone. The only way to ensure |
---|
| 181 | # persistance is to reassign the list attribute. |
---|
| 182 | br = hostel.beds_reserved |
---|
| 183 | br.remove(bed_string) |
---|
| 184 | hostel.beds_reserved = br |
---|
| 185 | message = _(u'unreserved') |
---|
| 186 | else: |
---|
| 187 | bt = u'%s_%s_reserved' % (sh, sex) |
---|
| 188 | # See comment above |
---|
| 189 | br = hostel.beds_reserved |
---|
| 190 | br.append(bed_string) |
---|
| 191 | hostel.beds_reserved = br |
---|
| 192 | message = _(u'reserved') |
---|
[6974] | 193 | self.bed_type = bt |
---|
[6988] | 194 | return message |
---|
[6974] | 195 | |
---|
[7042] | 196 | def releaseBed(self): |
---|
| 197 | if self.owner == NOT_OCCUPIED: |
---|
[7070] | 198 | return |
---|
[7042] | 199 | else: |
---|
| 200 | old_owner = self.owner |
---|
| 201 | self.owner = NOT_OCCUPIED |
---|
| 202 | notify(grok.ObjectModifiedEvent(self)) |
---|
| 203 | accommodation_session = grok.getSite()[ |
---|
[8685] | 204 | 'hostels'].accommodation_session |
---|
[7042] | 205 | try: |
---|
| 206 | bedticket = grok.getSite()['students'][old_owner][ |
---|
| 207 | 'accommodation'][str(accommodation_session)] |
---|
| 208 | except KeyError: |
---|
| 209 | return '%s without bed ticket' % old_owner |
---|
| 210 | bedticket.bed = None |
---|
[8183] | 211 | tz = getUtility(IKofaUtils).tzinfo |
---|
[8234] | 212 | timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
[8186] | 213 | bedticket.bed_coordinates = u'-- booking cancelled on %s --' % ( |
---|
| 214 | timestamp,) |
---|
[7042] | 215 | return old_owner |
---|
| 216 | |
---|
[6963] | 217 | def loggerInfo(self, ob_class, comment=None): |
---|
| 218 | target = self.__name__ |
---|
| 219 | return grok.getSite()['hostels'].logger_info(ob_class,target,comment) |
---|
| 220 | |
---|
| 221 | Bed = attrs_to_fields(Bed) |
---|
[7006] | 222 | |
---|
| 223 | @grok.subscribe(IBedTicket, grok.IObjectRemovedEvent) |
---|
| 224 | def handle_bedticket_removed(bedticket, event): |
---|
| 225 | """If a bed ticket is deleted, we make sure that also the owner attribute |
---|
| 226 | of the bed is cleared (set to NOT_OCCUPIED). |
---|
| 227 | """ |
---|
[7068] | 228 | if bedticket.bed != None: |
---|
| 229 | bedticket.bed.owner = NOT_OCCUPIED |
---|
| 230 | notify(grok.ObjectModifiedEvent(bedticket.bed)) |
---|