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