[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 | """ |
---|
| 17 | These are the hostels. |
---|
| 18 | """ |
---|
| 19 | import grok |
---|
| 20 | from datetime import datetime |
---|
| 21 | from grok import index |
---|
| 22 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
[6963] | 23 | from waeup.sirp.hostels.interfaces import IHostel, IBed |
---|
[6951] | 24 | |
---|
[6970] | 25 | NOT_OCCUPIED = u'not occupied' |
---|
| 26 | |
---|
[6951] | 27 | class Hostel(grok.Container): |
---|
| 28 | """This is a hostel. |
---|
| 29 | """ |
---|
| 30 | grok.implements(IHostel) |
---|
| 31 | grok.provides(IHostel) |
---|
| 32 | |
---|
[6952] | 33 | def loggerInfo(self, ob_class, comment=None): |
---|
| 34 | target = self.__name__ |
---|
| 35 | return grok.getSite()['hostels'].logger_info(ob_class,target,comment) |
---|
| 36 | |
---|
[6963] | 37 | def addBed(self, bed): |
---|
[6970] | 38 | """Add a bed. |
---|
[6963] | 39 | """ |
---|
| 40 | if not IBed.providedBy(bed): |
---|
| 41 | raise TypeError( |
---|
| 42 | 'Hostels contain only IBed instances') |
---|
[6970] | 43 | self[bed.bed_id] = bed |
---|
[6963] | 44 | return |
---|
| 45 | |
---|
[6970] | 46 | def updateBeds(self): |
---|
| 47 | """Fill hostel with beds or update beds. |
---|
| 48 | """ |
---|
| 49 | added_counter = 0 |
---|
| 50 | modified_counter = 0 |
---|
[6978] | 51 | removed_counter = 0 |
---|
| 52 | |
---|
| 53 | # Remove all empty beds. Occupied beds remain in hostel! |
---|
| 54 | keys = list(self.keys()) # create list copy |
---|
| 55 | for key in keys: |
---|
| 56 | if self[key].owner == NOT_OCCUPIED: |
---|
| 57 | del self[key] |
---|
| 58 | self._p_changed = True |
---|
| 59 | removed_counter += 1 |
---|
| 60 | |
---|
[6970] | 61 | blocks_for_female = getattr(self,'blocks_for_female',[]) |
---|
| 62 | blocks_for_male = getattr(self,'blocks_for_male',[]) |
---|
| 63 | beds_for_fresh = getattr(self,'beds_for_fresh',[]) |
---|
| 64 | beds_for_pre = getattr(self,'beds_for_pre',[]) |
---|
| 65 | beds_for_returning = getattr(self,'beds_for_returning',[]) |
---|
| 66 | beds_for_final = getattr(self,'beds_for_final',[]) |
---|
[6971] | 67 | beds_for_all = getattr(self,'beds_for_all',[]) |
---|
[6976] | 68 | beds_reserved = getattr(self,'beds_reserved',[]) |
---|
[6970] | 69 | all_blocks = blocks_for_female + blocks_for_male |
---|
| 70 | all_beds = (beds_for_pre + beds_for_fresh + |
---|
[6971] | 71 | beds_for_returning + beds_for_final + beds_for_all) |
---|
[6970] | 72 | for block in all_blocks: |
---|
| 73 | sex = 'male' |
---|
| 74 | if block in blocks_for_female: |
---|
| 75 | sex = 'female' |
---|
| 76 | for floor in range(1,int(self.floors_per_block)+1): |
---|
| 77 | for room in range(1,int(self.rooms_per_floor)+1): |
---|
| 78 | for bed in all_beds: |
---|
| 79 | room_nr = floor*100 + room |
---|
| 80 | bt = 'all' |
---|
[6976] | 81 | if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved: |
---|
[6970] | 82 | bt = "reserved" |
---|
| 83 | elif bed in beds_for_fresh: |
---|
| 84 | bt = 'fr' |
---|
| 85 | elif bed in beds_for_pre: |
---|
| 86 | bt = 'pr' |
---|
| 87 | elif bed in beds_for_final: |
---|
| 88 | bt = 'fi' |
---|
| 89 | elif bed in beds_for_returning: |
---|
| 90 | bt = 're' |
---|
[6973] | 91 | bt = u'%s_%s_%s' % (self.special_handling,sex,bt) |
---|
[6970] | 92 | uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed) |
---|
| 93 | if self.has_key(uid): |
---|
| 94 | bed = self[uid] |
---|
| 95 | if bed.bed_type != bt: |
---|
| 96 | bed.bed_type = bt |
---|
| 97 | modified_counter += 1 |
---|
| 98 | else: |
---|
| 99 | bed = Bed() |
---|
| 100 | bed.bed_id = uid |
---|
| 101 | bed.bed_type = bt |
---|
[6971] | 102 | bed.bed_number = len(self) + 1 |
---|
[6970] | 103 | bed.owner = NOT_OCCUPIED |
---|
| 104 | self.addBed(bed) |
---|
| 105 | added_counter +=1 |
---|
[6978] | 106 | return removed_counter, added_counter, modified_counter |
---|
[6970] | 107 | |
---|
[6951] | 108 | Hostel = attrs_to_fields(Hostel) |
---|
[6963] | 109 | |
---|
| 110 | class Bed(grok.Container): |
---|
| 111 | """This is a bed. |
---|
| 112 | """ |
---|
| 113 | grok.implements(IBed) |
---|
| 114 | grok.provides(IBed) |
---|
| 115 | |
---|
| 116 | def getBedCoordinates(self): |
---|
[6974] | 117 | """Determine the coordinates from the bed_id. |
---|
[6963] | 118 | """ |
---|
[6974] | 119 | return self.bed_id.split('_') |
---|
[6963] | 120 | |
---|
[6974] | 121 | def switchReservation(self): |
---|
| 122 | """Reserves a bed or relases a reserved bed respectively. |
---|
| 123 | """ |
---|
| 124 | sh, sex, bt = self.bed_type.split('_') |
---|
| 125 | hostel_id, block, room_nr, bed = self.getBedCoordinates() |
---|
| 126 | beds_for_fresh = getattr(self,'beds_for_fresh',[]) |
---|
| 127 | beds_for_pre = getattr(self,'beds_for_pre',[]) |
---|
| 128 | beds_for_returning = getattr(self,'beds_for_returning',[]) |
---|
| 129 | beds_for_final = getattr(self,'beds_for_final',[]) |
---|
[6975] | 130 | hostel = self.__parent__ |
---|
[6976] | 131 | bed_string = u'%s_%s_%s' % (block, room_nr, bed) |
---|
[6974] | 132 | if bt == 'reserved': |
---|
| 133 | bt = 'all' |
---|
| 134 | if bed in beds_for_fresh: |
---|
| 135 | bt = 'fr' |
---|
| 136 | elif bed in beds_for_pre: |
---|
| 137 | bt = 'pr' |
---|
| 138 | elif bed in beds_for_final: |
---|
| 139 | bt = 'fi' |
---|
| 140 | elif bed in beds_for_returning: |
---|
| 141 | bt = 're' |
---|
| 142 | bt = u'%s_%s_%s' % (sh, sex, bt) |
---|
[6976] | 143 | hostel.beds_reserved.remove(bed_string) |
---|
[6974] | 144 | else: |
---|
| 145 | bt = u'%s_%s_reserved' % (sh, sex) |
---|
[6976] | 146 | hostel.beds_reserved.append(bed_string) |
---|
[6974] | 147 | self.bed_type = bt |
---|
| 148 | return |
---|
| 149 | |
---|
[6963] | 150 | def loggerInfo(self, ob_class, comment=None): |
---|
| 151 | target = self.__name__ |
---|
| 152 | return grok.getSite()['hostels'].logger_info(ob_class,target,comment) |
---|
| 153 | |
---|
| 154 | Bed = attrs_to_fields(Bed) |
---|