[7195] | 1 | ## $Id: hostel.py 13346 2015-10-26 08:57:30Z 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 |
---|
[9202] | 24 | from zope.component.interfaces import IFactory |
---|
[6951] | 25 | from datetime import datetime |
---|
[7811] | 26 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
| 27 | from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED |
---|
[9414] | 28 | from waeup.kofa.hostels.interfaces import IHostel, IBed |
---|
[7811] | 29 | from waeup.kofa.students.interfaces import IBedTicket |
---|
[8183] | 30 | from waeup.kofa.interfaces import IKofaUtils |
---|
[7811] | 31 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[8186] | 32 | from waeup.kofa.utils.helpers import now |
---|
[6951] | 33 | |
---|
| 34 | class Hostel(grok.Container): |
---|
| 35 | """This is a hostel. |
---|
| 36 | """ |
---|
| 37 | grok.implements(IHostel) |
---|
| 38 | grok.provides(IHostel) |
---|
| 39 | |
---|
[9196] | 40 | @property |
---|
| 41 | def bed_statistics(self): |
---|
| 42 | total = len(self.keys()) |
---|
| 43 | booked = 0 |
---|
| 44 | for value in self.values(): |
---|
| 45 | if value.owner != NOT_OCCUPIED: |
---|
| 46 | booked += 1 |
---|
| 47 | return {'booked':booked, 'total':total} |
---|
| 48 | |
---|
| 49 | def clearHostel(self): |
---|
[9197] | 50 | """Remove all beds |
---|
[9196] | 51 | """ |
---|
[9197] | 52 | keys = [i for i in self.keys()] # create deep copy |
---|
| 53 | for bed in keys: |
---|
| 54 | del self[bed] |
---|
| 55 | return |
---|
[9196] | 56 | |
---|
[6963] | 57 | def addBed(self, bed): |
---|
[6970] | 58 | """Add a bed. |
---|
[6963] | 59 | """ |
---|
| 60 | if not IBed.providedBy(bed): |
---|
| 61 | raise TypeError( |
---|
| 62 | 'Hostels contain only IBed instances') |
---|
[6970] | 63 | self[bed.bed_id] = bed |
---|
[6963] | 64 | return |
---|
| 65 | |
---|
[6970] | 66 | def updateBeds(self): |
---|
| 67 | """Fill hostel with beds or update beds. |
---|
| 68 | """ |
---|
| 69 | added_counter = 0 |
---|
| 70 | modified_counter = 0 |
---|
[6978] | 71 | removed_counter = 0 |
---|
[6988] | 72 | modified_beds = u'' |
---|
[6978] | 73 | |
---|
| 74 | # Remove all empty beds. Occupied beds remain in hostel! |
---|
| 75 | keys = list(self.keys()) # create list copy |
---|
| 76 | for key in keys: |
---|
| 77 | if self[key].owner == NOT_OCCUPIED: |
---|
| 78 | del self[key] |
---|
| 79 | self._p_changed = True |
---|
| 80 | removed_counter += 1 |
---|
[6998] | 81 | else: |
---|
| 82 | self[key].bed_number = 9999 |
---|
| 83 | remaining = len(keys) - removed_counter |
---|
[6978] | 84 | |
---|
[6970] | 85 | blocks_for_female = getattr(self,'blocks_for_female',[]) |
---|
| 86 | blocks_for_male = getattr(self,'blocks_for_male',[]) |
---|
| 87 | beds_for_fresh = getattr(self,'beds_for_fresh',[]) |
---|
| 88 | beds_for_pre = getattr(self,'beds_for_pre',[]) |
---|
| 89 | beds_for_returning = getattr(self,'beds_for_returning',[]) |
---|
| 90 | beds_for_final = getattr(self,'beds_for_final',[]) |
---|
[6971] | 91 | beds_for_all = getattr(self,'beds_for_all',[]) |
---|
[6970] | 92 | all_blocks = blocks_for_female + blocks_for_male |
---|
| 93 | all_beds = (beds_for_pre + beds_for_fresh + |
---|
[6971] | 94 | beds_for_returning + beds_for_final + beds_for_all) |
---|
[6970] | 95 | for block in all_blocks: |
---|
| 96 | sex = 'male' |
---|
| 97 | if block in blocks_for_female: |
---|
| 98 | sex = 'female' |
---|
| 99 | for floor in range(1,int(self.floors_per_block)+1): |
---|
| 100 | for room in range(1,int(self.rooms_per_floor)+1): |
---|
| 101 | for bed in all_beds: |
---|
| 102 | room_nr = floor*100 + room |
---|
| 103 | bt = 'all' |
---|
[13346] | 104 | if bed in beds_for_fresh: |
---|
[6970] | 105 | bt = 'fr' |
---|
| 106 | elif bed in beds_for_pre: |
---|
| 107 | bt = 'pr' |
---|
| 108 | elif bed in beds_for_final: |
---|
| 109 | bt = 'fi' |
---|
| 110 | elif bed in beds_for_returning: |
---|
| 111 | bt = 're' |
---|
[6973] | 112 | bt = u'%s_%s_%s' % (self.special_handling,sex,bt) |
---|
[13168] | 113 | uid = u'%s_%s_%d_%s' % ( |
---|
| 114 | self.hostel_id,block,room_nr,bed) |
---|
[9701] | 115 | if uid in self: |
---|
[6970] | 116 | bed = self[uid] |
---|
[13170] | 117 | # Renumber remaining bed |
---|
[6998] | 118 | bed.bed_number = len(self) + 1 - remaining |
---|
| 119 | remaining -= 1 |
---|
[6970] | 120 | if bed.bed_type != bt: |
---|
| 121 | bed.bed_type = bt |
---|
| 122 | modified_counter += 1 |
---|
[9448] | 123 | modified_beds += '%s, ' % uid |
---|
| 124 | notify(grok.ObjectModifiedEvent(bed)) |
---|
[6970] | 125 | else: |
---|
| 126 | bed = Bed() |
---|
| 127 | bed.bed_id = uid |
---|
| 128 | bed.bed_type = bt |
---|
[6998] | 129 | bed.bed_number = len(self) + 1 - remaining |
---|
[6970] | 130 | bed.owner = NOT_OCCUPIED |
---|
| 131 | self.addBed(bed) |
---|
| 132 | added_counter +=1 |
---|
[6988] | 133 | return removed_counter, added_counter, modified_counter, modified_beds |
---|
[6970] | 134 | |
---|
[13166] | 135 | def writeLogMessage(self, view, message): |
---|
| 136 | ob_class = view.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 137 | self.__parent__.logger.info( |
---|
| 138 | '%s - %s - %s' % (ob_class, self.__name__, message)) |
---|
| 139 | return |
---|
| 140 | |
---|
[6951] | 141 | Hostel = attrs_to_fields(Hostel) |
---|
[6963] | 142 | |
---|
| 143 | class Bed(grok.Container): |
---|
| 144 | """This is a bed. |
---|
| 145 | """ |
---|
[9414] | 146 | grok.implements(IBed) |
---|
[6963] | 147 | grok.provides(IBed) |
---|
| 148 | |
---|
[9199] | 149 | @property |
---|
| 150 | def coordinates(self): |
---|
[6974] | 151 | """Determine the coordinates from the bed_id. |
---|
[6963] | 152 | """ |
---|
[6974] | 153 | return self.bed_id.split('_') |
---|
[6963] | 154 | |
---|
[9199] | 155 | # The following property attributes are only needed |
---|
[13170] | 156 | # for the exporter to ease evaluation with Excel. |
---|
[9199] | 157 | |
---|
| 158 | @property |
---|
| 159 | def hall(self): |
---|
| 160 | return self.coordinates[0] |
---|
| 161 | |
---|
| 162 | @property |
---|
| 163 | def block(self): |
---|
| 164 | return self.coordinates[1] |
---|
| 165 | |
---|
| 166 | @property |
---|
| 167 | def room(self): |
---|
| 168 | return self.coordinates[2] |
---|
| 169 | |
---|
| 170 | @property |
---|
| 171 | def bed(self): |
---|
| 172 | return self.coordinates[3] |
---|
| 173 | |
---|
| 174 | @property |
---|
| 175 | def special_handling(self): |
---|
| 176 | return self.bed_type.split('_')[0] |
---|
| 177 | |
---|
| 178 | @property |
---|
| 179 | def sex(self): |
---|
| 180 | return self.bed_type.split('_')[1] |
---|
| 181 | |
---|
| 182 | @property |
---|
| 183 | def bt(self): |
---|
| 184 | return self.bed_type.split('_')[2] |
---|
| 185 | |
---|
| 186 | |
---|
[6996] | 187 | def bookBed(self, student_id): |
---|
[6998] | 188 | if self.owner == NOT_OCCUPIED: |
---|
| 189 | self.owner = student_id |
---|
[7003] | 190 | notify(grok.ObjectModifiedEvent(self)) |
---|
[6998] | 191 | return None |
---|
| 192 | else: |
---|
| 193 | return self.owner |
---|
[6996] | 194 | |
---|
[6974] | 195 | def switchReservation(self): |
---|
[7003] | 196 | """Reserves or unreserve bed respectively. |
---|
[6974] | 197 | """ |
---|
| 198 | sh, sex, bt = self.bed_type.split('_') |
---|
[9199] | 199 | hostel_id, block, room_nr, bed = self.coordinates |
---|
[6975] | 200 | hostel = self.__parent__ |
---|
[6988] | 201 | beds_for_fresh = getattr(hostel,'beds_for_fresh',[]) |
---|
| 202 | beds_for_pre = getattr(hostel,'beds_for_pre',[]) |
---|
| 203 | beds_for_returning = getattr(hostel,'beds_for_returning',[]) |
---|
| 204 | beds_for_final = getattr(hostel,'beds_for_final',[]) |
---|
[6976] | 205 | bed_string = u'%s_%s_%s' % (block, room_nr, bed) |
---|
[6974] | 206 | if bt == 'reserved': |
---|
| 207 | bt = 'all' |
---|
| 208 | if bed in beds_for_fresh: |
---|
| 209 | bt = 'fr' |
---|
| 210 | elif bed in beds_for_pre: |
---|
| 211 | bt = 'pr' |
---|
| 212 | elif bed in beds_for_final: |
---|
| 213 | bt = 'fi' |
---|
| 214 | elif bed in beds_for_returning: |
---|
| 215 | bt = 're' |
---|
| 216 | bt = u'%s_%s_%s' % (sh, sex, bt) |
---|
[7718] | 217 | message = _(u'unreserved') |
---|
| 218 | else: |
---|
| 219 | bt = u'%s_%s_reserved' % (sh, sex) |
---|
| 220 | message = _(u'reserved') |
---|
[6974] | 221 | self.bed_type = bt |
---|
[9448] | 222 | notify(grok.ObjectModifiedEvent(self)) |
---|
[6988] | 223 | return message |
---|
[6974] | 224 | |
---|
[7042] | 225 | def releaseBed(self): |
---|
| 226 | if self.owner == NOT_OCCUPIED: |
---|
[7070] | 227 | return |
---|
[13316] | 228 | old_owner = self.owner |
---|
| 229 | self.owner = NOT_OCCUPIED |
---|
| 230 | notify(grok.ObjectModifiedEvent(self)) |
---|
| 231 | accommodation_session = grok.getSite()[ |
---|
| 232 | 'hostels'].accommodation_session |
---|
| 233 | try: |
---|
| 234 | bedticket = grok.getSite()['students'][old_owner][ |
---|
| 235 | 'accommodation'][str(accommodation_session)] |
---|
| 236 | except KeyError: |
---|
| 237 | return '%s without bed ticket' % old_owner |
---|
| 238 | bedticket.bed = None |
---|
| 239 | tz = getUtility(IKofaUtils).tzinfo |
---|
| 240 | timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
| 241 | bedticket.bed_coordinates = u'-- booking cancelled on %s --' % ( |
---|
| 242 | timestamp,) |
---|
| 243 | return old_owner |
---|
| 244 | |
---|
| 245 | def releaseBedIfMaintenanceNotPaid(self, n=7): |
---|
| 246 | if self.owner == NOT_OCCUPIED: |
---|
| 247 | return |
---|
| 248 | accommodation_session = grok.getSite()[ |
---|
| 249 | 'hostels'].accommodation_session |
---|
| 250 | try: |
---|
| 251 | bedticket = grok.getSite()['students'][self.owner][ |
---|
| 252 | 'accommodation'][str(accommodation_session)] |
---|
| 253 | except KeyError: |
---|
| 254 | return |
---|
| 255 | if bedticket.maint_payment_made: |
---|
| 256 | return |
---|
| 257 | jetzt = datetime.utcnow() |
---|
| 258 | days_ago = getattr(jetzt - bedticket.booking_date, 'days') |
---|
| 259 | if days_ago > n: |
---|
[13318] | 260 | old_owner = self.owner |
---|
[7042] | 261 | self.owner = NOT_OCCUPIED |
---|
| 262 | notify(grok.ObjectModifiedEvent(self)) |
---|
| 263 | bedticket.bed = None |
---|
[8183] | 264 | tz = getUtility(IKofaUtils).tzinfo |
---|
[8234] | 265 | timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
[13316] | 266 | bedticket.bed_coordinates = u'-- booking expired (%s) --' % ( |
---|
[8186] | 267 | timestamp,) |
---|
[13318] | 268 | return old_owner |
---|
[13316] | 269 | return |
---|
[7042] | 270 | |
---|
[13166] | 271 | def writeLogMessage(self, view, message): |
---|
| 272 | ob_class = view.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 273 | self.__parent__.__parent__.logger.info( |
---|
| 274 | '%s - %s - %s' % (ob_class, self.__name__, message)) |
---|
| 275 | return |
---|
[6963] | 276 | |
---|
| 277 | Bed = attrs_to_fields(Bed) |
---|
[7006] | 278 | |
---|
[9202] | 279 | class HostelFactory(grok.GlobalUtility): |
---|
| 280 | """A factory for hostels. |
---|
| 281 | |
---|
| 282 | We need this factory for the hostel processor. |
---|
| 283 | """ |
---|
| 284 | grok.implements(IFactory) |
---|
| 285 | grok.name(u'waeup.Hostel') |
---|
| 286 | title = u"Create a new hostel.", |
---|
| 287 | description = u"This factory instantiates new hostel instances." |
---|
| 288 | |
---|
| 289 | def __call__(self, *args, **kw): |
---|
| 290 | return Hostel() |
---|
| 291 | |
---|
| 292 | def getInterfaces(self): |
---|
| 293 | return implementedBy(Hostel) |
---|
| 294 | |
---|
| 295 | |
---|
[7006] | 296 | @grok.subscribe(IBedTicket, grok.IObjectRemovedEvent) |
---|
| 297 | def handle_bedticket_removed(bedticket, event): |
---|
| 298 | """If a bed ticket is deleted, we make sure that also the owner attribute |
---|
| 299 | of the bed is cleared (set to NOT_OCCUPIED). |
---|
| 300 | """ |
---|
[7068] | 301 | if bedticket.bed != None: |
---|
| 302 | bedticket.bed.owner = NOT_OCCUPIED |
---|
| 303 | notify(grok.ObjectModifiedEvent(bedticket.bed)) |
---|
[9202] | 304 | |
---|