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