[7195] | 1 | ## $Id: interfaces.py 13167 2015-07-13 08:37:27Z henrik $ |
---|
[6951] | 2 | ## |
---|
[7195] | 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 | ## |
---|
[9414] | 18 | from grok import getSite |
---|
[8685] | 19 | from datetime import datetime |
---|
[9414] | 20 | from zope.component import getUtility |
---|
| 21 | from zope.catalog.interfaces import ICatalog |
---|
[9196] | 22 | from zope.interface import invariant, Invalid, Attribute |
---|
[6951] | 23 | from zope import schema |
---|
[8685] | 24 | from waeup.kofa.interfaces import ( |
---|
| 25 | IKofaObject, academic_sessions_vocab, registration_states_vocab) |
---|
[7811] | 26 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 27 | from waeup.kofa.hostels.vocabularies import ( |
---|
[9414] | 28 | bed_letters, blocks, SpecialHandlingSource, |
---|
| 29 | NOT_OCCUPIED) |
---|
[6951] | 30 | |
---|
[7819] | 31 | class IHostelsContainer(IKofaObject): |
---|
[13167] | 32 | """A container for hostel objects. |
---|
[6951] | 33 | """ |
---|
| 34 | |
---|
[13165] | 35 | expired = Attribute('True if current datetime is in application period.') |
---|
| 36 | |
---|
[8685] | 37 | startdate = schema.Datetime( |
---|
| 38 | title = _(u'Hostel Allocation Start Date'), |
---|
| 39 | required = False, |
---|
| 40 | description = _('Example:') + u'2011-12-01 18:30:00+01:00', |
---|
| 41 | ) |
---|
| 42 | |
---|
| 43 | enddate = schema.Datetime( |
---|
| 44 | title = _(u'Hostel Allocation Closing Date'), |
---|
| 45 | required = False, |
---|
| 46 | description = _('Example:') + u'2011-12-31 23:59:59+01:00', |
---|
| 47 | ) |
---|
| 48 | |
---|
| 49 | accommodation_session = schema.Choice( |
---|
| 50 | title = _(u'Booking Session'), |
---|
| 51 | source = academic_sessions_vocab, |
---|
| 52 | default = datetime.now().year, |
---|
| 53 | required = False, |
---|
| 54 | readonly = False, |
---|
| 55 | ) |
---|
| 56 | |
---|
| 57 | accommodation_states = schema.List( |
---|
| 58 | title = _(u'Allowed States'), |
---|
| 59 | value_type = schema.Choice( |
---|
| 60 | vocabulary = registration_states_vocab, |
---|
| 61 | ), |
---|
| 62 | default = [], |
---|
| 63 | ) |
---|
| 64 | |
---|
[9197] | 65 | def clearAllHostels(): |
---|
| 66 | """Clear all hostels. |
---|
| 67 | """ |
---|
| 68 | |
---|
[13165] | 69 | def addHostel(hostel): |
---|
| 70 | """Add a hostel. |
---|
| 71 | """ |
---|
| 72 | |
---|
[13166] | 73 | def writeLogMessage(view, message): |
---|
[13167] | 74 | """Add an INFO message to hostels.log. |
---|
[13165] | 75 | """ |
---|
| 76 | |
---|
[7819] | 77 | class IHostel(IKofaObject): |
---|
[13167] | 78 | """Representation of a hostel. |
---|
[6951] | 79 | """ |
---|
[6952] | 80 | |
---|
[9196] | 81 | bed_statistics = Attribute('Number of booked and total beds') |
---|
| 82 | |
---|
| 83 | def clearHostel(): |
---|
| 84 | """Remove all beds. |
---|
| 85 | """ |
---|
| 86 | |
---|
[6970] | 87 | def updateBeds(): |
---|
| 88 | """Fill hostel with beds or update beds. |
---|
| 89 | """ |
---|
| 90 | |
---|
[6952] | 91 | hostel_id = schema.TextLine( |
---|
[7718] | 92 | title = _(u'Hostel Id'), |
---|
[6956] | 93 | ) |
---|
| 94 | |
---|
| 95 | sort_id = schema.Int( |
---|
[7718] | 96 | title = _(u'Sort Id'), |
---|
[6954] | 97 | required = True, |
---|
[6956] | 98 | default = 10, |
---|
| 99 | ) |
---|
| 100 | |
---|
| 101 | hostel_name = schema.TextLine( |
---|
[7718] | 102 | title = _(u'Hostel Name'), |
---|
[6956] | 103 | required = True, |
---|
[6959] | 104 | default = u'Hall 1', |
---|
[6956] | 105 | ) |
---|
| 106 | |
---|
[6959] | 107 | floors_per_block = schema.Int( |
---|
[7718] | 108 | title = _(u'Floors per Block'), |
---|
[6956] | 109 | required = True, |
---|
[6971] | 110 | default = 1, |
---|
[6956] | 111 | ) |
---|
| 112 | |
---|
| 113 | rooms_per_floor = schema.Int( |
---|
[7718] | 114 | title = _(u'Rooms per Floor'), |
---|
[6956] | 115 | required = True, |
---|
[6971] | 116 | default = 2, |
---|
[6956] | 117 | ) |
---|
| 118 | |
---|
[6976] | 119 | beds_reserved = schema.List( |
---|
[7718] | 120 | title = _(u'Reserved Beds'), |
---|
[6975] | 121 | value_type = schema.TextLine( |
---|
| 122 | default = u'', |
---|
| 123 | required = False, |
---|
| 124 | ), |
---|
| 125 | required = True, |
---|
| 126 | readonly = False, |
---|
| 127 | default = [], |
---|
| 128 | ) |
---|
| 129 | |
---|
[6958] | 130 | blocks_for_female = schema.List( |
---|
[7718] | 131 | title = _(u'Blocks for Female Students'), |
---|
[6958] | 132 | value_type = schema.Choice( |
---|
| 133 | vocabulary = blocks |
---|
| 134 | ), |
---|
| 135 | ) |
---|
| 136 | |
---|
[6970] | 137 | blocks_for_male = schema.List( |
---|
[7718] | 138 | title = _(u'Blocks for Male Students'), |
---|
[6970] | 139 | value_type = schema.Choice( |
---|
| 140 | vocabulary = blocks |
---|
| 141 | ), |
---|
| 142 | ) |
---|
| 143 | |
---|
[9146] | 144 | beds_for_pre= schema.List( |
---|
| 145 | title = _(u'Beds for Pre-Study Students'), |
---|
| 146 | value_type = schema.Choice( |
---|
| 147 | vocabulary = bed_letters |
---|
| 148 | ), |
---|
| 149 | ) |
---|
| 150 | |
---|
[6958] | 151 | beds_for_fresh = schema.List( |
---|
[7718] | 152 | title = _(u'Beds for Fresh Students'), |
---|
[6958] | 153 | value_type = schema.Choice( |
---|
| 154 | vocabulary = bed_letters |
---|
| 155 | ), |
---|
| 156 | ) |
---|
| 157 | |
---|
[6970] | 158 | beds_for_returning = schema.List( |
---|
[7718] | 159 | title = _(u'Beds for Returning Students'), |
---|
[6970] | 160 | value_type = schema.Choice( |
---|
| 161 | vocabulary = bed_letters |
---|
| 162 | ), |
---|
| 163 | ) |
---|
| 164 | |
---|
[6958] | 165 | beds_for_final = schema.List( |
---|
[7718] | 166 | title = _(u'Beds for Final Year Students'), |
---|
[6958] | 167 | value_type = schema.Choice( |
---|
| 168 | vocabulary = bed_letters |
---|
| 169 | ), |
---|
| 170 | ) |
---|
[6963] | 171 | |
---|
[6971] | 172 | beds_for_all = schema.List( |
---|
[7718] | 173 | title = _(u'Beds without category'), |
---|
[6971] | 174 | value_type = schema.Choice( |
---|
| 175 | vocabulary = bed_letters |
---|
| 176 | ), |
---|
| 177 | ) |
---|
| 178 | |
---|
[6970] | 179 | special_handling = schema.Choice( |
---|
[7718] | 180 | title = _(u'Special Handling'), |
---|
[9400] | 181 | source = SpecialHandlingSource(), |
---|
[6970] | 182 | required = True, |
---|
[6973] | 183 | default = u'regular', |
---|
[6970] | 184 | ) |
---|
| 185 | |
---|
[10680] | 186 | maint_fee = schema.Float( |
---|
| 187 | title = _(u'Rent'), |
---|
| 188 | default = 0.0, |
---|
| 189 | required = False, |
---|
| 190 | ) |
---|
| 191 | |
---|
[6970] | 192 | @invariant |
---|
| 193 | def blocksOverlap(hostel): |
---|
| 194 | bfe = hostel.blocks_for_female |
---|
| 195 | bma = hostel.blocks_for_male |
---|
| 196 | if set(bfe).intersection(set(bma)): |
---|
[7718] | 197 | raise Invalid(_('Female and male blocks overlap.')) |
---|
[6970] | 198 | |
---|
| 199 | @invariant |
---|
| 200 | def bedsOverlap(hostel): |
---|
[6981] | 201 | beds = (hostel.beds_for_fresh + |
---|
| 202 | hostel.beds_for_returning + |
---|
| 203 | hostel.beds_for_final + |
---|
[9146] | 204 | hostel.beds_for_pre + |
---|
[6981] | 205 | hostel.beds_for_all) |
---|
| 206 | if len(beds) != len(set(beds)): |
---|
[7718] | 207 | raise Invalid(_('Bed categories overlap.')) |
---|
[6970] | 208 | |
---|
[13166] | 209 | def writeLogMessage(view, message): |
---|
[13167] | 210 | """Add an INFO message to hostels.log. |
---|
[13166] | 211 | """ |
---|
| 212 | |
---|
[7819] | 213 | class IBed(IKofaObject): |
---|
[13167] | 214 | """Representation of a bed. |
---|
[6963] | 215 | """ |
---|
| 216 | |
---|
[9199] | 217 | coordinates = Attribute('The coordinates of the bed from bed_id') |
---|
| 218 | |
---|
[6996] | 219 | def bookBed(student_id): |
---|
| 220 | """Book a bed for a student. |
---|
| 221 | """ |
---|
[6963] | 222 | |
---|
[6974] | 223 | def switchReservation(): |
---|
| 224 | """Reserves bed or relases reserved bed respectively. |
---|
| 225 | """ |
---|
| 226 | |
---|
[6963] | 227 | bed_id = schema.TextLine( |
---|
[7718] | 228 | title = _(u'Bed Id'), |
---|
[6963] | 229 | required = True, |
---|
| 230 | default = u'', |
---|
| 231 | ) |
---|
| 232 | |
---|
| 233 | bed_type = schema.TextLine( |
---|
[7718] | 234 | title = _(u'Bed Type'), |
---|
[6963] | 235 | required = True, |
---|
| 236 | default = u'', |
---|
| 237 | ) |
---|
| 238 | |
---|
[6971] | 239 | bed_number = schema.Int( |
---|
[7718] | 240 | title = _(u'Bed Number'), |
---|
[6963] | 241 | required = True, |
---|
| 242 | ) |
---|
| 243 | |
---|
| 244 | owner = schema.TextLine( |
---|
[7718] | 245 | title = _(u'Owner (Student)'), |
---|
[9416] | 246 | description = _('Enter valid student id.'), |
---|
[6963] | 247 | required = True, |
---|
| 248 | default = u'', |
---|
| 249 | ) |
---|
[7068] | 250 | |
---|
[9414] | 251 | @invariant |
---|
| 252 | def allowed_owners(bed): |
---|
| 253 | if bed.owner == NOT_OCCUPIED: |
---|
| 254 | return |
---|
| 255 | catalog = getUtility(ICatalog, name='students_catalog') |
---|
| 256 | accommodation_session = getSite()['hostels'].accommodation_session |
---|
| 257 | students = catalog.searchResults(current_session=( |
---|
| 258 | accommodation_session,accommodation_session)) |
---|
| 259 | student_ids = [student.student_id for student in students] |
---|
| 260 | if not bed.owner in student_ids: |
---|
| 261 | raise Invalid(_( |
---|
[9416] | 262 | "Either student does not exist or student " |
---|
| 263 | "is not in accommodation session.")) |
---|
[9414] | 264 | catalog = getUtility(ICatalog, name='beds_catalog') |
---|
| 265 | beds = catalog.searchResults(owner=(bed.owner,bed.owner)) |
---|
| 266 | if len(beds): |
---|
| 267 | allocated_bed = [bed.bed_id for bed in beds][0] |
---|
| 268 | raise Invalid(_( |
---|
| 269 | "This student resides in bed ${a}.", mapping = {'a':allocated_bed})) |
---|
[13166] | 270 | |
---|
| 271 | def writeLogMessage(view, message): |
---|
[13167] | 272 | """Add an INFO message to hostels.log. |
---|
[13166] | 273 | """ |
---|