[7195] | 1 | ## $Id: browser.py 13494 2015-11-24 15:13:31Z henrik $ |
---|
| 2 | ## |
---|
[6953] | 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 | """UI components for hostels and related components. |
---|
| 19 | """ |
---|
| 20 | import grok |
---|
[6973] | 21 | import sys |
---|
[7718] | 22 | from zope.i18n import translate |
---|
| 23 | from zope.component import getUtility |
---|
[13445] | 24 | from zope.catalog.interfaces import ICatalog |
---|
[9217] | 25 | from waeup.kofa.browser.layout import ( |
---|
[7819] | 26 | KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage, |
---|
[13319] | 27 | NullValidator, UtilityView) |
---|
[7811] | 28 | from waeup.kofa.browser.breadcrumbs import Breadcrumb |
---|
| 29 | from waeup.kofa.browser.layout import default_primary_nav_template |
---|
| 30 | from waeup.kofa.browser.pages import delSubobjects |
---|
| 31 | from waeup.kofa.browser.viewlets import ( |
---|
[7257] | 32 | ManageActionButton, PrimaryNavTab) |
---|
[7811] | 33 | from waeup.kofa.browser.layout import jsaction, action |
---|
[13177] | 34 | from waeup.kofa.interfaces import IKofaObject, IKofaUtils, DOCLINK |
---|
[7811] | 35 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 36 | from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED |
---|
| 37 | from waeup.kofa.hostels.hostel import Hostel |
---|
| 38 | from waeup.kofa.hostels.interfaces import ( |
---|
[9414] | 39 | IHostelsContainer, IHostel, IBed) |
---|
[8685] | 40 | from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget |
---|
[6953] | 41 | |
---|
[6956] | 42 | # Save function used for save methods in manager pages |
---|
| 43 | def msave(view, **data): |
---|
| 44 | changed_fields = view.applyData(view.context, **data) |
---|
| 45 | # Turn list of lists into single list |
---|
| 46 | if changed_fields: |
---|
| 47 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
| 48 | fields_string = ' + '.join(changed_fields) |
---|
| 49 | view.context._p_changed = True |
---|
[7718] | 50 | view.flash(_('Form has been saved.')) |
---|
[6956] | 51 | if fields_string: |
---|
[13166] | 52 | view.context.writeLogMessage(view, 'saved: %s' % fields_string) |
---|
[6956] | 53 | return |
---|
| 54 | |
---|
[6953] | 55 | class HostelsTab(PrimaryNavTab): |
---|
| 56 | """Hostels tab in primary navigation. |
---|
| 57 | """ |
---|
| 58 | |
---|
[7819] | 59 | grok.context(IKofaObject) |
---|
[6953] | 60 | grok.order(5) |
---|
| 61 | grok.require('waeup.viewHostels') |
---|
[10771] | 62 | grok.name('hostelstab') |
---|
[7243] | 63 | template = default_primary_nav_template |
---|
[6953] | 64 | pnav = 5 |
---|
[7674] | 65 | tab_title = _(u'Hostels') |
---|
[6953] | 66 | |
---|
| 67 | @property |
---|
| 68 | def link_target(self): |
---|
| 69 | return self.view.application_url('hostels') |
---|
| 70 | |
---|
| 71 | class HostelsBreadcrumb(Breadcrumb): |
---|
| 72 | """A breadcrumb for the hostels container. |
---|
| 73 | """ |
---|
| 74 | grok.context(IHostelsContainer) |
---|
[7718] | 75 | title = _(u'Hostels') |
---|
[6953] | 76 | |
---|
| 77 | class HostelBreadcrumb(Breadcrumb): |
---|
| 78 | """A breadcrumb for the hostel container. |
---|
| 79 | """ |
---|
| 80 | grok.context(IHostel) |
---|
| 81 | |
---|
[6959] | 82 | def title(self): |
---|
| 83 | return self.context.hostel_name |
---|
| 84 | |
---|
[7068] | 85 | class BedBreadcrumb(Breadcrumb): |
---|
| 86 | """A breadcrumb for the hostel container. |
---|
| 87 | """ |
---|
| 88 | grok.context(IBed) |
---|
| 89 | |
---|
| 90 | def title(self): |
---|
[9199] | 91 | co = self.context.coordinates |
---|
[7718] | 92 | return _('Block ${a}, Room ${b}, Bed ${c}', |
---|
| 93 | mapping = {'a':co[1], 'b':co[2], 'c':co[3]}) |
---|
[7068] | 94 | |
---|
[7819] | 95 | class HostelsContainerPage(KofaDisplayFormPage): |
---|
[6953] | 96 | """The standard view for hostels containers. |
---|
| 97 | """ |
---|
| 98 | grok.context(IHostelsContainer) |
---|
| 99 | grok.name('index') |
---|
| 100 | grok.require('waeup.viewHostels') |
---|
[6959] | 101 | grok.template('containerpage') |
---|
[7718] | 102 | label = _('Accommodation Section') |
---|
[6953] | 103 | pnav = 5 |
---|
[8685] | 104 | form_fields = grok.AutoFields(IHostelsContainer) |
---|
| 105 | form_fields[ |
---|
| 106 | 'startdate'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 107 | form_fields[ |
---|
| 108 | 'enddate'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
[6953] | 109 | |
---|
| 110 | class HostelsContainerManageActionButton(ManageActionButton): |
---|
| 111 | grok.order(1) |
---|
| 112 | grok.context(IHostelsContainer) |
---|
| 113 | grok.view(HostelsContainerPage) |
---|
| 114 | grok.require('waeup.manageHostels') |
---|
[7718] | 115 | text = _('Manage accommodation section') |
---|
[6953] | 116 | |
---|
[13484] | 117 | class HostelsStatisticsActionButton(ManageActionButton): |
---|
| 118 | grok.order(2) |
---|
| 119 | grok.context(IHostelsContainer) |
---|
| 120 | grok.view(HostelsContainerPage) |
---|
| 121 | grok.require('waeup.manageHostels') |
---|
| 122 | icon = 'actionicon_statistics.png' |
---|
| 123 | text = _('Bed statistics') |
---|
| 124 | target = 'statistics' |
---|
| 125 | |
---|
[8685] | 126 | class HostelsContainerManagePage(KofaEditFormPage): |
---|
[6953] | 127 | """The manage page for hostel containers. |
---|
| 128 | """ |
---|
| 129 | grok.context(IHostelsContainer) |
---|
| 130 | grok.name('manage') |
---|
| 131 | grok.require('waeup.manageHostels') |
---|
[6959] | 132 | grok.template('containermanagepage') |
---|
[6953] | 133 | pnav = 5 |
---|
[7718] | 134 | label = _('Manage accommodation section') |
---|
[8685] | 135 | form_fields = grok.AutoFields(IHostelsContainer) |
---|
| 136 | taboneactions = [_('Save')] |
---|
[9197] | 137 | tabtwoactions = [_('Add hostel'), |
---|
| 138 | _('Clear all hostels'), |
---|
| 139 | _('Remove selected')] |
---|
[13177] | 140 | doclink = DOCLINK + '/hostels.html#accommodation-section' |
---|
[6953] | 141 | |
---|
[6959] | 142 | # It's quite dangerous to remove entire hostels with its content (beds). |
---|
[6966] | 143 | # Thus, this remove method should be combined with an archiving function. |
---|
[7718] | 144 | @jsaction(_('Remove selected')) |
---|
[6959] | 145 | def delHostels(self, **data): |
---|
| 146 | form = self.request.form |
---|
[9701] | 147 | if 'val_id' in form: |
---|
[6959] | 148 | deleted = [] |
---|
| 149 | child_id = form['val_id'] |
---|
[6966] | 150 | if not isinstance(child_id, list): |
---|
| 151 | child_id = [child_id] |
---|
[6959] | 152 | for id in child_id: |
---|
| 153 | deleted.append(id) |
---|
[13170] | 154 | self.context.writeLogMessage( |
---|
| 155 | self, 'deleted: % s' % ', '.join(deleted)) |
---|
[6959] | 156 | delSubobjects(self, redirect='@@manage', tab='2') |
---|
| 157 | return |
---|
[6953] | 158 | |
---|
[7718] | 159 | @action(_('Add hostel'), validator=NullValidator) |
---|
[6959] | 160 | def addSubunit(self, **data): |
---|
| 161 | self.redirect(self.url(self.context, 'addhostel')) |
---|
| 162 | return |
---|
| 163 | |
---|
[11254] | 164 | @jsaction(_('Clear all hostels'), style='danger') |
---|
[9197] | 165 | def clearHostels(self, **data): |
---|
| 166 | self.context.clearAllHostels() |
---|
| 167 | self.flash(_('All hostels cleared.')) |
---|
[13166] | 168 | self.context.writeLogMessage(self, 'all hostels cleared') |
---|
[11254] | 169 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[9197] | 170 | return |
---|
| 171 | |
---|
[8685] | 172 | @action(_('Save'), style='primary') |
---|
| 173 | def save(self, **data): |
---|
[13445] | 174 | if data['accommodation_session'] != self.context.accommodation_session: |
---|
| 175 | catalog = getUtility(ICatalog, name='beds_catalog') |
---|
| 176 | beds = catalog.searchResults(bed_type=(None,None)) |
---|
| 177 | if len(beds): |
---|
| 178 | self.flash(_('You can\'t change the booking session ' |
---|
| 179 | 'before clearing all hostels.'), |
---|
| 180 | type='warning') |
---|
| 181 | return |
---|
[8685] | 182 | self.applyData(self.context, **data) |
---|
| 183 | self.flash(_('Settings have been saved.')) |
---|
| 184 | return |
---|
| 185 | |
---|
[13319] | 186 | class ReleaseExpiredAllocationsPage(UtilityView, grok.View): |
---|
| 187 | """Release all expired allocated beds. |
---|
| 188 | """ |
---|
| 189 | grok.context(IHostelsContainer) |
---|
| 190 | grok.name('releaseexpired') |
---|
[13320] | 191 | grok.require('waeup.manageHostels') |
---|
[13319] | 192 | |
---|
| 193 | def update(self, n=7): |
---|
| 194 | released = self.context.releaseExpiredAllocations(n) |
---|
| 195 | if len(released): |
---|
| 196 | message = ', '.join(released) |
---|
| 197 | self.context.writeLogMessage(self, 'released: %s' % message) |
---|
| 198 | flash_msg = _('Successfully released beds: ${a}', mapping = {'a':message}) |
---|
| 199 | else: |
---|
| 200 | flash_msg = _('No bed released.') |
---|
| 201 | self.flash(flash_msg, type='success') |
---|
| 202 | self.redirect(self.url(self.context)) |
---|
| 203 | return |
---|
| 204 | |
---|
| 205 | def render(self): |
---|
| 206 | return |
---|
| 207 | |
---|
[13484] | 208 | class HostelsStatisticsPage(KofaDisplayFormPage): |
---|
| 209 | """Some statistics about beds in hostels. |
---|
| 210 | """ |
---|
| 211 | grok.context(IHostelsContainer) |
---|
| 212 | grok.name('statistics') |
---|
| 213 | grok.require('waeup.manageHostels') |
---|
| 214 | grok.template('containerstatistics') |
---|
| 215 | label = _('Bed Statistics') |
---|
| 216 | |
---|
[7819] | 217 | class HostelAddFormPage(KofaAddFormPage): |
---|
[6953] | 218 | """Add-form to add a hostel. |
---|
| 219 | """ |
---|
| 220 | grok.context(IHostelsContainer) |
---|
| 221 | grok.require('waeup.manageHostels') |
---|
| 222 | grok.name('addhostel') |
---|
[6970] | 223 | #grok.template('hosteladdpage') |
---|
[10683] | 224 | form_fields = grok.AutoFields(IHostel).omit('beds_reserved', 'hostel_id') |
---|
[7718] | 225 | label = _('Add hostel') |
---|
[6953] | 226 | pnav = 5 |
---|
[13177] | 227 | doclink = DOCLINK + '/hostels.html#accommodation-section' |
---|
[6953] | 228 | |
---|
[7718] | 229 | @action(_('Create hostel')) |
---|
[6953] | 230 | def addHostel(self, **data): |
---|
[6954] | 231 | hostel = Hostel() |
---|
| 232 | self.applyData(hostel, **data) |
---|
[13426] | 233 | hostel.hostel_id = data['hostel_name'].lower().replace( |
---|
| 234 | ' ','-').replace('_','-').replace('.','') |
---|
[6966] | 235 | try: |
---|
| 236 | self.context.addHostel(hostel) |
---|
| 237 | except KeyError: |
---|
[11254] | 238 | self.flash(_('The hostel already exists.'), type='warning') |
---|
[6966] | 239 | return |
---|
[7718] | 240 | self.flash(_('Hostel created.')) |
---|
[13166] | 241 | self.context.writeLogMessage(self, 'added: % s' % data['hostel_name']) |
---|
[6953] | 242 | self.redirect(self.url(self.context[hostel.hostel_id], 'index')) |
---|
| 243 | return |
---|
| 244 | |
---|
[7819] | 245 | class HostelDisplayFormPage(KofaDisplayFormPage): |
---|
[6953] | 246 | """ Page to display hostel data |
---|
| 247 | """ |
---|
| 248 | grok.context(IHostel) |
---|
| 249 | grok.name('index') |
---|
| 250 | grok.require('waeup.viewHostels') |
---|
[9534] | 251 | grok.template('hostelpage') |
---|
| 252 | form_fields = grok.AutoFields(IHostel).omit('beds_reserved') |
---|
[6953] | 253 | pnav = 5 |
---|
| 254 | |
---|
[6956] | 255 | @property |
---|
| 256 | def label(self): |
---|
| 257 | return self.context.hostel_name |
---|
[6953] | 258 | |
---|
| 259 | class HostelManageActionButton(ManageActionButton): |
---|
| 260 | grok.order(1) |
---|
| 261 | grok.context(IHostel) |
---|
| 262 | grok.view(HostelDisplayFormPage) |
---|
| 263 | grok.require('waeup.manageHostels') |
---|
[7718] | 264 | text = _('Manage') |
---|
[6970] | 265 | target = 'manage' |
---|
[6953] | 266 | |
---|
[7819] | 267 | class HostelManageFormPage(KofaEditFormPage): |
---|
[6953] | 268 | """ View to edit hostel data |
---|
| 269 | """ |
---|
| 270 | grok.context(IHostel) |
---|
[6970] | 271 | grok.name('manage') |
---|
[6953] | 272 | grok.require('waeup.manageHostels') |
---|
[9534] | 273 | form_fields = grok.AutoFields(IHostel).omit('hostel_id', 'beds_reserved') |
---|
[6970] | 274 | grok.template('hostelmanagepage') |
---|
[7718] | 275 | label = _('Manage hostel') |
---|
[6953] | 276 | pnav = 5 |
---|
[7718] | 277 | taboneactions = [_('Save')] |
---|
| 278 | tabtwoactions = [_('Update all beds'), |
---|
| 279 | _('Switch reservation of selected beds'), |
---|
[9197] | 280 | _('Release selected beds'), |
---|
| 281 | _('Clear hostel')] |
---|
[6996] | 282 | not_occupied = NOT_OCCUPIED |
---|
[13177] | 283 | doclink = DOCLINK + '/hostels.html#browser-pages' |
---|
[6953] | 284 | |
---|
[6956] | 285 | @property |
---|
[6996] | 286 | def students_url(self): |
---|
| 287 | return self.url(grok.getSite(),'students') |
---|
| 288 | |
---|
[11254] | 289 | @action(_('Save'), style='primary') |
---|
[6953] | 290 | def save(self, **data): |
---|
| 291 | msave(self, **data) |
---|
| 292 | return |
---|
[6970] | 293 | |
---|
[13346] | 294 | @action(_('Update all beds'), style='primary', |
---|
| 295 | warning=_('Attention: The updater removes all reservation flags of existing beds.' |
---|
| 296 | ' You really want to update?')) |
---|
[6970] | 297 | def updateBeds(self, **data): |
---|
[6988] | 298 | removed, added, modified, modified_beds = self.context.updateBeds() |
---|
| 299 | message = '%d empty beds removed, %d beds added, %d occupied beds modified (%s)' % ( |
---|
| 300 | removed, added, modified, modified_beds) |
---|
[13494] | 301 | if modified > 50: |
---|
| 302 | flash_message = _( |
---|
| 303 | '${a} empty beds removed, ${b} beds added, ' |
---|
| 304 | + '${c} occupied beds modified', |
---|
| 305 | mapping = {'a':removed, 'b':added, 'c':modified}) |
---|
| 306 | else: |
---|
| 307 | flash_message = _( |
---|
| 308 | '${a} empty beds removed, ${b} beds added, ' |
---|
| 309 | + '${c} occupied beds modified (${d})', |
---|
| 310 | mapping = {'a':removed, 'b':added, 'c':modified, 'd':modified_beds}) |
---|
[7718] | 311 | self.flash(flash_message) |
---|
[13166] | 312 | self.context.writeLogMessage(self, message) |
---|
[11254] | 313 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[6970] | 314 | return |
---|
[6973] | 315 | |
---|
[7718] | 316 | @action(_('Switch reservation of selected beds')) |
---|
[6974] | 317 | def switchReservations(self, **data): |
---|
[6973] | 318 | form = self.request.form |
---|
[9701] | 319 | if 'val_id' in form: |
---|
[6973] | 320 | child_id = form['val_id'] |
---|
| 321 | else: |
---|
[11254] | 322 | self.flash(_('No item selected.'), type='warning') |
---|
| 323 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[6973] | 324 | return |
---|
| 325 | if not isinstance(child_id, list): |
---|
| 326 | child_id = [child_id] |
---|
[7718] | 327 | switched = [] # for log file |
---|
| 328 | switched_translated = [] # for flash message |
---|
[7833] | 329 | # Here we know that the cookie has been set |
---|
| 330 | preferred_language = self.request.cookies.get('kofa.language') |
---|
[6973] | 331 | for bed_id in child_id: |
---|
[7718] | 332 | message = self.context[bed_id].switchReservation() |
---|
| 333 | switched.append('%s (%s)' % (bed_id,message)) |
---|
[7811] | 334 | m_translated = translate(message, 'waeup.kofa', |
---|
[7718] | 335 | target_language=preferred_language) |
---|
| 336 | switched_translated.append('%s (%s)' % (bed_id,m_translated)) |
---|
[6973] | 337 | if len(switched): |
---|
[6988] | 338 | message = ', '.join(switched) |
---|
[7718] | 339 | m_translated = ', '.join(switched_translated) |
---|
[13476] | 340 | if len(switched) > 50: |
---|
| 341 | self.flash(_('Successfully switched ${a} beds.', |
---|
| 342 | mapping = {'a':len(switched)})) |
---|
| 343 | else: |
---|
| 344 | self.flash(_('Successfully switched beds: ${a}', |
---|
| 345 | mapping = {'a':m_translated})) |
---|
[13166] | 346 | self.context.writeLogMessage(self, 'switched: %s' % message) |
---|
[11254] | 347 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[6973] | 348 | return |
---|
| 349 | |
---|
[7718] | 350 | @action(_('Release selected beds')) |
---|
[7042] | 351 | def releaseBeds(self, **data): |
---|
| 352 | form = self.request.form |
---|
[9701] | 353 | if 'val_id' in form: |
---|
[7042] | 354 | child_id = form['val_id'] |
---|
| 355 | else: |
---|
[11254] | 356 | self.flash(_('No item selected.'), type='warning') |
---|
| 357 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[7042] | 358 | return |
---|
| 359 | if not isinstance(child_id, list): |
---|
| 360 | child_id = [child_id] |
---|
| 361 | released = [] |
---|
| 362 | for bed_id in child_id: |
---|
[7068] | 363 | message = self.context[bed_id].releaseBed() |
---|
[7070] | 364 | if message: |
---|
| 365 | released.append('%s (%s)' % (bed_id,message)) |
---|
[7042] | 366 | if len(released): |
---|
| 367 | message = ', '.join(released) |
---|
[7718] | 368 | self.flash(_('Successfully released beds: ${a}', |
---|
| 369 | mapping = {'a':message})) |
---|
[13166] | 370 | self.context.writeLogMessage(self, 'released: %s' % message) |
---|
[11254] | 371 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[7070] | 372 | else: |
---|
[11254] | 373 | self.flash(_('No allocated bed selected.'), type='warning') |
---|
| 374 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[7042] | 375 | return |
---|
[7068] | 376 | |
---|
[11254] | 377 | @jsaction(_('Clear hostel'), style='danger') |
---|
[9197] | 378 | def clearHostel(self, **data): |
---|
| 379 | self.context.clearHostel() |
---|
| 380 | self.flash(_('Hostel cleared.')) |
---|
[13166] | 381 | self.context.writeLogMessage(self, 'cleared') |
---|
[11254] | 382 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
[9197] | 383 | return |
---|
| 384 | |
---|
[7819] | 385 | class BedManageFormPage(KofaEditFormPage): |
---|
[7068] | 386 | """ View to edit bed data |
---|
| 387 | """ |
---|
[9414] | 388 | grok.context(IBed) |
---|
[7068] | 389 | grok.name('index') |
---|
| 390 | grok.require('waeup.manageHostels') |
---|
[9414] | 391 | form_fields = grok.AutoFields(IBed).omit( |
---|
[9199] | 392 | 'bed_id', 'bed_number', 'bed_type') |
---|
[7718] | 393 | label = _('Allocate student') |
---|
[7068] | 394 | pnav = 5 |
---|
[13177] | 395 | doclink = DOCLINK + '/hostels.html#manage-bed' |
---|
[7068] | 396 | |
---|
[7718] | 397 | @action(_('Save')) |
---|
[7068] | 398 | def save(self, **data): |
---|
[9416] | 399 | if data['owner'] == NOT_OCCUPIED: |
---|
[11254] | 400 | self.flash(_('No valid student id.'), type='warning') |
---|
[9416] | 401 | self.redirect(self.url(self.context)) |
---|
| 402 | return |
---|
[7068] | 403 | msave(self, **data) |
---|
[11254] | 404 | self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2') |
---|
[7068] | 405 | return |
---|
| 406 | |
---|
| 407 | def update(self): |
---|
| 408 | if self.context.owner != NOT_OCCUPIED: |
---|
| 409 | # Don't use this form for exchanging students. |
---|
| 410 | # Beds must be released first before they can be allocated to |
---|
| 411 | # other students. |
---|
[11254] | 412 | self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2') |
---|
[7811] | 413 | return |
---|