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