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