source: main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser.py @ 7536

Last change on this file since 7536 was 7484, checked in by Henrik Bettermann, 13 years ago

Redirect to specified tabs using the request's QUERY_STRING value. This suboptimal solution will be replaced later by a pure Javascript solution.

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