1 | ## $Id: browser.py 13166 2015-07-13 08:20:23Z 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 | """ |
---|
20 | import grok |
---|
21 | import sys |
---|
22 | from zope.i18n import translate |
---|
23 | from zope.component import getUtility |
---|
24 | from waeup.kofa.browser.layout import ( |
---|
25 | KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage, |
---|
26 | NullValidator) |
---|
27 | from waeup.kofa.browser.breadcrumbs import Breadcrumb |
---|
28 | from waeup.kofa.browser.layout import default_primary_nav_template |
---|
29 | from waeup.kofa.browser.pages import delSubobjects |
---|
30 | from waeup.kofa.browser.viewlets import ( |
---|
31 | ManageActionButton, PrimaryNavTab) |
---|
32 | from waeup.kofa.browser.layout import jsaction, action |
---|
33 | from waeup.kofa.interfaces import IKofaObject, IKofaUtils |
---|
34 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
35 | from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED |
---|
36 | from waeup.kofa.hostels.hostel import Hostel |
---|
37 | from waeup.kofa.hostels.interfaces import ( |
---|
38 | IHostelsContainer, IHostel, IBed) |
---|
39 | from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget |
---|
40 | |
---|
41 | # Save function used for save methods in manager pages |
---|
42 | def 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 | |
---|
54 | class 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 | |
---|
70 | class HostelsBreadcrumb(Breadcrumb): |
---|
71 | """A breadcrumb for the hostels container. |
---|
72 | """ |
---|
73 | grok.context(IHostelsContainer) |
---|
74 | title = _(u'Hostels') |
---|
75 | |
---|
76 | class 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 | |
---|
84 | class 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 | |
---|
94 | class 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 | |
---|
109 | class 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 | |
---|
116 | class 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 | |
---|
131 | # It's quite dangerous to remove entire hostels with its content (beds). |
---|
132 | # Thus, this remove method should be combined with an archiving function. |
---|
133 | @jsaction(_('Remove selected')) |
---|
134 | def delHostels(self, **data): |
---|
135 | form = self.request.form |
---|
136 | if 'val_id' in form: |
---|
137 | deleted = [] |
---|
138 | child_id = form['val_id'] |
---|
139 | if not isinstance(child_id, list): |
---|
140 | child_id = [child_id] |
---|
141 | for id in child_id: |
---|
142 | deleted.append(id) |
---|
143 | self.context.writeLogMessage(self, 'deleted: % s' % ', '.join(deleted)) |
---|
144 | delSubobjects(self, redirect='@@manage', tab='2') |
---|
145 | return |
---|
146 | |
---|
147 | @action(_('Add hostel'), validator=NullValidator) |
---|
148 | def addSubunit(self, **data): |
---|
149 | self.redirect(self.url(self.context, 'addhostel')) |
---|
150 | return |
---|
151 | |
---|
152 | @jsaction(_('Clear all hostels'), style='danger') |
---|
153 | def clearHostels(self, **data): |
---|
154 | self.context.clearAllHostels() |
---|
155 | self.flash(_('All hostels cleared.')) |
---|
156 | self.context.writeLogMessage(self, 'all hostels cleared') |
---|
157 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
158 | return |
---|
159 | |
---|
160 | @action(_('Save'), style='primary') |
---|
161 | def save(self, **data): |
---|
162 | self.applyData(self.context, **data) |
---|
163 | self.flash(_('Settings have been saved.')) |
---|
164 | return |
---|
165 | |
---|
166 | class HostelAddFormPage(KofaAddFormPage): |
---|
167 | """Add-form to add a hostel. |
---|
168 | """ |
---|
169 | grok.context(IHostelsContainer) |
---|
170 | grok.require('waeup.manageHostels') |
---|
171 | grok.name('addhostel') |
---|
172 | #grok.template('hosteladdpage') |
---|
173 | form_fields = grok.AutoFields(IHostel).omit('beds_reserved', 'hostel_id') |
---|
174 | label = _('Add hostel') |
---|
175 | pnav = 5 |
---|
176 | |
---|
177 | @action(_('Create hostel')) |
---|
178 | def addHostel(self, **data): |
---|
179 | hostel = Hostel() |
---|
180 | self.applyData(hostel, **data) |
---|
181 | hostel.hostel_id = data[ |
---|
182 | 'hostel_name'].lower().replace(' ','-').replace('_','-') |
---|
183 | try: |
---|
184 | self.context.addHostel(hostel) |
---|
185 | except KeyError: |
---|
186 | self.flash(_('The hostel already exists.'), type='warning') |
---|
187 | return |
---|
188 | self.flash(_('Hostel created.')) |
---|
189 | self.context.writeLogMessage(self, 'added: % s' % data['hostel_name']) |
---|
190 | self.redirect(self.url(self.context[hostel.hostel_id], 'index')) |
---|
191 | return |
---|
192 | |
---|
193 | class HostelDisplayFormPage(KofaDisplayFormPage): |
---|
194 | """ Page to display hostel data |
---|
195 | """ |
---|
196 | grok.context(IHostel) |
---|
197 | grok.name('index') |
---|
198 | grok.require('waeup.viewHostels') |
---|
199 | grok.template('hostelpage') |
---|
200 | form_fields = grok.AutoFields(IHostel).omit('beds_reserved') |
---|
201 | pnav = 5 |
---|
202 | |
---|
203 | @property |
---|
204 | def label(self): |
---|
205 | return self.context.hostel_name |
---|
206 | |
---|
207 | class HostelManageActionButton(ManageActionButton): |
---|
208 | grok.order(1) |
---|
209 | grok.context(IHostel) |
---|
210 | grok.view(HostelDisplayFormPage) |
---|
211 | grok.require('waeup.manageHostels') |
---|
212 | text = _('Manage') |
---|
213 | target = 'manage' |
---|
214 | |
---|
215 | class HostelManageFormPage(KofaEditFormPage): |
---|
216 | """ View to edit hostel data |
---|
217 | """ |
---|
218 | grok.context(IHostel) |
---|
219 | grok.name('manage') |
---|
220 | grok.require('waeup.manageHostels') |
---|
221 | form_fields = grok.AutoFields(IHostel).omit('hostel_id', 'beds_reserved') |
---|
222 | grok.template('hostelmanagepage') |
---|
223 | label = _('Manage hostel') |
---|
224 | pnav = 5 |
---|
225 | taboneactions = [_('Save')] |
---|
226 | tabtwoactions = [_('Update all beds'), |
---|
227 | _('Switch reservation of selected beds'), |
---|
228 | _('Release selected beds'), |
---|
229 | _('Clear hostel')] |
---|
230 | not_occupied = NOT_OCCUPIED |
---|
231 | |
---|
232 | @property |
---|
233 | def students_url(self): |
---|
234 | return self.url(grok.getSite(),'students') |
---|
235 | |
---|
236 | @action(_('Save'), style='primary') |
---|
237 | def save(self, **data): |
---|
238 | msave(self, **data) |
---|
239 | return |
---|
240 | |
---|
241 | @action(_('Update all beds'), style='primary') |
---|
242 | def updateBeds(self, **data): |
---|
243 | removed, added, modified, modified_beds = self.context.updateBeds() |
---|
244 | message = '%d empty beds removed, %d beds added, %d occupied beds modified (%s)' % ( |
---|
245 | removed, added, modified, modified_beds) |
---|
246 | flash_message = _( |
---|
247 | '${a} empty beds removed, ${b} beds added, ' |
---|
248 | + '${c} occupied beds modified (${d})', |
---|
249 | mapping = {'a':removed, 'b':added, 'c':modified, 'd':modified_beds}) |
---|
250 | self.flash(flash_message) |
---|
251 | self.context.writeLogMessage(self, message) |
---|
252 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
253 | return |
---|
254 | |
---|
255 | @action(_('Switch reservation of selected beds')) |
---|
256 | def switchReservations(self, **data): |
---|
257 | form = self.request.form |
---|
258 | if 'val_id' in form: |
---|
259 | child_id = form['val_id'] |
---|
260 | else: |
---|
261 | self.flash(_('No item selected.'), type='warning') |
---|
262 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
263 | return |
---|
264 | if not isinstance(child_id, list): |
---|
265 | child_id = [child_id] |
---|
266 | switched = [] # for log file |
---|
267 | switched_translated = [] # for flash message |
---|
268 | # Here we know that the cookie has been set |
---|
269 | preferred_language = self.request.cookies.get('kofa.language') |
---|
270 | for bed_id in child_id: |
---|
271 | message = self.context[bed_id].switchReservation() |
---|
272 | switched.append('%s (%s)' % (bed_id,message)) |
---|
273 | m_translated = translate(message, 'waeup.kofa', |
---|
274 | target_language=preferred_language) |
---|
275 | switched_translated.append('%s (%s)' % (bed_id,m_translated)) |
---|
276 | if len(switched): |
---|
277 | message = ', '.join(switched) |
---|
278 | m_translated = ', '.join(switched_translated) |
---|
279 | self.flash(_('Successfully switched beds: ${a}', |
---|
280 | mapping = {'a':m_translated})) |
---|
281 | self.context.writeLogMessage(self, 'switched: %s' % message) |
---|
282 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
283 | return |
---|
284 | |
---|
285 | @action(_('Release selected beds')) |
---|
286 | def releaseBeds(self, **data): |
---|
287 | form = self.request.form |
---|
288 | if 'val_id' in form: |
---|
289 | child_id = form['val_id'] |
---|
290 | else: |
---|
291 | self.flash(_('No item selected.'), type='warning') |
---|
292 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
293 | return |
---|
294 | if not isinstance(child_id, list): |
---|
295 | child_id = [child_id] |
---|
296 | released = [] |
---|
297 | for bed_id in child_id: |
---|
298 | message = self.context[bed_id].releaseBed() |
---|
299 | if message: |
---|
300 | released.append('%s (%s)' % (bed_id,message)) |
---|
301 | if len(released): |
---|
302 | message = ', '.join(released) |
---|
303 | self.flash(_('Successfully released beds: ${a}', |
---|
304 | mapping = {'a':message})) |
---|
305 | self.context.writeLogMessage(self, 'released: %s' % message) |
---|
306 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
307 | else: |
---|
308 | self.flash(_('No allocated bed selected.'), type='warning') |
---|
309 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
310 | return |
---|
311 | |
---|
312 | @jsaction(_('Clear hostel'), style='danger') |
---|
313 | def clearHostel(self, **data): |
---|
314 | self.context.clearHostel() |
---|
315 | self.flash(_('Hostel cleared.')) |
---|
316 | self.context.writeLogMessage(self, 'cleared') |
---|
317 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
318 | return |
---|
319 | |
---|
320 | class BedManageFormPage(KofaEditFormPage): |
---|
321 | """ View to edit bed data |
---|
322 | """ |
---|
323 | grok.context(IBed) |
---|
324 | grok.name('index') |
---|
325 | grok.require('waeup.manageHostels') |
---|
326 | form_fields = grok.AutoFields(IBed).omit( |
---|
327 | 'bed_id', 'bed_number', 'bed_type') |
---|
328 | label = _('Allocate student') |
---|
329 | pnav = 5 |
---|
330 | |
---|
331 | @action(_('Save')) |
---|
332 | def save(self, **data): |
---|
333 | if data['owner'] == NOT_OCCUPIED: |
---|
334 | self.flash(_('No valid student id.'), type='warning') |
---|
335 | self.redirect(self.url(self.context)) |
---|
336 | return |
---|
337 | msave(self, **data) |
---|
338 | self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2') |
---|
339 | return |
---|
340 | |
---|
341 | def update(self): |
---|
342 | if self.context.owner != NOT_OCCUPIED: |
---|
343 | # Don't use this form for exchanging students. |
---|
344 | # Beds must be released first before they can be allocated to |
---|
345 | # other students. |
---|
346 | self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2') |
---|
347 | return |
---|