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