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