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

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

Fix permission.

  • Property svn:keywords set to Id
File size: 13.3 KB
Line 
1## $Id: browser.py 13320 2015-10-14 17:24:33Z 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, UtilityView)
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 ReleaseExpiredAllocationsPage(UtilityView, grok.View):
169    """Release all expired allocated beds.
170    """
171    grok.context(IHostelsContainer)
172    grok.name('releaseexpired')
173    grok.require('waeup.manageHostels')
174
175    def update(self, n=7):
176        released = self.context.releaseExpiredAllocations(n)
177        if len(released):
178            message = ', '.join(released)
179            self.context.writeLogMessage(self, 'released: %s' % message)
180            flash_msg = _('Successfully released beds: ${a}', mapping = {'a':message})
181        else:
182            flash_msg = _('No bed released.')
183        self.flash(flash_msg, type='success')
184        self.redirect(self.url(self.context))
185        return
186
187    def render(self):
188        return
189
190class HostelAddFormPage(KofaAddFormPage):
191    """Add-form to add a hostel.
192    """
193    grok.context(IHostelsContainer)
194    grok.require('waeup.manageHostels')
195    grok.name('addhostel')
196    #grok.template('hosteladdpage')
197    form_fields = grok.AutoFields(IHostel).omit('beds_reserved', 'hostel_id')
198    label = _('Add hostel')
199    pnav = 5
200    doclink = DOCLINK + '/hostels.html#accommodation-section'
201
202    @action(_('Create hostel'))
203    def addHostel(self, **data):
204        hostel = Hostel()
205        self.applyData(hostel, **data)
206        hostel.hostel_id = data[
207            'hostel_name'].lower().replace(' ','-').replace('_','-')
208        try:
209            self.context.addHostel(hostel)
210        except KeyError:
211            self.flash(_('The hostel already exists.'), type='warning')
212            return
213        self.flash(_('Hostel created.'))
214        self.context.writeLogMessage(self, 'added: % s' % data['hostel_name'])
215        self.redirect(self.url(self.context[hostel.hostel_id], 'index'))
216        return
217
218class HostelDisplayFormPage(KofaDisplayFormPage):
219    """ Page to display hostel data
220    """
221    grok.context(IHostel)
222    grok.name('index')
223    grok.require('waeup.viewHostels')
224    grok.template('hostelpage')
225    form_fields = grok.AutoFields(IHostel).omit('beds_reserved')
226    pnav = 5
227
228    @property
229    def label(self):
230        return self.context.hostel_name
231
232class HostelManageActionButton(ManageActionButton):
233    grok.order(1)
234    grok.context(IHostel)
235    grok.view(HostelDisplayFormPage)
236    grok.require('waeup.manageHostels')
237    text = _('Manage')
238    target = 'manage'
239
240class HostelManageFormPage(KofaEditFormPage):
241    """ View to edit hostel data
242    """
243    grok.context(IHostel)
244    grok.name('manage')
245    grok.require('waeup.manageHostels')
246    form_fields = grok.AutoFields(IHostel).omit('hostel_id', 'beds_reserved')
247    grok.template('hostelmanagepage')
248    label = _('Manage hostel')
249    pnav = 5
250    taboneactions = [_('Save')]
251    tabtwoactions = [_('Update all beds'),
252        _('Switch reservation of selected beds'),
253        _('Release selected beds'),
254        _('Clear hostel')]
255    not_occupied = NOT_OCCUPIED
256    doclink = DOCLINK + '/hostels.html#browser-pages'
257
258    @property
259    def students_url(self):
260        return self.url(grok.getSite(),'students')
261
262    @action(_('Save'), style='primary')
263    def save(self, **data):
264        msave(self, **data)
265        return
266
267    @action(_('Update all beds'), style='primary')
268    def updateBeds(self, **data):
269        removed, added, modified, modified_beds = self.context.updateBeds()
270        message = '%d empty beds removed, %d beds added, %d occupied beds modified (%s)' % (
271            removed, added, modified, modified_beds)
272        flash_message = _(
273            '${a} empty beds removed, ${b} beds added, '
274            + '${c} occupied beds modified (${d})',
275            mapping = {'a':removed, 'b':added, 'c':modified, 'd':modified_beds})
276        self.flash(flash_message)
277        self.context.writeLogMessage(self, message)
278        self.redirect(self.url(self.context, '@@manage')+'#tab2')
279        return
280
281    @action(_('Switch reservation of selected beds'))
282    def switchReservations(self, **data):
283        form = self.request.form
284        if 'val_id' in form:
285            child_id = form['val_id']
286        else:
287            self.flash(_('No item selected.'), type='warning')
288            self.redirect(self.url(self.context, '@@manage')+'#tab2')
289            return
290        if not isinstance(child_id, list):
291            child_id = [child_id]
292        switched = [] # for log file
293        switched_translated = [] # for flash message
294        # Here we know that the cookie has been set
295        preferred_language = self.request.cookies.get('kofa.language')
296        for bed_id in child_id:
297            message = self.context[bed_id].switchReservation()
298            switched.append('%s (%s)' % (bed_id,message))
299            m_translated = translate(message, 'waeup.kofa',
300                target_language=preferred_language)
301            switched_translated.append('%s (%s)' % (bed_id,m_translated))
302        if len(switched):
303            message = ', '.join(switched)
304            m_translated = ', '.join(switched_translated)
305            self.flash(_('Successfully switched beds: ${a}',
306                mapping = {'a':m_translated}))
307            self.context.writeLogMessage(self, 'switched: %s' % message)
308            self.redirect(self.url(self.context, '@@manage')+'#tab2')
309        return
310
311    @action(_('Release selected beds'))
312    def releaseBeds(self, **data):
313        form = self.request.form
314        if 'val_id' in form:
315            child_id = form['val_id']
316        else:
317            self.flash(_('No item selected.'), type='warning')
318            self.redirect(self.url(self.context, '@@manage')+'#tab2')
319            return
320        if not isinstance(child_id, list):
321            child_id = [child_id]
322        released = []
323        for bed_id in child_id:
324            message = self.context[bed_id].releaseBed()
325            if message:
326                released.append('%s (%s)' % (bed_id,message))
327        if len(released):
328            message = ', '.join(released)
329            self.flash(_('Successfully released beds: ${a}',
330                mapping = {'a':message}))
331            self.context.writeLogMessage(self, 'released: %s' % message)
332            self.redirect(self.url(self.context, '@@manage')+'#tab2')
333        else:
334            self.flash(_('No allocated bed selected.'), type='warning')
335            self.redirect(self.url(self.context, '@@manage')+'#tab2')
336        return
337
338    @jsaction(_('Clear hostel'), style='danger')
339    def clearHostel(self, **data):
340        self.context.clearHostel()
341        self.flash(_('Hostel cleared.'))
342        self.context.writeLogMessage(self, 'cleared')
343        self.redirect(self.url(self.context, '@@manage')+'#tab2')
344        return
345
346class BedManageFormPage(KofaEditFormPage):
347    """ View to edit bed data
348    """
349    grok.context(IBed)
350    grok.name('index')
351    grok.require('waeup.manageHostels')
352    form_fields = grok.AutoFields(IBed).omit(
353        'bed_id', 'bed_number', 'bed_type')
354    label = _('Allocate student')
355    pnav = 5
356    doclink = DOCLINK + '/hostels.html#manage-bed'
357
358    @action(_('Save'))
359    def save(self, **data):
360        if data['owner'] == NOT_OCCUPIED:
361            self.flash(_('No valid student id.'), type='warning')
362            self.redirect(self.url(self.context))
363            return
364        msave(self, **data)
365        self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2')
366        return
367
368    def update(self):
369        if self.context.owner != NOT_OCCUPIED:
370            # Don't use this form for exchanging students.
371            # Beds must be released first before they can be allocated to
372            # other students.
373            self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2')
374        return
Note: See TracBrowser for help on using the repository browser.