[7195] | 1 | ## $Id: interfaces.py 13346 2015-10-26 08:57:30Z 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, |
---|
[13280] | 40 | description = _('Example: ') + u'2011-12-01 18:30:00+01:00', |
---|
[8685] | 41 | ) |
---|
| 42 | |
---|
| 43 | enddate = schema.Datetime( |
---|
| 44 | title = _(u'Hostel Allocation Closing Date'), |
---|
| 45 | required = False, |
---|
[13280] | 46 | description = _('Example: ') + u'2011-12-31 23:59:59+01:00', |
---|
[8685] | 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 | |
---|
[13316] | 73 | def releaseExpiredAllocations(n): |
---|
| 74 | """Release bed if bed allocation has expired. Allocation expires |
---|
| 75 | after `n` days if maintenance fee has not been paid. |
---|
| 76 | """ |
---|
| 77 | |
---|
[13166] | 78 | def writeLogMessage(view, message): |
---|
[13167] | 79 | """Add an INFO message to hostels.log. |
---|
[13165] | 80 | """ |
---|
| 81 | |
---|
[7819] | 82 | class IHostel(IKofaObject): |
---|
[13167] | 83 | """Representation of a hostel. |
---|
[6951] | 84 | """ |
---|
[6952] | 85 | |
---|
[9196] | 86 | bed_statistics = Attribute('Number of booked and total beds') |
---|
| 87 | |
---|
| 88 | def clearHostel(): |
---|
| 89 | """Remove all beds. |
---|
| 90 | """ |
---|
| 91 | |
---|
[6970] | 92 | def updateBeds(): |
---|
| 93 | """Fill hostel with beds or update beds. |
---|
| 94 | """ |
---|
| 95 | |
---|
[6952] | 96 | hostel_id = schema.TextLine( |
---|
[7718] | 97 | title = _(u'Hostel Id'), |
---|
[6956] | 98 | ) |
---|
| 99 | |
---|
| 100 | sort_id = schema.Int( |
---|
[7718] | 101 | title = _(u'Sort Id'), |
---|
[6954] | 102 | required = True, |
---|
[6956] | 103 | default = 10, |
---|
| 104 | ) |
---|
| 105 | |
---|
| 106 | hostel_name = schema.TextLine( |
---|
[7718] | 107 | title = _(u'Hostel Name'), |
---|
[6956] | 108 | required = True, |
---|
[6959] | 109 | default = u'Hall 1', |
---|
[6956] | 110 | ) |
---|
| 111 | |
---|
[6959] | 112 | floors_per_block = schema.Int( |
---|
[7718] | 113 | title = _(u'Floors per Block'), |
---|
[6956] | 114 | required = True, |
---|
[6971] | 115 | default = 1, |
---|
[6956] | 116 | ) |
---|
| 117 | |
---|
| 118 | rooms_per_floor = schema.Int( |
---|
[7718] | 119 | title = _(u'Rooms per Floor'), |
---|
[6956] | 120 | required = True, |
---|
[6971] | 121 | default = 2, |
---|
[6956] | 122 | ) |
---|
| 123 | |
---|
[6958] | 124 | blocks_for_female = schema.List( |
---|
[7718] | 125 | title = _(u'Blocks for Female Students'), |
---|
[6958] | 126 | value_type = schema.Choice( |
---|
| 127 | vocabulary = blocks |
---|
| 128 | ), |
---|
[13346] | 129 | default = [], |
---|
[6958] | 130 | ) |
---|
| 131 | |
---|
[6970] | 132 | blocks_for_male = schema.List( |
---|
[7718] | 133 | title = _(u'Blocks for Male Students'), |
---|
[6970] | 134 | value_type = schema.Choice( |
---|
| 135 | vocabulary = blocks |
---|
| 136 | ), |
---|
[13346] | 137 | default = [], |
---|
[6970] | 138 | ) |
---|
| 139 | |
---|
[9146] | 140 | beds_for_pre= schema.List( |
---|
| 141 | title = _(u'Beds for Pre-Study Students'), |
---|
| 142 | value_type = schema.Choice( |
---|
| 143 | vocabulary = bed_letters |
---|
| 144 | ), |
---|
[13346] | 145 | default = [], |
---|
[9146] | 146 | ) |
---|
| 147 | |
---|
[6958] | 148 | beds_for_fresh = schema.List( |
---|
[7718] | 149 | title = _(u'Beds for Fresh Students'), |
---|
[6958] | 150 | value_type = schema.Choice( |
---|
| 151 | vocabulary = bed_letters |
---|
| 152 | ), |
---|
[13346] | 153 | default = [], |
---|
[6958] | 154 | ) |
---|
| 155 | |
---|
[6970] | 156 | beds_for_returning = schema.List( |
---|
[7718] | 157 | title = _(u'Beds for Returning Students'), |
---|
[6970] | 158 | value_type = schema.Choice( |
---|
| 159 | vocabulary = bed_letters |
---|
| 160 | ), |
---|
[13346] | 161 | default = [], |
---|
[6970] | 162 | ) |
---|
| 163 | |
---|
[6958] | 164 | beds_for_final = schema.List( |
---|
[7718] | 165 | title = _(u'Beds for Final Year Students'), |
---|
[6958] | 166 | value_type = schema.Choice( |
---|
| 167 | vocabulary = bed_letters |
---|
| 168 | ), |
---|
[13346] | 169 | default = [], |
---|
[6958] | 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 | ), |
---|
[13346] | 177 | default = [], |
---|
[6971] | 178 | ) |
---|
| 179 | |
---|
[6970] | 180 | special_handling = schema.Choice( |
---|
[7718] | 181 | title = _(u'Special Handling'), |
---|
[9400] | 182 | source = SpecialHandlingSource(), |
---|
[6970] | 183 | required = True, |
---|
[6973] | 184 | default = u'regular', |
---|
[6970] | 185 | ) |
---|
| 186 | |
---|
[10680] | 187 | maint_fee = schema.Float( |
---|
| 188 | title = _(u'Rent'), |
---|
| 189 | default = 0.0, |
---|
| 190 | required = False, |
---|
| 191 | ) |
---|
| 192 | |
---|
[6970] | 193 | @invariant |
---|
| 194 | def blocksOverlap(hostel): |
---|
| 195 | bfe = hostel.blocks_for_female |
---|
| 196 | bma = hostel.blocks_for_male |
---|
| 197 | if set(bfe).intersection(set(bma)): |
---|
[7718] | 198 | raise Invalid(_('Female and male blocks overlap.')) |
---|
[6970] | 199 | |
---|
| 200 | @invariant |
---|
| 201 | def bedsOverlap(hostel): |
---|
[6981] | 202 | beds = (hostel.beds_for_fresh + |
---|
| 203 | hostel.beds_for_returning + |
---|
| 204 | hostel.beds_for_final + |
---|
[9146] | 205 | hostel.beds_for_pre + |
---|
[6981] | 206 | hostel.beds_for_all) |
---|
| 207 | if len(beds) != len(set(beds)): |
---|
[7718] | 208 | raise Invalid(_('Bed categories overlap.')) |
---|
[6970] | 209 | |
---|
[13166] | 210 | def writeLogMessage(view, message): |
---|
[13167] | 211 | """Add an INFO message to hostels.log. |
---|
[13166] | 212 | """ |
---|
| 213 | |
---|
[7819] | 214 | class IBed(IKofaObject): |
---|
[13167] | 215 | """Representation of a bed. |
---|
[6963] | 216 | """ |
---|
| 217 | |
---|
[13170] | 218 | coordinates = Attribute('Coordinates tuple derived from bed_id') |
---|
| 219 | hall = Attribute('Hall id, for exporter only') |
---|
| 220 | block = Attribute('Block letter, for exporter only') |
---|
| 221 | room = Attribute('Room number, for exporter only') |
---|
| 222 | bed = Attribute('Bed letter, for exporter only') |
---|
| 223 | special_handling = Attribute('Special handling code, for exporter only') |
---|
| 224 | sex = Attribute('Sex, for exporter only') |
---|
| 225 | bt = Attribute('Last part of bed type, for exporter only') |
---|
[9199] | 226 | |
---|
[6996] | 227 | def bookBed(student_id): |
---|
| 228 | """Book a bed for a student. |
---|
| 229 | """ |
---|
[6963] | 230 | |
---|
[6974] | 231 | def switchReservation(): |
---|
| 232 | """Reserves bed or relases reserved bed respectively. |
---|
| 233 | """ |
---|
| 234 | |
---|
[13316] | 235 | def releaseBedIfMaintenanceNotPaid(): |
---|
| 236 | """Release bed if maintenance fee has not been paid on time. |
---|
| 237 | """ |
---|
| 238 | |
---|
[6963] | 239 | bed_id = schema.TextLine( |
---|
[7718] | 240 | title = _(u'Bed Id'), |
---|
[6963] | 241 | required = True, |
---|
| 242 | default = u'', |
---|
| 243 | ) |
---|
| 244 | |
---|
| 245 | bed_type = schema.TextLine( |
---|
[7718] | 246 | title = _(u'Bed Type'), |
---|
[6963] | 247 | required = True, |
---|
| 248 | default = u'', |
---|
| 249 | ) |
---|
| 250 | |
---|
[6971] | 251 | bed_number = schema.Int( |
---|
[7718] | 252 | title = _(u'Bed Number'), |
---|
[6963] | 253 | required = True, |
---|
| 254 | ) |
---|
| 255 | |
---|
| 256 | owner = schema.TextLine( |
---|
[7718] | 257 | title = _(u'Owner (Student)'), |
---|
[9416] | 258 | description = _('Enter valid student id.'), |
---|
[6963] | 259 | required = True, |
---|
| 260 | default = u'', |
---|
| 261 | ) |
---|
[7068] | 262 | |
---|
[9414] | 263 | @invariant |
---|
| 264 | def allowed_owners(bed): |
---|
| 265 | if bed.owner == NOT_OCCUPIED: |
---|
| 266 | return |
---|
| 267 | catalog = getUtility(ICatalog, name='students_catalog') |
---|
| 268 | accommodation_session = getSite()['hostels'].accommodation_session |
---|
| 269 | students = catalog.searchResults(current_session=( |
---|
| 270 | accommodation_session,accommodation_session)) |
---|
| 271 | student_ids = [student.student_id for student in students] |
---|
| 272 | if not bed.owner in student_ids: |
---|
| 273 | raise Invalid(_( |
---|
[9416] | 274 | "Either student does not exist or student " |
---|
| 275 | "is not in accommodation session.")) |
---|
[9414] | 276 | catalog = getUtility(ICatalog, name='beds_catalog') |
---|
| 277 | beds = catalog.searchResults(owner=(bed.owner,bed.owner)) |
---|
| 278 | if len(beds): |
---|
| 279 | allocated_bed = [bed.bed_id for bed in beds][0] |
---|
| 280 | raise Invalid(_( |
---|
[13170] | 281 | "This student resides in bed ${a}.", |
---|
| 282 | mapping = {'a':allocated_bed})) |
---|
[13166] | 283 | |
---|
| 284 | def writeLogMessage(view, message): |
---|
[13167] | 285 | """Add an INFO message to hostels.log. |
---|
[13166] | 286 | """ |
---|