[12015] | 1 | ## $Id: viewlets.py 12351 2014-12-31 12:53:13Z henrik $ |
---|
[11958] | 2 | ## |
---|
| 3 | ## Copyright (C) 2014 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 | |
---|
| 19 | import grok |
---|
[11967] | 20 | from zope.i18n import translate |
---|
| 21 | from zope.interface import Interface |
---|
[12346] | 22 | from zope.component import getUtility |
---|
[11958] | 23 | from waeup.ikoba.interfaces import IIkobaObject |
---|
| 24 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 25 | from waeup.ikoba.browser.viewlets import ( |
---|
| 26 | PrimaryNavTab, ManageActionButton, AddActionButton) |
---|
[11987] | 27 | from waeup.ikoba.browser.layout import default_primary_nav_template |
---|
[11967] | 28 | from waeup.ikoba.customers.interfaces import ( |
---|
[12015] | 29 | ICustomer, ICustomersContainer, |
---|
[12090] | 30 | ICustomerDocumentsContainer, ICustomerDocument, |
---|
[12346] | 31 | IContractsContainer, IContract, ICustomersUtils) |
---|
[11967] | 32 | from waeup.ikoba.customers.browser import ( |
---|
| 33 | CustomersContainerPage, CustomersContainerManagePage, |
---|
[12015] | 34 | CustomerBaseDisplayFormPage, |
---|
[12090] | 35 | DocumentsManageFormPage, DocumentDisplayFormPage, DocumentManageFormPage, |
---|
[12337] | 36 | ContractsFormPage, ContractDisplayFormPage, |
---|
[12097] | 37 | ContractManageFormPage) |
---|
[11958] | 38 | |
---|
[11985] | 39 | grok.context(IIkobaObject) # Make IIkobaObject the default context |
---|
[11964] | 40 | grok.templatedir('browser_templates') |
---|
| 41 | |
---|
[11985] | 42 | |
---|
[11958] | 43 | class CustomersTab(PrimaryNavTab): |
---|
| 44 | """Customers tab in primary navigation. |
---|
| 45 | """ |
---|
| 46 | |
---|
| 47 | grok.context(IIkobaObject) |
---|
| 48 | grok.order(4) |
---|
| 49 | grok.require('waeup.viewCustomersTab') |
---|
| 50 | grok.name('customerstab') |
---|
| 51 | |
---|
| 52 | pnav = 4 |
---|
| 53 | tab_title = _(u'Customers') |
---|
| 54 | |
---|
| 55 | @property |
---|
| 56 | def link_target(self): |
---|
[11964] | 57 | return self.view.application_url('customers') |
---|
| 58 | |
---|
[11985] | 59 | |
---|
[11964] | 60 | class PrimaryCustomerNavManager(grok.ViewletManager): |
---|
| 61 | """Viewlet manager for the primary navigation tab. |
---|
| 62 | """ |
---|
| 63 | grok.name('primary_nav_customer') |
---|
| 64 | |
---|
[11985] | 65 | |
---|
[11964] | 66 | class PrimaryCustomerNavTab(grok.Viewlet): |
---|
| 67 | """Base for primary customer nav tabs. |
---|
| 68 | """ |
---|
| 69 | grok.baseclass() |
---|
| 70 | grok.viewletmanager(PrimaryCustomerNavManager) |
---|
| 71 | template = default_primary_nav_template |
---|
| 72 | grok.order(1) |
---|
| 73 | grok.require('waeup.Authenticated') |
---|
| 74 | pnav = 0 |
---|
| 75 | tab_title = u'Some Text' |
---|
| 76 | |
---|
| 77 | @property |
---|
| 78 | def link_target(self): |
---|
| 79 | return self.view.application_url() |
---|
| 80 | |
---|
| 81 | @property |
---|
| 82 | def active(self): |
---|
| 83 | view_pnav = getattr(self.view, 'pnav', 0) |
---|
| 84 | if view_pnav == self.pnav: |
---|
| 85 | return 'active' |
---|
| 86 | return '' |
---|
| 87 | |
---|
[11985] | 88 | |
---|
[11964] | 89 | class MyCustomerDataTab(PrimaryCustomerNavTab): |
---|
| 90 | """MyData dropdown tab in primary navigation. |
---|
| 91 | """ |
---|
| 92 | grok.order(3) |
---|
| 93 | grok.require('waeup.viewMyCustomerDataTab') |
---|
| 94 | grok.template('mydatadropdowntabs') |
---|
| 95 | grok.name('mycustomerdatatab') |
---|
| 96 | pnav = 4 |
---|
| 97 | tab_title = _(u'My Data') |
---|
| 98 | |
---|
| 99 | @property |
---|
| 100 | def active(self): |
---|
| 101 | view_pnav = getattr(self.view, 'pnav', 0) |
---|
| 102 | if view_pnav == self.pnav: |
---|
| 103 | return 'active dropdown' |
---|
| 104 | return 'dropdown' |
---|
| 105 | |
---|
| 106 | @property |
---|
| 107 | def targets(self): |
---|
| 108 | customer = grok.getSite()['customers'][self.request.principal.id] |
---|
| 109 | customer_url = self.view.url(customer) |
---|
| 110 | targets = [] |
---|
| 111 | targets += [ |
---|
| 112 | {'url':customer_url, 'title':'Base Data'}, |
---|
[12097] | 113 | {'url':customer_url + '/contracts', 'title':_('My Contracts')}, |
---|
[12090] | 114 | {'url':customer_url + '/documents', 'title':_('My Documents')}, |
---|
[11964] | 115 | {'url':customer_url + '/history', 'title':_('History')}, |
---|
| 116 | ] |
---|
[11967] | 117 | return targets |
---|
| 118 | |
---|
[11985] | 119 | |
---|
[11967] | 120 | class CustomerManageSidebar(grok.ViewletManager): |
---|
| 121 | grok.name('left_customermanage') |
---|
| 122 | |
---|
[11985] | 123 | |
---|
[11967] | 124 | class CustomerManageLink(grok.Viewlet): |
---|
| 125 | """A link displayed in the customer box which shows up for CustomerNavigation |
---|
| 126 | objects. |
---|
| 127 | |
---|
| 128 | """ |
---|
| 129 | grok.baseclass() |
---|
| 130 | grok.viewletmanager(CustomerManageSidebar) |
---|
| 131 | grok.view(Interface) |
---|
| 132 | grok.order(5) |
---|
| 133 | grok.require('waeup.viewCustomer') |
---|
| 134 | |
---|
| 135 | link = 'index' |
---|
| 136 | text = _(u'Base Data') |
---|
| 137 | |
---|
| 138 | def render(self): |
---|
| 139 | url = self.view.url(self.context.customer, self.link) |
---|
| 140 | # Here we know that the cookie has been set |
---|
[11979] | 141 | lang = self.request.cookies.get('ikoba.language') |
---|
| 142 | text = translate(self.text, 'waeup.ikoba', |
---|
[11967] | 143 | target_language=lang) |
---|
| 144 | if not self.link: |
---|
| 145 | return '' |
---|
| 146 | return u'<li><a href="%s">%s</a></li>' % ( |
---|
| 147 | url, text) |
---|
| 148 | |
---|
[11985] | 149 | |
---|
[11967] | 150 | class CustomerManageBaseLink(CustomerManageLink): |
---|
| 151 | grok.order(2) |
---|
| 152 | link = 'index' |
---|
| 153 | text = _(u'Base Data') |
---|
| 154 | |
---|
[12090] | 155 | |
---|
[12097] | 156 | class CustomerManageContractsLink(CustomerManageLink): |
---|
[12090] | 157 | grok.order(3) |
---|
[12097] | 158 | link = 'contracts' |
---|
| 159 | text = _(u'Contracts') |
---|
[12090] | 160 | |
---|
| 161 | |
---|
[12015] | 162 | class CustomerManageDocumentsLink(CustomerManageLink): |
---|
[12090] | 163 | grok.order(4) |
---|
[12015] | 164 | link = 'documents' |
---|
| 165 | text = _(u'Documents') |
---|
[11985] | 166 | |
---|
[12015] | 167 | |
---|
[11967] | 168 | class CustomerManageHistoryLink(CustomerManageLink): |
---|
| 169 | grok.order(8) |
---|
| 170 | link = 'history' |
---|
| 171 | text = _(u'History') |
---|
| 172 | |
---|
[11985] | 173 | |
---|
[11967] | 174 | class CustomersContainerManageActionButton(ManageActionButton): |
---|
| 175 | grok.order(1) |
---|
| 176 | grok.context(ICustomersContainer) |
---|
| 177 | grok.view(CustomersContainerPage) |
---|
| 178 | grok.require('waeup.manageCustomer') |
---|
| 179 | text = _('Manage customer section') |
---|
| 180 | |
---|
[11985] | 181 | |
---|
[11967] | 182 | class CustomersContainerAddActionButton(AddActionButton): |
---|
| 183 | grok.order(1) |
---|
| 184 | grok.context(ICustomersContainer) |
---|
| 185 | grok.view(CustomersContainerManagePage) |
---|
| 186 | grok.require('waeup.manageCustomer') |
---|
| 187 | text = _('Add customer') |
---|
| 188 | target = 'addcustomer' |
---|
| 189 | |
---|
[11985] | 190 | |
---|
[11967] | 191 | class ContactActionButton(ManageActionButton): |
---|
| 192 | grok.order(5) |
---|
| 193 | grok.context(ICustomer) |
---|
| 194 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 195 | grok.require('waeup.manageCustomer') |
---|
| 196 | icon = 'actionicon_mail.png' |
---|
| 197 | text = _('Send email') |
---|
| 198 | target = 'contactcustomer' |
---|
| 199 | |
---|
[11985] | 200 | |
---|
[11967] | 201 | class CustomerBaseManageActionButton(ManageActionButton): |
---|
| 202 | grok.order(1) |
---|
| 203 | grok.context(ICustomer) |
---|
| 204 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 205 | grok.require('waeup.manageCustomer') |
---|
| 206 | text = _('Manage') |
---|
| 207 | target = 'manage_base' |
---|
| 208 | |
---|
[11985] | 209 | |
---|
[11967] | 210 | class CustomerTrigTransActionButton(ManageActionButton): |
---|
| 211 | grok.order(2) |
---|
| 212 | grok.context(ICustomer) |
---|
| 213 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 214 | grok.require('waeup.triggerTransition') |
---|
| 215 | icon = 'actionicon_trigtrans.png' |
---|
[12028] | 216 | text = _(u'Transition') |
---|
[11967] | 217 | target = 'trigtrans' |
---|
| 218 | |
---|
[11985] | 219 | |
---|
[11967] | 220 | class CustomerLoginAsActionButton(ManageActionButton): |
---|
| 221 | grok.order(3) |
---|
| 222 | grok.context(ICustomer) |
---|
| 223 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 224 | grok.require('waeup.loginAsCustomer') |
---|
| 225 | icon = 'actionicon_mask.png' |
---|
| 226 | text = _(u'Login as customer') |
---|
| 227 | target = 'loginasstep1' |
---|
| 228 | |
---|
[11985] | 229 | |
---|
[11967] | 230 | class CustomerDeactivateActionButton(ManageActionButton): |
---|
| 231 | grok.order(7) |
---|
| 232 | grok.context(ICustomer) |
---|
| 233 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 234 | grok.require('waeup.manageCustomer') |
---|
| 235 | text = _('Deactivate account') |
---|
| 236 | target = 'deactivate' |
---|
| 237 | icon = 'actionicon_traffic_lights_red.png' |
---|
| 238 | |
---|
| 239 | @property |
---|
| 240 | def target_url(self): |
---|
| 241 | if self.context.suspended: |
---|
| 242 | return '' |
---|
| 243 | return self.view.url(self.view.context, self.target) |
---|
| 244 | |
---|
| 245 | @property |
---|
| 246 | def onclick(self): |
---|
| 247 | return "return window.confirm(%s);" % _( |
---|
| 248 | "'A history message will be added. Are you sure?'") |
---|
| 249 | |
---|
[11985] | 250 | |
---|
[11967] | 251 | class CustomerActivateActionButton(ManageActionButton): |
---|
| 252 | grok.order(7) |
---|
| 253 | grok.context(ICustomer) |
---|
| 254 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 255 | grok.require('waeup.manageCustomer') |
---|
| 256 | text = _('Activate account') |
---|
| 257 | target = 'activate' |
---|
| 258 | icon = 'actionicon_traffic_lights_green.png' |
---|
| 259 | |
---|
| 260 | @property |
---|
| 261 | def target_url(self): |
---|
| 262 | if not self.context.suspended: |
---|
| 263 | return '' |
---|
| 264 | return self.view.url(self.view.context, self.target) |
---|
| 265 | |
---|
| 266 | @property |
---|
| 267 | def onclick(self): |
---|
| 268 | return "return window.confirm(%s);" % _( |
---|
[11977] | 269 | "'A history message will be added. Are you sure?'") |
---|
| 270 | |
---|
[11985] | 271 | |
---|
[11986] | 272 | class CustomerBaseActionButton(ManageActionButton): |
---|
| 273 | grok.order(1) |
---|
| 274 | grok.context(ICustomer) |
---|
| 275 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 276 | grok.require('waeup.handleCustomer') |
---|
| 277 | target = 'edit_base' |
---|
| 278 | |
---|
[12351] | 279 | @property |
---|
| 280 | def text(self): |
---|
| 281 | if self.view.is_requestable: |
---|
| 282 | return _('Edit and request registration') |
---|
| 283 | return _('Edit') |
---|
[12016] | 284 | |
---|
[12351] | 285 | |
---|
[11977] | 286 | class CustomerPasswordActionButton(ManageActionButton): |
---|
| 287 | grok.order(2) |
---|
| 288 | grok.context(ICustomer) |
---|
| 289 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 290 | grok.require('waeup.handleCustomer') |
---|
| 291 | icon = 'actionicon_key.png' |
---|
| 292 | text = _('Change password') |
---|
[11985] | 293 | target = 'changepassword' |
---|
[11986] | 294 | |
---|
[12016] | 295 | |
---|
[11986] | 296 | class CustomerPassportActionButton(ManageActionButton): |
---|
| 297 | grok.order(3) |
---|
| 298 | grok.context(ICustomer) |
---|
| 299 | grok.view(CustomerBaseDisplayFormPage) |
---|
| 300 | grok.require('waeup.handleCustomer') |
---|
| 301 | icon = 'actionicon_portrait.png' |
---|
| 302 | text = _('Change portrait') |
---|
| 303 | target = 'change_portrait' |
---|
[12015] | 304 | |
---|
[12346] | 305 | @property |
---|
| 306 | def target_url(self): |
---|
| 307 | CUSTMANAGE_STATES = getUtility( |
---|
| 308 | ICustomersUtils).CUSTMANAGE_CUSTOMER_STATES |
---|
| 309 | if self.context.state not in CUSTMANAGE_STATES: |
---|
| 310 | return '' |
---|
| 311 | return self.view.url(self.view.context, self.target) |
---|
[12015] | 312 | |
---|
[12346] | 313 | |
---|
[12059] | 314 | # Viewlets for customer documents |
---|
[12015] | 315 | |
---|
[12090] | 316 | class PDFDocumentOverviewActionButton(ManageActionButton): |
---|
[12059] | 317 | grok.order(2) |
---|
| 318 | grok.context(ICustomerDocumentsContainer) |
---|
| 319 | grok.view(DocumentsManageFormPage) |
---|
| 320 | grok.require('waeup.viewCustomer') |
---|
| 321 | icon = 'actionicon_pdf.png' |
---|
| 322 | text = _('Download documents overview') |
---|
[12091] | 323 | target = 'documents_overview_slip.pdf' |
---|
[12059] | 324 | |
---|
| 325 | |
---|
[12016] | 326 | class DocumentManageActionButton(ManageActionButton): |
---|
| 327 | grok.order(1) |
---|
| 328 | grok.context(ICustomerDocument) |
---|
| 329 | grok.view(DocumentDisplayFormPage) |
---|
| 330 | grok.require('waeup.manageCustomer') |
---|
| 331 | text = _('Manage') |
---|
| 332 | target = 'manage' |
---|
| 333 | |
---|
[12166] | 334 | @property |
---|
| 335 | def target_url(self): |
---|
| 336 | if not self.context.is_editable_by_manager: |
---|
| 337 | return '' |
---|
| 338 | return self.view.url(self.view.context, self.target) |
---|
[12018] | 339 | |
---|
[12166] | 340 | |
---|
[12018] | 341 | class DocumentEditActionButton(ManageActionButton): |
---|
| 342 | grok.order(1) |
---|
| 343 | grok.context(ICustomerDocument) |
---|
| 344 | grok.view(DocumentDisplayFormPage) |
---|
| 345 | grok.require('waeup.handleCustomer') |
---|
| 346 | text = _('Edit') |
---|
| 347 | target = 'edit' |
---|
| 348 | |
---|
| 349 | @property |
---|
| 350 | def target_url(self): |
---|
[12166] | 351 | if not self.context.is_editable_by_customer: |
---|
[12018] | 352 | return '' |
---|
| 353 | return self.view.url(self.view.context, self.target) |
---|
| 354 | |
---|
| 355 | |
---|
[12028] | 356 | class DocumentTrigTransActionButton(ManageActionButton): |
---|
| 357 | grok.order(2) |
---|
| 358 | grok.context(ICustomerDocument) |
---|
| 359 | grok.view(DocumentDisplayFormPage) |
---|
| 360 | grok.require('waeup.triggerTransition') |
---|
| 361 | icon = 'actionicon_trigtrans.png' |
---|
| 362 | text = _(u'Transition') |
---|
| 363 | target = 'trigtrans' |
---|
| 364 | |
---|
| 365 | |
---|
[12016] | 366 | class DocumentViewActionButton(ManageActionButton): |
---|
| 367 | grok.order(1) |
---|
| 368 | grok.context(ICustomerDocument) |
---|
| 369 | grok.view(DocumentManageFormPage) |
---|
[12018] | 370 | grok.require('waeup.handleCustomer') |
---|
[12016] | 371 | text = _('View') |
---|
| 372 | target = 'index' |
---|
| 373 | icon = 'actionicon_view.png' |
---|
[12062] | 374 | |
---|
[12090] | 375 | |
---|
| 376 | class PDFDocumentSlipActionButton(ManageActionButton): |
---|
[12062] | 377 | grok.order(3) |
---|
| 378 | grok.context(ICustomerDocument) |
---|
| 379 | grok.view(DocumentDisplayFormPage) |
---|
| 380 | grok.require('waeup.viewCustomer') |
---|
| 381 | icon = 'actionicon_pdf.png' |
---|
| 382 | text = _('Download document slip') |
---|
| 383 | target = 'document_slip.pdf' |
---|
[12090] | 384 | |
---|
[12214] | 385 | |
---|
[12097] | 386 | # Viewlets for customer contracts |
---|
[12090] | 387 | |
---|
[12097] | 388 | class PDFContractOverviewActionButton(ManageActionButton): |
---|
[12090] | 389 | grok.order(2) |
---|
[12097] | 390 | grok.context(IContractsContainer) |
---|
[12337] | 391 | grok.view(ContractsFormPage) |
---|
[12090] | 392 | grok.require('waeup.viewCustomer') |
---|
| 393 | icon = 'actionicon_pdf.png' |
---|
[12097] | 394 | text = _('Download contracts overview') |
---|
| 395 | target = 'contracts_overview_slip.pdf' |
---|
[12090] | 396 | |
---|
| 397 | |
---|
[12097] | 398 | class ContractManageActionButton(ManageActionButton): |
---|
[12090] | 399 | grok.order(1) |
---|
[12097] | 400 | grok.context(IContract) |
---|
| 401 | grok.view(ContractDisplayFormPage) |
---|
[12090] | 402 | grok.require('waeup.manageCustomer') |
---|
| 403 | text = _('Manage') |
---|
| 404 | target = 'manage' |
---|
| 405 | |
---|
| 406 | |
---|
[12097] | 407 | class ContractEditActionButton(ManageActionButton): |
---|
[12090] | 408 | grok.order(1) |
---|
[12097] | 409 | grok.context(IContract) |
---|
| 410 | grok.view(ContractDisplayFormPage) |
---|
[12090] | 411 | grok.require('waeup.handleCustomer') |
---|
| 412 | text = _('Edit') |
---|
| 413 | target = 'edit' |
---|
| 414 | |
---|
| 415 | @property |
---|
| 416 | def target_url(self): |
---|
[12337] | 417 | if not self.context.is_editable: |
---|
[12090] | 418 | return '' |
---|
| 419 | return self.view.url(self.view.context, self.target) |
---|
| 420 | |
---|
| 421 | |
---|
[12097] | 422 | class ContractTrigTransActionButton(ManageActionButton): |
---|
[12090] | 423 | grok.order(2) |
---|
[12097] | 424 | grok.context(IContract) |
---|
| 425 | grok.view(ContractDisplayFormPage) |
---|
[12090] | 426 | grok.require('waeup.triggerTransition') |
---|
| 427 | icon = 'actionicon_trigtrans.png' |
---|
| 428 | text = _(u'Transition') |
---|
| 429 | target = 'trigtrans' |
---|
| 430 | |
---|
| 431 | |
---|
[12097] | 432 | class ContractViewActionButton(ManageActionButton): |
---|
[12090] | 433 | grok.order(1) |
---|
[12097] | 434 | grok.context(IContract) |
---|
| 435 | grok.view(ContractManageFormPage) |
---|
[12090] | 436 | grok.require('waeup.handleCustomer') |
---|
| 437 | text = _('View') |
---|
| 438 | target = 'index' |
---|
| 439 | icon = 'actionicon_view.png' |
---|
| 440 | |
---|
| 441 | |
---|
[12097] | 442 | class PDFContractSlipActionButton(ManageActionButton): |
---|
[12090] | 443 | grok.order(3) |
---|
[12097] | 444 | grok.context(IContract) |
---|
| 445 | grok.view(ContractDisplayFormPage) |
---|
[12090] | 446 | grok.require('waeup.viewCustomer') |
---|
| 447 | icon = 'actionicon_pdf.png' |
---|
[12097] | 448 | text = _('Download contract slip') |
---|
| 449 | target = 'contract_slip.pdf' |
---|
[12090] | 450 | |
---|