source: main/waeup.ikoba/trunk/src/waeup/ikoba/customers/viewlets.py @ 12346

Last change on this file since 12346 was 12346, checked in by Henrik Bettermann, 10 years ago

First batch of UI improvements.

  • Property svn:keywords set to Id
File size: 12.5 KB
Line 
1## $Id: viewlets.py 12346 2014-12-30 17:47:58Z henrik $
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
19import grok
20from zope.i18n import translate
21from zope.interface import Interface
22from zope.component import getUtility
23from waeup.ikoba.interfaces import IIkobaObject
24from waeup.ikoba.interfaces import MessageFactory as _
25from waeup.ikoba.browser.viewlets import (
26    PrimaryNavTab, ManageActionButton, AddActionButton)
27from waeup.ikoba.browser.layout import default_primary_nav_template
28from waeup.ikoba.customers.interfaces import (
29    ICustomer, ICustomersContainer,
30    ICustomerDocumentsContainer, ICustomerDocument,
31    IContractsContainer, IContract, ICustomersUtils)
32from waeup.ikoba.customers.browser import (
33    CustomersContainerPage, CustomersContainerManagePage,
34    CustomerBaseDisplayFormPage,
35    DocumentsManageFormPage, DocumentDisplayFormPage, DocumentManageFormPage,
36    ContractsFormPage, ContractDisplayFormPage,
37    ContractManageFormPage)
38
39grok.context(IIkobaObject)  # Make IIkobaObject the default context
40grok.templatedir('browser_templates')
41
42
43class 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):
57        return self.view.application_url('customers')
58
59
60class PrimaryCustomerNavManager(grok.ViewletManager):
61    """Viewlet manager for the primary navigation tab.
62    """
63    grok.name('primary_nav_customer')
64
65
66class 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
88
89class 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'},
113            {'url':customer_url + '/contracts', 'title':_('My Contracts')},
114            {'url':customer_url + '/documents', 'title':_('My Documents')},
115            {'url':customer_url + '/history', 'title':_('History')},
116            ]
117        return targets
118
119
120class CustomerManageSidebar(grok.ViewletManager):
121    grok.name('left_customermanage')
122
123
124class 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
141        lang = self.request.cookies.get('ikoba.language')
142        text = translate(self.text, 'waeup.ikoba',
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
149
150class CustomerManageBaseLink(CustomerManageLink):
151    grok.order(2)
152    link = 'index'
153    text = _(u'Base Data')
154
155
156class CustomerManageContractsLink(CustomerManageLink):
157    grok.order(3)
158    link = 'contracts'
159    text = _(u'Contracts')
160
161
162class CustomerManageDocumentsLink(CustomerManageLink):
163    grok.order(4)
164    link = 'documents'
165    text = _(u'Documents')
166
167
168class CustomerManageHistoryLink(CustomerManageLink):
169    grok.order(8)
170    link = 'history'
171    text = _(u'History')
172
173
174class 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
181
182class 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
190
191class 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
200
201class 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
209
210class CustomerTrigTransActionButton(ManageActionButton):
211    grok.order(2)
212    grok.context(ICustomer)
213    grok.view(CustomerBaseDisplayFormPage)
214    grok.require('waeup.triggerTransition')
215    icon = 'actionicon_trigtrans.png'
216    text = _(u'Transition')
217    target = 'trigtrans'
218
219
220class 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
229
230class 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
250
251class 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);" % _(
269            "'A history message will be added. Are you sure?'")
270
271
272class CustomerBaseActionButton(ManageActionButton):
273    grok.order(1)
274    grok.context(ICustomer)
275    grok.view(CustomerBaseDisplayFormPage)
276    grok.require('waeup.handleCustomer')
277    text = _('Edit')
278    target = 'edit_base'
279
280
281class CustomerPasswordActionButton(ManageActionButton):
282    grok.order(2)
283    grok.context(ICustomer)
284    grok.view(CustomerBaseDisplayFormPage)
285    grok.require('waeup.handleCustomer')
286    icon = 'actionicon_key.png'
287    text = _('Change password')
288    target = 'changepassword'
289
290
291class CustomerPassportActionButton(ManageActionButton):
292    grok.order(3)
293    grok.context(ICustomer)
294    grok.view(CustomerBaseDisplayFormPage)
295    grok.require('waeup.handleCustomer')
296    icon = 'actionicon_portrait.png'
297    text = _('Change portrait')
298    target = 'change_portrait'
299
300    @property
301    def target_url(self):
302        CUSTMANAGE_STATES = getUtility(
303            ICustomersUtils).CUSTMANAGE_CUSTOMER_STATES
304        if self.context.state not in CUSTMANAGE_STATES:
305            return ''
306        return self.view.url(self.view.context, self.target)
307
308
309# Viewlets for customer documents
310
311class PDFDocumentOverviewActionButton(ManageActionButton):
312    grok.order(2)
313    grok.context(ICustomerDocumentsContainer)
314    grok.view(DocumentsManageFormPage)
315    grok.require('waeup.viewCustomer')
316    icon = 'actionicon_pdf.png'
317    text = _('Download documents overview')
318    target = 'documents_overview_slip.pdf'
319
320
321class DocumentManageActionButton(ManageActionButton):
322    grok.order(1)
323    grok.context(ICustomerDocument)
324    grok.view(DocumentDisplayFormPage)
325    grok.require('waeup.manageCustomer')
326    text = _('Manage')
327    target = 'manage'
328
329    @property
330    def target_url(self):
331        if not self.context.is_editable_by_manager:
332            return ''
333        return self.view.url(self.view.context, self.target)
334
335
336class DocumentEditActionButton(ManageActionButton):
337    grok.order(1)
338    grok.context(ICustomerDocument)
339    grok.view(DocumentDisplayFormPage)
340    grok.require('waeup.handleCustomer')
341    text = _('Edit')
342    target = 'edit'
343
344    @property
345    def target_url(self):
346        if not self.context.is_editable_by_customer:
347            return ''
348        return self.view.url(self.view.context, self.target)
349
350
351class DocumentTrigTransActionButton(ManageActionButton):
352    grok.order(2)
353    grok.context(ICustomerDocument)
354    grok.view(DocumentDisplayFormPage)
355    grok.require('waeup.triggerTransition')
356    icon = 'actionicon_trigtrans.png'
357    text = _(u'Transition')
358    target = 'trigtrans'
359
360
361class DocumentViewActionButton(ManageActionButton):
362    grok.order(1)
363    grok.context(ICustomerDocument)
364    grok.view(DocumentManageFormPage)
365    grok.require('waeup.handleCustomer')
366    text = _('View')
367    target = 'index'
368    icon = 'actionicon_view.png'
369
370
371class PDFDocumentSlipActionButton(ManageActionButton):
372    grok.order(3)
373    grok.context(ICustomerDocument)
374    grok.view(DocumentDisplayFormPage)
375    grok.require('waeup.viewCustomer')
376    icon = 'actionicon_pdf.png'
377    text = _('Download document slip')
378    target = 'document_slip.pdf'
379
380
381# Viewlets for customer contracts
382
383class PDFContractOverviewActionButton(ManageActionButton):
384    grok.order(2)
385    grok.context(IContractsContainer)
386    grok.view(ContractsFormPage)
387    grok.require('waeup.viewCustomer')
388    icon = 'actionicon_pdf.png'
389    text = _('Download contracts overview')
390    target = 'contracts_overview_slip.pdf'
391
392
393class ContractManageActionButton(ManageActionButton):
394    grok.order(1)
395    grok.context(IContract)
396    grok.view(ContractDisplayFormPage)
397    grok.require('waeup.manageCustomer')
398    text = _('Manage')
399    target = 'manage'
400
401
402class ContractEditActionButton(ManageActionButton):
403    grok.order(1)
404    grok.context(IContract)
405    grok.view(ContractDisplayFormPage)
406    grok.require('waeup.handleCustomer')
407    text = _('Edit')
408    target = 'edit'
409
410    @property
411    def target_url(self):
412        if not self.context.is_editable:
413            return ''
414        return self.view.url(self.view.context, self.target)
415
416
417class ContractTrigTransActionButton(ManageActionButton):
418    grok.order(2)
419    grok.context(IContract)
420    grok.view(ContractDisplayFormPage)
421    grok.require('waeup.triggerTransition')
422    icon = 'actionicon_trigtrans.png'
423    text = _(u'Transition')
424    target = 'trigtrans'
425
426
427class ContractViewActionButton(ManageActionButton):
428    grok.order(1)
429    grok.context(IContract)
430    grok.view(ContractManageFormPage)
431    grok.require('waeup.handleCustomer')
432    text = _('View')
433    target = 'index'
434    icon = 'actionicon_view.png'
435
436
437class PDFContractSlipActionButton(ManageActionButton):
438    grok.order(3)
439    grok.context(IContract)
440    grok.view(ContractDisplayFormPage)
441    grok.require('waeup.viewCustomer')
442    icon = 'actionicon_pdf.png'
443    text = _('Download contract slip')
444    target = 'contract_slip.pdf'
445
Note: See TracBrowser for help on using the repository browser.