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

Last change on this file since 13167 was 13166, checked in by Henrik Bettermann, 10 years ago

Adjust logging methods in the various section.

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