source: main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/hostels/browser.py @ 7038

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

Add pagetemplates and extend logging.

  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
2## This program is free software; you can redistribute it and/or modify
3## it under the terms of the GNU General Public License as published by
4## the Free Software Foundation; either version 2 of the License, or
5## (at your option) any later version.
6##
7## This program is distributed in the hope that it will be useful,
8## but WITHOUT ANY WARRANTY; without even the implied warranty of
9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10## GNU General Public License for more details.
11##
12## You should have received a copy of the GNU General Public License
13## along with this program; if not, write to the Free Software
14## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15##
16"""UI components for hostels and related components.
17"""
18import grok
19from time import time
20from datetime import date, datetime
21from zope.component import createObject
22from waeup.sirp.browser import (
23    WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, WAeUPDisplayFormPage,
24    NullValidator)
25from waeup.sirp.browser.breadcrumbs import Breadcrumb
26from waeup.sirp.browser.resources import datepicker, datatable, tabs, toggleall
27from waeup.sirp.browser.viewlets import (
28    ManageActionButton, PrimaryNavTab, AddActionButton)
29from waeup.sirp.interfaces import IWAeUPObject, IUserAccount
30from waeup.sirp.widgets.datewidget import (
31    FriendlyDateWidget, FriendlyDateDisplayWidget,
32    FriendlyDatetimeDisplayWidget)
33from waeup.sirp.browser.pages import delSubobjects
34from waeup.sirp.authentication import get_principal_role_manager
35from waeup.sirp.hostels.container import HostelsContainer
36from waeup.sirp.hostels.hostel import Hostel
37from waeup.sirp.hostels.interfaces import IHostelsContainer, IHostel
38
39def write_log_message(view, message):
40    ob_class = view.__implemented__.__name__.replace('waeup.sirp.','')
41    view.context.loggerInfo(ob_class, message)
42    return
43
44# Save function used for save methods in manager pages
45def msave(view, **data):
46    form = view.request.form
47    changed_fields = view.applyData(view.context, **data)
48    # Turn list of lists into single list
49    if changed_fields:
50        changed_fields = reduce(lambda x,y: x+y, changed_fields.values())
51    fields_string = ' + '.join(changed_fields)
52    view.context._p_changed = True
53    view.flash('Form has been saved.')
54    if fields_string:
55        write_log_message(view, 'saved: % s' % fields_string)
56    return
57
58class HostelsTab(PrimaryNavTab):
59    """Hostels tab in primary navigation.
60    """
61
62    grok.context(IWAeUPObject)
63    grok.order(5)
64    grok.require('waeup.viewHostels')
65    grok.template('primarynavtab')
66
67    pnav = 5
68    tab_title = u'Hostels'
69
70    @property
71    def link_target(self):
72        return self.view.application_url('hostels')
73
74class HostelsBreadcrumb(Breadcrumb):
75    """A breadcrumb for the hostels container.
76    """
77    grok.context(IHostelsContainer)
78    title = u'Hostels'
79
80class HostelBreadcrumb(Breadcrumb):
81    """A breadcrumb for the hostel container.
82    """
83    grok.context(IHostel)
84
85    def title(self):
86        return self.context.hostel_name
87
88class HostelsContainerPage(WAeUPDisplayFormPage):
89    """The standard view for hostels containers.
90    """
91    grok.context(IHostelsContainer)
92    grok.name('index')
93    grok.require('waeup.viewHostels')
94    grok.template('containerpage')
95    label = 'Accommodation Section'
96    title = 'Hostels'
97    pnav = 5
98
99class HostelsContainerManageActionButton(ManageActionButton):
100    grok.order(1)
101    grok.context(IHostelsContainer)
102    grok.view(HostelsContainerPage)
103    grok.require('waeup.manageHostels')
104    text = 'Manage accommodation section'
105
106class HostelsContainerManagePage(WAeUPDisplayFormPage):
107    """The manage page for hostel containers.
108    """
109    grok.context(IHostelsContainer)
110    grok.name('manage')
111    grok.require('waeup.manageHostels')
112    grok.template('containermanagepage')
113    pnav = 5
114    title = 'Hostels'
115    label = 'Manage accommodation section'
116
117    # It's quite dangerous to remove entire hostels with its content (beds).
118    # Thus, this removed should be combined with an archiving function.
119    @grok.action('Remove selected')
120    def delHostels(self, **data):
121        form = self.request.form
122        if form.has_key('val_id'):
123            deleted = []
124            child_id = form['val_id']
125            child_id = [child_id]
126            for id in child_id:
127                deleted.append(id)
128            write_log_message(self, 'deleted: % s' % ', '.join(deleted))
129        delSubobjects(self, redirect='@@manage', tab='2')
130        return
131
132    @grok.action('Add hostel', validator=NullValidator)
133    def addSubunit(self, **data):
134        self.redirect(self.url(self.context, 'addhostel'))
135        return
136
137class HostelAddFormPage(WAeUPAddFormPage):
138    """Add-form to add a hostel.
139    """
140    grok.context(IHostelsContainer)
141    grok.require('waeup.manageHostels')
142    grok.name('addhostel')
143    grok.template('hosteladdpage')
144    form_fields = grok.AutoFields(IHostel)
145    title = 'Hostels'
146    label = 'Add hostel'
147    pnav = 5
148
149    @grok.action('Create hostel')
150    def addHostel(self, **data):
151        hostel = Hostel()
152        self.applyData(hostel, **data)
153        hostel.hostel_id = data['hostel_name'].lower().replace(' ','_')
154        self.context.addHostel(hostel)
155        self.flash('Hostel created.')
156        write_log_message(self, 'added: % s' % data['hostel_name'])
157        self.redirect(self.url(self.context[hostel.hostel_id], 'index'))
158        return
159
160class HostelDisplayFormPage(WAeUPDisplayFormPage):
161    """ Page to display hostel data
162    """
163    grok.context(IHostel)
164    grok.name('index')
165    grok.require('waeup.viewHostels')
166    #grok.template('hostelpage')
167    pnav = 5
168
169    @property
170    def label(self):
171        return self.context.hostel_name
172
173    @property
174    def title(self):
175        return self.context.hostel_name
176
177class HostelManageActionButton(ManageActionButton):
178    grok.order(1)
179    grok.context(IHostel)
180    grok.view(HostelDisplayFormPage)
181    grok.require('waeup.manageHostels')
182    text = 'Manage'
183    target = 'edit'
184
185class HostelManageFormPage(WAeUPEditFormPage):
186    """ View to edit hostel data
187    """
188    grok.context(IHostel)
189    grok.name('edit')
190    grok.require('waeup.manageHostels')
191    form_fields = grok.AutoFields(IHostel).omit('hostel_id')
192    #grok.template('hostelmanagepage')
193    label = 'Manage hostel'
194    pnav = 5
195
196    @property
197    def title(self):
198        return self.context.hostel_name
199
200    def update(self):
201        datepicker.need() # Enable jQuery datepicker in date fields.
202        super(HostelManageFormPage, self).update()
203        return
204
205    @grok.action('Save')
206    def save(self, **data):
207        msave(self, **data)
208        return
Note: See TracBrowser for help on using the repository browser.