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 | """ |
---|
18 | import grok |
---|
19 | from time import time |
---|
20 | from datetime import date, datetime |
---|
21 | from zope.component import createObject |
---|
22 | from waeup.sirp.browser import ( |
---|
23 | WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, WAeUPDisplayFormPage, |
---|
24 | NullValidator) |
---|
25 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
26 | from waeup.sirp.browser.resources import datepicker, datatable, tabs, toggleall |
---|
27 | from waeup.sirp.browser.viewlets import ( |
---|
28 | ManageActionButton, PrimaryNavTab, AddActionButton) |
---|
29 | from waeup.sirp.interfaces import IWAeUPObject, IUserAccount |
---|
30 | from waeup.sirp.widgets.datewidget import ( |
---|
31 | FriendlyDateWidget, FriendlyDateDisplayWidget, |
---|
32 | FriendlyDatetimeDisplayWidget) |
---|
33 | from waeup.sirp.browser.pages import delSubobjects |
---|
34 | from waeup.sirp.authentication import get_principal_role_manager |
---|
35 | from waeup.sirp.hostels.container import HostelsContainer |
---|
36 | from waeup.sirp.hostels.hostel import Hostel |
---|
37 | from waeup.sirp.hostels.interfaces import IHostelsContainer, IHostel |
---|
38 | |
---|
39 | def 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 |
---|
45 | def 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 | |
---|
58 | class 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 | |
---|
74 | class HostelsBreadcrumb(Breadcrumb): |
---|
75 | """A breadcrumb for the hostels container. |
---|
76 | """ |
---|
77 | grok.context(IHostelsContainer) |
---|
78 | title = u'Hostels' |
---|
79 | |
---|
80 | class 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 | |
---|
88 | class 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 | |
---|
99 | class 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 | |
---|
106 | class 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 remove method 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 | if not isinstance(child_id, list): |
---|
126 | child_id = [child_id] |
---|
127 | for id in child_id: |
---|
128 | deleted.append(id) |
---|
129 | write_log_message(self, 'deleted: % s' % ', '.join(deleted)) |
---|
130 | delSubobjects(self, redirect='@@manage', tab='2') |
---|
131 | return |
---|
132 | |
---|
133 | @grok.action('Add hostel', validator=NullValidator) |
---|
134 | def addSubunit(self, **data): |
---|
135 | self.redirect(self.url(self.context, 'addhostel')) |
---|
136 | return |
---|
137 | |
---|
138 | class HostelAddFormPage(WAeUPAddFormPage): |
---|
139 | """Add-form to add a hostel. |
---|
140 | """ |
---|
141 | grok.context(IHostelsContainer) |
---|
142 | grok.require('waeup.manageHostels') |
---|
143 | grok.name('addhostel') |
---|
144 | #grok.template('hosteladdpage') |
---|
145 | form_fields = grok.AutoFields(IHostel) |
---|
146 | title = 'Hostels' |
---|
147 | label = 'Add hostel' |
---|
148 | pnav = 5 |
---|
149 | |
---|
150 | @grok.action('Create hostel') |
---|
151 | def addHostel(self, **data): |
---|
152 | hostel = Hostel() |
---|
153 | self.applyData(hostel, **data) |
---|
154 | hostel.hostel_id = data['hostel_name'].lower().replace(' ','_') |
---|
155 | try: |
---|
156 | self.context.addHostel(hostel) |
---|
157 | except KeyError: |
---|
158 | self.flash('The hostel already exists.') |
---|
159 | return |
---|
160 | self.flash('Hostel created.') |
---|
161 | write_log_message(self, 'added: % s' % data['hostel_name']) |
---|
162 | self.redirect(self.url(self.context[hostel.hostel_id], 'index')) |
---|
163 | return |
---|
164 | |
---|
165 | class HostelDisplayFormPage(WAeUPDisplayFormPage): |
---|
166 | """ Page to display hostel data |
---|
167 | """ |
---|
168 | grok.context(IHostel) |
---|
169 | grok.name('index') |
---|
170 | grok.require('waeup.viewHostels') |
---|
171 | #grok.template('hostelpage') |
---|
172 | pnav = 5 |
---|
173 | |
---|
174 | @property |
---|
175 | def label(self): |
---|
176 | return self.context.hostel_name |
---|
177 | |
---|
178 | @property |
---|
179 | def title(self): |
---|
180 | return self.context.hostel_name |
---|
181 | |
---|
182 | class HostelManageActionButton(ManageActionButton): |
---|
183 | grok.order(1) |
---|
184 | grok.context(IHostel) |
---|
185 | grok.view(HostelDisplayFormPage) |
---|
186 | grok.require('waeup.manageHostels') |
---|
187 | text = 'Manage' |
---|
188 | target = 'manage' |
---|
189 | |
---|
190 | class HostelManageFormPage(WAeUPEditFormPage): |
---|
191 | """ View to edit hostel data |
---|
192 | """ |
---|
193 | grok.context(IHostel) |
---|
194 | grok.name('manage') |
---|
195 | grok.require('waeup.manageHostels') |
---|
196 | form_fields = grok.AutoFields(IHostel).omit('hostel_id') |
---|
197 | grok.template('hostelmanagepage') |
---|
198 | label = 'Manage hostel' |
---|
199 | pnav = 5 |
---|
200 | taboneactions = ['Save'] |
---|
201 | tabtwoactions = ['Update beds'] |
---|
202 | |
---|
203 | @property |
---|
204 | def title(self): |
---|
205 | return self.context.hostel_name |
---|
206 | |
---|
207 | def update(self): |
---|
208 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
209 | tabs.need() |
---|
210 | datatable.need() |
---|
211 | super(HostelManageFormPage, self).update() |
---|
212 | return |
---|
213 | |
---|
214 | @grok.action('Save') |
---|
215 | def save(self, **data): |
---|
216 | msave(self, **data) |
---|
217 | return |
---|
218 | |
---|
219 | @grok.action('Update beds') |
---|
220 | def updateBeds(self, **data): |
---|
221 | added, modified = self.context.updateBeds() |
---|
222 | self.flash('%d added, %d modified' % (added, modified)) |
---|
223 | self.redirect(self.url(self.context, '@@manage')+'#tab-2') |
---|
224 | return |
---|