[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). |
---|
[6966] | 118 | # Thus, this remove method should be combined with an archiving function. |
---|
[6959] | 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'] |
---|
[6966] | 125 | if not isinstance(child_id, list): |
---|
| 126 | child_id = [child_id] |
---|
[6959] | 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 |
---|
[6953] | 132 | |
---|
[6959] | 133 | @grok.action('Add hostel', validator=NullValidator) |
---|
| 134 | def addSubunit(self, **data): |
---|
| 135 | self.redirect(self.url(self.context, 'addhostel')) |
---|
| 136 | return |
---|
| 137 | |
---|
[6953] | 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') |
---|
[6970] | 144 | #grok.template('hosteladdpage') |
---|
[6953] | 145 | form_fields = grok.AutoFields(IHostel) |
---|
[6959] | 146 | title = 'Hostels' |
---|
[6953] | 147 | label = 'Add hostel' |
---|
| 148 | pnav = 5 |
---|
| 149 | |
---|
| 150 | @grok.action('Create hostel') |
---|
| 151 | def addHostel(self, **data): |
---|
[6954] | 152 | hostel = Hostel() |
---|
| 153 | self.applyData(hostel, **data) |
---|
[6956] | 154 | hostel.hostel_id = data['hostel_name'].lower().replace(' ','_') |
---|
[6966] | 155 | try: |
---|
| 156 | self.context.addHostel(hostel) |
---|
| 157 | except KeyError: |
---|
| 158 | self.flash('The hostel already exists.') |
---|
| 159 | return |
---|
[6953] | 160 | self.flash('Hostel created.') |
---|
[6959] | 161 | write_log_message(self, 'added: % s' % data['hostel_name']) |
---|
[6953] | 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 | |
---|
[6956] | 174 | @property |
---|
| 175 | def label(self): |
---|
| 176 | return self.context.hostel_name |
---|
[6953] | 177 | |
---|
[6956] | 178 | @property |
---|
| 179 | def title(self): |
---|
| 180 | return self.context.hostel_name |
---|
[6953] | 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' |
---|
[6970] | 188 | target = 'manage' |
---|
[6953] | 189 | |
---|
| 190 | class HostelManageFormPage(WAeUPEditFormPage): |
---|
| 191 | """ View to edit hostel data |
---|
| 192 | """ |
---|
| 193 | grok.context(IHostel) |
---|
[6970] | 194 | grok.name('manage') |
---|
[6953] | 195 | grok.require('waeup.manageHostels') |
---|
| 196 | form_fields = grok.AutoFields(IHostel).omit('hostel_id') |
---|
[6970] | 197 | grok.template('hostelmanagepage') |
---|
[6959] | 198 | label = 'Manage hostel' |
---|
[6953] | 199 | pnav = 5 |
---|
[6970] | 200 | taboneactions = ['Save'] |
---|
| 201 | tabtwoactions = ['Update beds'] |
---|
[6953] | 202 | |
---|
[6956] | 203 | @property |
---|
| 204 | def title(self): |
---|
| 205 | return self.context.hostel_name |
---|
[6953] | 206 | |
---|
| 207 | def update(self): |
---|
| 208 | datepicker.need() # Enable jQuery datepicker in date fields. |
---|
[6970] | 209 | tabs.need() |
---|
| 210 | datatable.need() |
---|
[6953] | 211 | super(HostelManageFormPage, self).update() |
---|
| 212 | return |
---|
| 213 | |
---|
| 214 | @grok.action('Save') |
---|
| 215 | def save(self, **data): |
---|
| 216 | msave(self, **data) |
---|
| 217 | return |
---|
[6970] | 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 |
---|