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