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

Last change on this file since 13315 was 13177, checked in by Henrik Bettermann, 9 years ago

Add doclinks.

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