1 | ## $Id: browser.py 13529 2015-12-02 20:15:36Z 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 zope.catalog.interfaces import ICatalog |
---|
25 | from waeup.kofa.browser.layout import ( |
---|
26 | KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage, |
---|
27 | NullValidator, UtilityView) |
---|
28 | from waeup.kofa.browser.breadcrumbs import Breadcrumb |
---|
29 | from waeup.kofa.browser.layout import default_primary_nav_template |
---|
30 | from waeup.kofa.browser.pages import delSubobjects |
---|
31 | from waeup.kofa.browser.viewlets import ( |
---|
32 | ManageActionButton, PrimaryNavTab) |
---|
33 | from waeup.kofa.browser.layout import jsaction, action |
---|
34 | from waeup.kofa.interfaces import IKofaObject, IKofaUtils, DOCLINK |
---|
35 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
36 | from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED |
---|
37 | from waeup.kofa.hostels.hostel import Hostel |
---|
38 | from waeup.kofa.hostels.interfaces import ( |
---|
39 | IHostelsContainer, IHostel, IBed) |
---|
40 | from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget |
---|
41 | |
---|
42 | # Save function used for save methods in manager pages |
---|
43 | def msave(view, **data): |
---|
44 | changed_fields = view.applyData(view.context, **data) |
---|
45 | # Turn list of lists into single list |
---|
46 | if changed_fields: |
---|
47 | changed_fields = reduce(lambda x,y: x+y, changed_fields.values()) |
---|
48 | fields_string = ' + '.join(changed_fields) |
---|
49 | view.context._p_changed = True |
---|
50 | view.flash(_('Form has been saved.')) |
---|
51 | if fields_string: |
---|
52 | view.context.writeLogMessage(view, 'saved: %s' % fields_string) |
---|
53 | return |
---|
54 | |
---|
55 | class HostelsTab(PrimaryNavTab): |
---|
56 | """Hostels tab in primary navigation. |
---|
57 | """ |
---|
58 | |
---|
59 | grok.context(IKofaObject) |
---|
60 | grok.order(5) |
---|
61 | grok.require('waeup.viewHostels') |
---|
62 | grok.name('hostelstab') |
---|
63 | template = default_primary_nav_template |
---|
64 | pnav = 5 |
---|
65 | tab_title = _(u'Hostels') |
---|
66 | |
---|
67 | @property |
---|
68 | def link_target(self): |
---|
69 | return self.view.application_url('hostels') |
---|
70 | |
---|
71 | class HostelsBreadcrumb(Breadcrumb): |
---|
72 | """A breadcrumb for the hostels container. |
---|
73 | """ |
---|
74 | grok.context(IHostelsContainer) |
---|
75 | title = _(u'Hostels') |
---|
76 | |
---|
77 | class HostelBreadcrumb(Breadcrumb): |
---|
78 | """A breadcrumb for the hostel container. |
---|
79 | """ |
---|
80 | grok.context(IHostel) |
---|
81 | |
---|
82 | def title(self): |
---|
83 | return self.context.hostel_name |
---|
84 | |
---|
85 | class BedBreadcrumb(Breadcrumb): |
---|
86 | """A breadcrumb for the hostel container. |
---|
87 | """ |
---|
88 | grok.context(IBed) |
---|
89 | |
---|
90 | def title(self): |
---|
91 | co = self.context.coordinates |
---|
92 | return _('Block ${a}, Room ${b}, Bed ${c}', |
---|
93 | mapping = {'a':co[1], 'b':co[2], 'c':co[3]}) |
---|
94 | |
---|
95 | class HostelsContainerPage(KofaDisplayFormPage): |
---|
96 | """The standard view for hostels containers. |
---|
97 | """ |
---|
98 | grok.context(IHostelsContainer) |
---|
99 | grok.name('index') |
---|
100 | grok.require('waeup.viewHostels') |
---|
101 | grok.template('containerpage') |
---|
102 | label = _('Accommodation Section') |
---|
103 | pnav = 5 |
---|
104 | form_fields = grok.AutoFields(IHostelsContainer) |
---|
105 | form_fields[ |
---|
106 | 'startdate'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
107 | form_fields[ |
---|
108 | 'enddate'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
109 | |
---|
110 | class HostelsContainerManageActionButton(ManageActionButton): |
---|
111 | grok.order(1) |
---|
112 | grok.context(IHostelsContainer) |
---|
113 | grok.view(HostelsContainerPage) |
---|
114 | grok.require('waeup.manageHostels') |
---|
115 | text = _('Manage accommodation section') |
---|
116 | |
---|
117 | class HostelsStatisticsActionButton(ManageActionButton): |
---|
118 | grok.order(2) |
---|
119 | grok.context(IHostelsContainer) |
---|
120 | grok.view(HostelsContainerPage) |
---|
121 | grok.require('waeup.manageHostels') |
---|
122 | icon = 'actionicon_statistics.png' |
---|
123 | text = _('Bed statistics') |
---|
124 | target = 'statistics' |
---|
125 | |
---|
126 | class HostelsContainerManagePage(KofaEditFormPage): |
---|
127 | """The manage page for hostel containers. |
---|
128 | """ |
---|
129 | grok.context(IHostelsContainer) |
---|
130 | grok.name('manage') |
---|
131 | grok.require('waeup.manageHostels') |
---|
132 | grok.template('containermanagepage') |
---|
133 | pnav = 5 |
---|
134 | label = _('Manage accommodation section') |
---|
135 | form_fields = grok.AutoFields(IHostelsContainer) |
---|
136 | taboneactions = [_('Save')] |
---|
137 | tabtwoactions = [_('Add hostel'), |
---|
138 | _('Clear all hostels'), |
---|
139 | _('Remove selected')] |
---|
140 | doclink = DOCLINK + '/hostels.html#accommodation-section' |
---|
141 | |
---|
142 | # It's quite dangerous to remove entire hostels with its content (beds). |
---|
143 | # Thus, this remove method should be combined with an archiving function. |
---|
144 | @jsaction(_('Remove selected')) |
---|
145 | def delHostels(self, **data): |
---|
146 | form = self.request.form |
---|
147 | if 'val_id' in form: |
---|
148 | deleted = [] |
---|
149 | child_id = form['val_id'] |
---|
150 | if not isinstance(child_id, list): |
---|
151 | child_id = [child_id] |
---|
152 | for id in child_id: |
---|
153 | deleted.append(id) |
---|
154 | self.context.writeLogMessage( |
---|
155 | self, 'deleted: % s' % ', '.join(deleted)) |
---|
156 | delSubobjects(self, redirect='@@manage', tab='2') |
---|
157 | return |
---|
158 | |
---|
159 | @action(_('Add hostel'), validator=NullValidator) |
---|
160 | def addSubunit(self, **data): |
---|
161 | self.redirect(self.url(self.context, 'addhostel')) |
---|
162 | return |
---|
163 | |
---|
164 | @jsaction(_('Clear all hostels'), style='danger') |
---|
165 | def clearHostels(self, **data): |
---|
166 | self.context.clearAllHostels() |
---|
167 | self.flash(_('All hostels cleared.')) |
---|
168 | self.context.writeLogMessage(self, 'all hostels cleared') |
---|
169 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
170 | return |
---|
171 | |
---|
172 | @action(_('Save'), style='primary') |
---|
173 | def save(self, **data): |
---|
174 | if data['accommodation_session'] != self.context.accommodation_session: |
---|
175 | catalog = getUtility(ICatalog, name='beds_catalog') |
---|
176 | beds = catalog.searchResults(bed_type=(None,None)) |
---|
177 | if len(beds): |
---|
178 | self.flash(_('You can\'t change the booking session ' |
---|
179 | 'before clearing all hostels.'), |
---|
180 | type='warning') |
---|
181 | return |
---|
182 | self.applyData(self.context, **data) |
---|
183 | self.flash(_('Settings have been saved.')) |
---|
184 | return |
---|
185 | |
---|
186 | class ReleaseExpiredAllocationsPage(UtilityView, grok.View): |
---|
187 | """Release all expired allocated beds. |
---|
188 | """ |
---|
189 | grok.context(IHostelsContainer) |
---|
190 | grok.name('releaseexpired') |
---|
191 | grok.require('waeup.manageHostels') |
---|
192 | |
---|
193 | def update(self, n=7): |
---|
194 | if not grok.getSite()['configuration'].maintmode_enabled_by: |
---|
195 | self.flash( |
---|
196 | _('Portal must be in maintenance mode for releasing expired ' |
---|
197 | 'bed allocations.'), |
---|
198 | type='danger') |
---|
199 | self.redirect(self.url(self.context)) |
---|
200 | return |
---|
201 | released = self.context.releaseExpiredAllocations(n) |
---|
202 | if len(released): |
---|
203 | message = ', '.join(released) |
---|
204 | self.context.writeLogMessage(self, 'released: %s' % message) |
---|
205 | flash_msg = _('Successfully released beds: ${a}', |
---|
206 | mapping = {'a':message}) |
---|
207 | else: |
---|
208 | flash_msg = _('No bed released.') |
---|
209 | self.flash(flash_msg, type='success') |
---|
210 | self.redirect(self.url(self.context)) |
---|
211 | return |
---|
212 | |
---|
213 | def render(self): |
---|
214 | return |
---|
215 | |
---|
216 | class HostelsStatisticsPage(KofaDisplayFormPage): |
---|
217 | """Some statistics about beds in hostels. |
---|
218 | """ |
---|
219 | grok.context(IHostelsContainer) |
---|
220 | grok.name('statistics') |
---|
221 | grok.require('waeup.manageHostels') |
---|
222 | grok.template('containerstatistics') |
---|
223 | label = _('Bed Statistics') |
---|
224 | |
---|
225 | class HostelAddFormPage(KofaAddFormPage): |
---|
226 | """Add-form to add a hostel. |
---|
227 | """ |
---|
228 | grok.context(IHostelsContainer) |
---|
229 | grok.require('waeup.manageHostels') |
---|
230 | grok.name('addhostel') |
---|
231 | #grok.template('hosteladdpage') |
---|
232 | form_fields = grok.AutoFields(IHostel).omit('beds_reserved', 'hostel_id') |
---|
233 | label = _('Add hostel') |
---|
234 | pnav = 5 |
---|
235 | doclink = DOCLINK + '/hostels.html#accommodation-section' |
---|
236 | |
---|
237 | @action(_('Create hostel')) |
---|
238 | def addHostel(self, **data): |
---|
239 | hostel = Hostel() |
---|
240 | self.applyData(hostel, **data) |
---|
241 | hostel.hostel_id = data['hostel_name'].lower().replace( |
---|
242 | ' ','-').replace('_','-').replace('.','') |
---|
243 | try: |
---|
244 | self.context.addHostel(hostel) |
---|
245 | except KeyError: |
---|
246 | self.flash(_('The hostel already exists.'), type='warning') |
---|
247 | return |
---|
248 | self.flash(_('Hostel created.')) |
---|
249 | self.context.writeLogMessage(self, 'added: % s' % data['hostel_name']) |
---|
250 | self.redirect(self.url(self.context[hostel.hostel_id], 'index')) |
---|
251 | return |
---|
252 | |
---|
253 | class HostelDisplayFormPage(KofaDisplayFormPage): |
---|
254 | """ Page to display hostel data |
---|
255 | """ |
---|
256 | grok.context(IHostel) |
---|
257 | grok.name('index') |
---|
258 | grok.require('waeup.viewHostels') |
---|
259 | grok.template('hostelpage') |
---|
260 | form_fields = grok.AutoFields(IHostel).omit('beds_reserved') |
---|
261 | pnav = 5 |
---|
262 | |
---|
263 | @property |
---|
264 | def label(self): |
---|
265 | return self.context.hostel_name |
---|
266 | |
---|
267 | class HostelManageActionButton(ManageActionButton): |
---|
268 | grok.order(1) |
---|
269 | grok.context(IHostel) |
---|
270 | grok.view(HostelDisplayFormPage) |
---|
271 | grok.require('waeup.manageHostels') |
---|
272 | text = _('Manage') |
---|
273 | target = 'manage' |
---|
274 | |
---|
275 | class HostelManageFormPage(KofaEditFormPage): |
---|
276 | """ View to edit hostel data |
---|
277 | """ |
---|
278 | grok.context(IHostel) |
---|
279 | grok.name('manage') |
---|
280 | grok.require('waeup.manageHostels') |
---|
281 | form_fields = grok.AutoFields(IHostel).omit('hostel_id', 'beds_reserved') |
---|
282 | grok.template('hostelmanagepage') |
---|
283 | label = _('Manage hostel') |
---|
284 | pnav = 5 |
---|
285 | taboneactions = [_('Save')] |
---|
286 | tabtwoactions = [_('Update all beds'), |
---|
287 | _('Switch reservation of selected beds'), |
---|
288 | _('Release selected beds'), |
---|
289 | _('Clear hostel')] |
---|
290 | not_occupied = NOT_OCCUPIED |
---|
291 | doclink = DOCLINK + '/hostels.html#browser-pages' |
---|
292 | |
---|
293 | @property |
---|
294 | def students_url(self): |
---|
295 | return self.url(grok.getSite(),'students') |
---|
296 | |
---|
297 | @action(_('Save'), style='primary') |
---|
298 | def save(self, **data): |
---|
299 | msave(self, **data) |
---|
300 | return |
---|
301 | |
---|
302 | @action(_('Update all beds'), style='primary', |
---|
303 | warning=_('Attention: The updater removes all reservation flags of existing beds.' |
---|
304 | ' You really want to update?')) |
---|
305 | def updateBeds(self, **data): |
---|
306 | if not grok.getSite()['configuration'].maintmode_enabled_by: |
---|
307 | self.flash( |
---|
308 | _('Portal must be in maintenance mode for bed updates.'), |
---|
309 | type='danger') |
---|
310 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
311 | return |
---|
312 | removed, added, modified, modified_beds = self.context.updateBeds() |
---|
313 | message = '%d empty beds removed, %d beds added, %d occupied beds modified (%s)' % ( |
---|
314 | removed, added, modified, modified_beds) |
---|
315 | if modified > 50: |
---|
316 | flash_message = _( |
---|
317 | '${a} empty beds removed, ${b} beds added, ' |
---|
318 | + '${c} occupied beds modified', |
---|
319 | mapping = {'a':removed, 'b':added, 'c':modified}) |
---|
320 | else: |
---|
321 | flash_message = _( |
---|
322 | '${a} empty beds removed, ${b} beds added, ' |
---|
323 | + '${c} occupied beds modified (${d})', |
---|
324 | mapping = {'a':removed, 'b':added, 'c':modified, 'd':modified_beds}) |
---|
325 | self.flash(flash_message) |
---|
326 | self.context.writeLogMessage(self, message) |
---|
327 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
328 | return |
---|
329 | |
---|
330 | @action(_('Switch reservation of selected beds')) |
---|
331 | def switchReservations(self, **data): |
---|
332 | form = self.request.form |
---|
333 | if 'val_id' in form: |
---|
334 | child_id = form['val_id'] |
---|
335 | else: |
---|
336 | self.flash(_('No item selected.'), type='warning') |
---|
337 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
338 | return |
---|
339 | if not isinstance(child_id, list): |
---|
340 | child_id = [child_id] |
---|
341 | switched = [] # for log file |
---|
342 | switched_translated = [] # for flash message |
---|
343 | # Here we know that the cookie has been set |
---|
344 | preferred_language = self.request.cookies.get('kofa.language') |
---|
345 | for bed_id in child_id: |
---|
346 | message = self.context[bed_id].switchReservation() |
---|
347 | switched.append('%s (%s)' % (bed_id,message)) |
---|
348 | m_translated = translate(message, 'waeup.kofa', |
---|
349 | target_language=preferred_language) |
---|
350 | switched_translated.append('%s (%s)' % (bed_id,m_translated)) |
---|
351 | if len(switched): |
---|
352 | message = ', '.join(switched) |
---|
353 | m_translated = ', '.join(switched_translated) |
---|
354 | if len(switched) > 50: |
---|
355 | self.flash(_('Successfully switched ${a} beds.', |
---|
356 | mapping = {'a':len(switched)})) |
---|
357 | else: |
---|
358 | self.flash(_('Successfully switched beds: ${a}', |
---|
359 | mapping = {'a':m_translated})) |
---|
360 | self.context.writeLogMessage(self, 'switched: %s' % message) |
---|
361 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
362 | return |
---|
363 | |
---|
364 | @action(_('Release selected beds')) |
---|
365 | def releaseBeds(self, **data): |
---|
366 | form = self.request.form |
---|
367 | if 'val_id' in form: |
---|
368 | child_id = form['val_id'] |
---|
369 | else: |
---|
370 | self.flash(_('No item selected.'), type='warning') |
---|
371 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
372 | return |
---|
373 | if not isinstance(child_id, list): |
---|
374 | child_id = [child_id] |
---|
375 | released = [] |
---|
376 | for bed_id in child_id: |
---|
377 | message = self.context[bed_id].releaseBed() |
---|
378 | if message: |
---|
379 | released.append('%s (%s)' % (bed_id,message)) |
---|
380 | if len(released): |
---|
381 | message = ', '.join(released) |
---|
382 | self.flash(_('Successfully released beds: ${a}', |
---|
383 | mapping = {'a':message})) |
---|
384 | self.context.writeLogMessage(self, 'released: %s' % message) |
---|
385 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
386 | else: |
---|
387 | self.flash(_('No allocated bed selected.'), type='warning') |
---|
388 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
389 | return |
---|
390 | |
---|
391 | @jsaction(_('Clear hostel'), style='danger') |
---|
392 | def clearHostel(self, **data): |
---|
393 | self.context.clearHostel() |
---|
394 | self.flash(_('Hostel cleared.')) |
---|
395 | self.context.writeLogMessage(self, 'cleared') |
---|
396 | self.redirect(self.url(self.context, '@@manage')+'#tab2') |
---|
397 | return |
---|
398 | |
---|
399 | class BedManageFormPage(KofaEditFormPage): |
---|
400 | """ View to edit bed data |
---|
401 | """ |
---|
402 | grok.context(IBed) |
---|
403 | grok.name('index') |
---|
404 | grok.require('waeup.manageHostels') |
---|
405 | form_fields = grok.AutoFields(IBed).omit( |
---|
406 | 'bed_id', 'bed_number', 'bed_type') |
---|
407 | label = _('Allocate student') |
---|
408 | pnav = 5 |
---|
409 | doclink = DOCLINK + '/hostels.html#manage-bed' |
---|
410 | |
---|
411 | @action(_('Save')) |
---|
412 | def save(self, **data): |
---|
413 | if data['owner'] == NOT_OCCUPIED: |
---|
414 | self.flash(_('No valid student id.'), type='warning') |
---|
415 | self.redirect(self.url(self.context)) |
---|
416 | return |
---|
417 | msave(self, **data) |
---|
418 | self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2') |
---|
419 | return |
---|
420 | |
---|
421 | def update(self): |
---|
422 | if self.context.owner != NOT_OCCUPIED: |
---|
423 | # Don't use this form for exchanging students. |
---|
424 | # Beds must be released first before they can be allocated to |
---|
425 | # other students. |
---|
426 | self.redirect(self.url(self.context.__parent__, '@@manage')+'#tab2') |
---|
427 | return |
---|