[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 | # not yet tested nor used |
---|
| 47 | def updateBeds(self): |
---|
| 48 | """Fill hostel with beds or update beds. |
---|
| 49 | """ |
---|
| 50 | added_counter = 0 |
---|
| 51 | modified_counter = 0 |
---|
| 52 | blocks_for_female = getattr(self,'blocks_for_female',[]) |
---|
| 53 | blocks_for_male = getattr(self,'blocks_for_male',[]) |
---|
| 54 | beds_for_fresh = getattr(self,'beds_for_fresh',[]) |
---|
| 55 | beds_for_pre = getattr(self,'beds_for_pre',[]) |
---|
| 56 | beds_for_returning = getattr(self,'beds_for_returning',[]) |
---|
| 57 | beds_for_final = getattr(self,'beds_for_final',[]) |
---|
[6971] | 58 | beds_for_all = getattr(self,'beds_for_all',[]) |
---|
[6976] | 59 | beds_reserved = getattr(self,'beds_reserved',[]) |
---|
[6970] | 60 | all_blocks = blocks_for_female + blocks_for_male |
---|
| 61 | all_beds = (beds_for_pre + beds_for_fresh + |
---|
[6971] | 62 | beds_for_returning + beds_for_final + beds_for_all) |
---|
[6970] | 63 | for block in all_blocks: |
---|
| 64 | sex = 'male' |
---|
| 65 | if block in blocks_for_female: |
---|
| 66 | sex = 'female' |
---|
| 67 | for floor in range(1,int(self.floors_per_block)+1): |
---|
| 68 | for room in range(1,int(self.rooms_per_floor)+1): |
---|
| 69 | for bed in all_beds: |
---|
| 70 | room_nr = floor*100 + room |
---|
| 71 | bt = 'all' |
---|
[6976] | 72 | if '%s_%s_%s' % (block,room_nr,bed) in beds_reserved: |
---|
[6970] | 73 | bt = "reserved" |
---|
| 74 | elif bed in beds_for_fresh: |
---|
| 75 | bt = 'fr' |
---|
| 76 | elif bed in beds_for_pre: |
---|
| 77 | bt = 'pr' |
---|
| 78 | elif bed in beds_for_final: |
---|
| 79 | bt = 'fi' |
---|
| 80 | elif bed in beds_for_returning: |
---|
| 81 | bt = 're' |
---|
[6973] | 82 | bt = u'%s_%s_%s' % (self.special_handling,sex,bt) |
---|
[6970] | 83 | uid = u'%s_%s_%d_%s' % (self.hostel_id,block,room_nr,bed) |
---|
| 84 | if self.has_key(uid): |
---|
| 85 | bed = self[uid] |
---|
| 86 | if bed.bed_type != bt: |
---|
| 87 | bed.bed_type = bt |
---|
| 88 | #modified.append('"%(uid)s","%(bt)s","%(bed.owner)s"' % vars()) |
---|
| 89 | modified_counter += 1 |
---|
| 90 | else: |
---|
| 91 | bed = Bed() |
---|
| 92 | bed.bed_id = uid |
---|
| 93 | bed.bed_type = bt |
---|
[6971] | 94 | bed.bed_number = len(self) + 1 |
---|
[6970] | 95 | bed.owner = NOT_OCCUPIED |
---|
| 96 | self.addBed(bed) |
---|
| 97 | #generated.append('"%(uid)s","%(bt)s"' % vars()) |
---|
| 98 | added_counter +=1 |
---|
| 99 | return added_counter, modified_counter |
---|
| 100 | |
---|
[6951] | 101 | Hostel = attrs_to_fields(Hostel) |
---|
[6963] | 102 | |
---|
| 103 | class Bed(grok.Container): |
---|
| 104 | """This is a bed. |
---|
| 105 | """ |
---|
| 106 | grok.implements(IBed) |
---|
| 107 | grok.provides(IBed) |
---|
| 108 | |
---|
| 109 | def getBedCoordinates(self): |
---|
[6974] | 110 | """Determine the coordinates from the bed_id. |
---|
[6963] | 111 | """ |
---|
[6974] | 112 | return self.bed_id.split('_') |
---|
[6963] | 113 | |
---|
[6974] | 114 | def switchReservation(self): |
---|
| 115 | """Reserves a bed or relases a reserved bed respectively. |
---|
| 116 | """ |
---|
| 117 | sh, sex, bt = self.bed_type.split('_') |
---|
| 118 | hostel_id, block, room_nr, bed = self.getBedCoordinates() |
---|
| 119 | beds_for_fresh = getattr(self,'beds_for_fresh',[]) |
---|
| 120 | beds_for_pre = getattr(self,'beds_for_pre',[]) |
---|
| 121 | beds_for_returning = getattr(self,'beds_for_returning',[]) |
---|
| 122 | beds_for_final = getattr(self,'beds_for_final',[]) |
---|
[6975] | 123 | hostel = self.__parent__ |
---|
[6976] | 124 | bed_string = u'%s_%s_%s' % (block, room_nr, bed) |
---|
[6974] | 125 | if bt == 'reserved': |
---|
| 126 | bt = 'all' |
---|
| 127 | if bed in beds_for_fresh: |
---|
| 128 | bt = 'fr' |
---|
| 129 | elif bed in beds_for_pre: |
---|
| 130 | bt = 'pr' |
---|
| 131 | elif bed in beds_for_final: |
---|
| 132 | bt = 'fi' |
---|
| 133 | elif bed in beds_for_returning: |
---|
| 134 | bt = 're' |
---|
| 135 | bt = u'%s_%s_%s' % (sh, sex, bt) |
---|
[6976] | 136 | hostel.beds_reserved.remove(bed_string) |
---|
[6974] | 137 | else: |
---|
| 138 | bt = u'%s_%s_reserved' % (sh, sex) |
---|
[6976] | 139 | hostel.beds_reserved.append(bed_string) |
---|
[6974] | 140 | self.bed_type = bt |
---|
| 141 | return |
---|
| 142 | |
---|
[6963] | 143 | def loggerInfo(self, ob_class, comment=None): |
---|
| 144 | target = self.__name__ |
---|
| 145 | return grok.getSite()['hostels'].logger_info(ob_class,target,comment) |
---|
| 146 | |
---|
| 147 | Bed = attrs_to_fields(Bed) |
---|