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