source: main/waeup.kofa/trunk/src/waeup/kofa/hostels/browser.py @ 12012

Last change on this file since 12012 was 11254, checked in by uli, 11 years ago

Merge changes from uli-diazo-themed back into trunk. If this works, then a miracle happened.

  • Property svn:keywords set to Id
File size: 12.3 KB
RevLine 
[7195]1## $Id: browser.py 11254 2014-02-22 15:46:03Z uli $
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"""
20import grok
[6973]21import sys
[7718]22from zope.i18n import translate
23from zope.component import getUtility
[9217]24from waeup.kofa.browser.layout import (
[7819]25    KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage,
[6959]26    NullValidator)
[7811]27from waeup.kofa.browser.breadcrumbs import Breadcrumb
28from waeup.kofa.browser.layout import default_primary_nav_template
29from waeup.kofa.browser.pages import delSubobjects
30from waeup.kofa.browser.viewlets import (
[7257]31    ManageActionButton, PrimaryNavTab)
[7811]32from waeup.kofa.browser.layout import jsaction, action
[7819]33from waeup.kofa.interfaces import IKofaObject, IKofaUtils
[7811]34from waeup.kofa.interfaces import MessageFactory as _
35from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED
36from waeup.kofa.hostels.hostel import Hostel
37from waeup.kofa.hostels.interfaces import (
[9414]38    IHostelsContainer, IHostel, IBed)
[8685]39from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget
[6953]40
[6956]41def 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
47def 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]59class 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')
[10771]66    grok.name('hostelstab')
[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
75class HostelsBreadcrumb(Breadcrumb):
76    """A breadcrumb for the hostels container.
77    """
78    grok.context(IHostelsContainer)
[7718]79    title = _(u'Hostels')
[6953]80
81class 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]89class 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]99class 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
114class 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]121class 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
[6959]136    # It's quite dangerous to remove entire hostels with its content (beds).
[6966]137    # Thus, this remove method should be combined with an archiving function.
[7718]138    @jsaction(_('Remove selected'))
[6959]139    def delHostels(self, **data):
140        form = self.request.form
[9701]141        if 'val_id' in form:
[6959]142            deleted = []
143            child_id = form['val_id']
[6966]144            if not isinstance(child_id, list):
145                child_id = [child_id]
[6959]146            for id in child_id:
147                deleted.append(id)
148            write_log_message(self, 'deleted: % s' % ', '.join(deleted))
149        delSubobjects(self, redirect='@@manage', tab='2')
150        return
[6953]151
[7718]152    @action(_('Add hostel'), validator=NullValidator)
[6959]153    def addSubunit(self, **data):
154        self.redirect(self.url(self.context, 'addhostel'))
155        return
156
[11254]157    @jsaction(_('Clear all hostels'), style='danger')
[9197]158    def clearHostels(self, **data):
159        self.context.clearAllHostels()
160        self.flash(_('All hostels cleared.'))
161        write_log_message(self, 'all hostels cleared')
[11254]162        self.redirect(self.url(self.context, '@@manage')+'#tab2')
[9197]163        return
164
[8685]165    @action(_('Save'), style='primary')
166    def save(self, **data):
167        self.applyData(self.context, **data)
168        self.flash(_('Settings have been saved.'))
169        return
170
[7819]171class HostelAddFormPage(KofaAddFormPage):
[6953]172    """Add-form to add a hostel.
173    """
174    grok.context(IHostelsContainer)
175    grok.require('waeup.manageHostels')
176    grok.name('addhostel')
[6970]177    #grok.template('hosteladdpage')
[10683]178    form_fields = grok.AutoFields(IHostel).omit('beds_reserved', 'hostel_id')
[7718]179    label = _('Add hostel')
[6953]180    pnav = 5
181
[7718]182    @action(_('Create hostel'))
[6953]183    def addHostel(self, **data):
[6954]184        hostel = Hostel()
185        self.applyData(hostel, **data)
[6973]186        hostel.hostel_id = data[
187            'hostel_name'].lower().replace(' ','-').replace('_','-')
[6966]188        try:
189            self.context.addHostel(hostel)
190        except KeyError:
[11254]191            self.flash(_('The hostel already exists.'), type='warning')
[6966]192            return
[7718]193        self.flash(_('Hostel created.'))
[6959]194        write_log_message(self, 'added: % s' % data['hostel_name'])
[6953]195        self.redirect(self.url(self.context[hostel.hostel_id], 'index'))
196        return
197
[7819]198class HostelDisplayFormPage(KofaDisplayFormPage):
[6953]199    """ Page to display hostel data
200    """
201    grok.context(IHostel)
202    grok.name('index')
203    grok.require('waeup.viewHostels')
[9534]204    grok.template('hostelpage')
205    form_fields = grok.AutoFields(IHostel).omit('beds_reserved')
[6953]206    pnav = 5
207
[6956]208    @property
209    def label(self):
210        return self.context.hostel_name
[6953]211
212class HostelManageActionButton(ManageActionButton):
213    grok.order(1)
214    grok.context(IHostel)
215    grok.view(HostelDisplayFormPage)
216    grok.require('waeup.manageHostels')
[7718]217    text = _('Manage')
[6970]218    target = 'manage'
[6953]219
[7819]220class HostelManageFormPage(KofaEditFormPage):
[6953]221    """ View to edit hostel data
222    """
223    grok.context(IHostel)
[6970]224    grok.name('manage')
[6953]225    grok.require('waeup.manageHostels')
[9534]226    form_fields = grok.AutoFields(IHostel).omit('hostel_id', 'beds_reserved')
[6970]227    grok.template('hostelmanagepage')
[7718]228    label = _('Manage hostel')
[6953]229    pnav = 5
[7718]230    taboneactions = [_('Save')]
231    tabtwoactions = [_('Update all beds'),
232        _('Switch reservation of selected beds'),
[9197]233        _('Release selected beds'),
234        _('Clear hostel')]
[6996]235    not_occupied = NOT_OCCUPIED
[6953]236
[6956]237    @property
[6996]238    def students_url(self):
239        return self.url(grok.getSite(),'students')
240
[11254]241    @action(_('Save'), style='primary')
[6953]242    def save(self, **data):
243        msave(self, **data)
244        return
[6970]245
[11254]246    @action(_('Update all beds'), style='primary')
[6970]247    def updateBeds(self, **data):
[6988]248        removed, added, modified, modified_beds = self.context.updateBeds()
249        message = '%d empty beds removed, %d beds added, %d occupied beds modified (%s)' % (
250            removed, added, modified, modified_beds)
[7718]251        flash_message = _(
252            '${a} empty beds removed, ${b} beds added, '
253            + '${c} occupied beds modified (${d})',
254            mapping = {'a':removed, 'b':added, 'c':modified, 'd':modified_beds})
255        self.flash(flash_message)
[6988]256        write_log_message(self, message)
[11254]257        self.redirect(self.url(self.context, '@@manage')+'#tab2')
[6970]258        return
[6973]259
[7718]260    @action(_('Switch reservation of selected beds'))
[6974]261    def switchReservations(self, **data):
[6973]262        form = self.request.form
[9701]263        if 'val_id' in form:
[6973]264            child_id = form['val_id']
265        else:
[11254]266            self.flash(_('No item selected.'), type='warning')
267            self.redirect(self.url(self.context, '@@manage')+'#tab2')
[6973]268            return
269        if not isinstance(child_id, list):
270            child_id = [child_id]
[7718]271        switched = [] # for log file
272        switched_translated = [] # for flash message
[7833]273        # Here we know that the cookie has been set
274        preferred_language = self.request.cookies.get('kofa.language')
[6973]275        for bed_id in child_id:
[7718]276            message = self.context[bed_id].switchReservation()
277            switched.append('%s (%s)' % (bed_id,message))
[7811]278            m_translated = translate(message, 'waeup.kofa',
[7718]279                target_language=preferred_language)
280            switched_translated.append('%s (%s)' % (bed_id,m_translated))
[6973]281        if len(switched):
[6988]282            message = ', '.join(switched)
[7718]283            m_translated = ', '.join(switched_translated)
284            self.flash(_('Successfully switched beds: ${a}',
285                mapping = {'a':m_translated}))
[7042]286            write_log_message(self, 'switched: %s' % message)
[11254]287            self.redirect(self.url(self.context, '@@manage')+'#tab2')
[6973]288        return
289
[7718]290    @action(_('Release selected beds'))
[7042]291    def releaseBeds(self, **data):
292        form = self.request.form
[9701]293        if 'val_id' in form:
[7042]294            child_id = form['val_id']
295        else:
[11254]296            self.flash(_('No item selected.'), type='warning')
297            self.redirect(self.url(self.context, '@@manage')+'#tab2')
[7042]298            return
299        if not isinstance(child_id, list):
300            child_id = [child_id]
301        released = []
302        for bed_id in child_id:
[7068]303            message = self.context[bed_id].releaseBed()
[7070]304            if message:
305                released.append('%s (%s)' % (bed_id,message))
[7042]306        if len(released):
307            message = ', '.join(released)
[7718]308            self.flash(_('Successfully released beds: ${a}',
309                mapping = {'a':message}))
[7042]310            write_log_message(self, 'released: %s' % message)
[11254]311            self.redirect(self.url(self.context, '@@manage')+'#tab2')
[7070]312        else:
[11254]313            self.flash(_('No allocated bed selected.'), type='warning')
314            self.redirect(self.url(self.context, '@@manage')+'#tab2')
[7042]315        return
[7068]316
[11254]317    @jsaction(_('Clear hostel'), style='danger')
[9197]318    def clearHostel(self, **data):
319        self.context.clearHostel()
320        self.flash(_('Hostel cleared.'))
321        write_log_message(self, 'cleared')
[11254]322        self.redirect(self.url(self.context, '@@manage')+'#tab2')
[9197]323        return
324
[7819]325class BedManageFormPage(KofaEditFormPage):
[7068]326    """ View to edit bed data
327    """
[9414]328    grok.context(IBed)
[7068]329    grok.name('index')
330    grok.require('waeup.manageHostels')
[9414]331    form_fields = grok.AutoFields(IBed).omit(
[9199]332        'bed_id', 'bed_number', 'bed_type')
[7718]333    label = _('Allocate student')
[7068]334    pnav = 5
335
[7718]336    @action(_('Save'))
[7068]337    def save(self, **data):
[9416]338        if data['owner'] == NOT_OCCUPIED:
[11254]339            self.flash(_('No valid student id.'), type='warning')
[9416]340            self.redirect(self.url(self.context))
341            return
[7068]342        msave(self, **data)
[11254]343        self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2')
[7068]344        return
345
346    def update(self):
347        if self.context.owner != NOT_OCCUPIED:
348            # Don't use this form for exchanging students.
349            # Beds must be released first before they can be allocated to
350            # other students.
[11254]351            self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2')
[7811]352        return
Note: See TracBrowser for help on using the repository browser.