[6953] | 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 ( |
---|
[6959] | 23 | WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, WAeUPDisplayFormPage, |
---|
| 24 | NullValidator) |
---|
[6953] | 25 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
[6959] | 26 | from waeup.sirp.browser.resources import datepicker, datatable, tabs, toggleall |
---|
[6953] | 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) |
---|
[6959] | 33 | from waeup.sirp.browser.pages import delSubobjects |
---|
[6953] | 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 | |
---|
[6956] | 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 |
---|
[6953] | 43 | |
---|
[6956] | 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 | |
---|
[6953] | 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 | |
---|
[6959] | 85 | def title(self): |
---|
| 86 | return self.context.hostel_name |
---|
| 87 | |
---|
[6953] | 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') |
---|
[6959] | 94 | grok.template('containerpage') |
---|
[6953] | 95 | label = 'Accommodation Section' |
---|
[6959] | 96 | title = 'Hostels' |
---|
[6953] | 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') |
---|
[6959] | 112 | grok.template('containermanagepage') |
---|
[6953] | 113 | pnav = 5 |
---|
[6959] | 114 | title = 'Hostels' |
---|
[6953] | 115 | label = 'Manage accommodation section' |
---|
| 116 | |
---|
[6959] | 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 |
---|
[6953] | 131 | |
---|
[6959] | 132 | @grok.action('Add hostel', validator=NullValidator) |
---|
| 133 | def addSubunit(self, **data): |
---|
| 134 | self.redirect(self.url(self.context, 'addhostel')) |
---|
| 135 | return |
---|
| 136 | |
---|
[6953] | 137 | class 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) |
---|
[6959] | 145 | title = 'Hostels' |
---|
[6953] | 146 | label = 'Add hostel' |
---|
| 147 | pnav = 5 |
---|
| 148 | |
---|
| 149 | @grok.action('Create hostel') |
---|
| 150 | def addHostel(self, **data): |
---|
[6954] | 151 | hostel = Hostel() |
---|
| 152 | self.applyData(hostel, **data) |
---|
[6956] | 153 | hostel.hostel_id = data['hostel_name'].lower().replace(' ','_') |
---|
[6953] | 154 | self.context.addHostel(hostel) |
---|
| 155 | self.flash('Hostel created.') |
---|
[6959] | 156 | write_log_message(self, 'added: % s' % data['hostel_name']) |
---|
[6953] | 157 | self.redirect(self.url(self.context[hostel.hostel_id], 'index')) |
---|
| 158 | return |
---|
| 159 | |
---|
| 160 | class 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 | |
---|
[6956] | 169 | @property |
---|
| 170 | def label(self): |
---|
| 171 | return self.context.hostel_name |
---|
[6953] | 172 | |
---|
[6956] | 173 | @property |
---|
| 174 | def title(self): |
---|
| 175 | return self.context.hostel_name |
---|
[6953] | 176 | |
---|
| 177 | class 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 | |
---|
| 185 | class 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') |
---|
[6959] | 193 | label = 'Manage hostel' |
---|
[6953] | 194 | pnav = 5 |
---|
| 195 | |
---|
[6956] | 196 | @property |
---|
| 197 | def title(self): |
---|
| 198 | return self.context.hostel_name |
---|
[6953] | 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 |
---|