1 | ## $Id: browser.py 12766 2015-03-15 09:37:37Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2015 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 | """UI components for payments. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from waeup.ikoba.browser.breadcrumbs import Breadcrumb |
---|
22 | from waeup.ikoba.interfaces import IIkobaObject |
---|
23 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
24 | from waeup.ikoba.browser.viewlets import PrimaryNavTab, ManageActionButton |
---|
25 | from waeup.ikoba.browser.layout import ( |
---|
26 | IkobaPage, IkobaEditFormPage, IkobaDisplayFormPage, |
---|
27 | NullValidator, jsaction, action, UtilityView) |
---|
28 | from waeup.ikoba.utils.helpers import get_current_principal |
---|
29 | |
---|
30 | from waeup.ikoba.payments.interfaces import ( |
---|
31 | IPaymentsContainer, IPayment, payment_states |
---|
32 | ) |
---|
33 | from waeup.ikoba.payments.payment import ( |
---|
34 | find_payable_from_payable_id, |
---|
35 | get_payment_providers |
---|
36 | ) |
---|
37 | from waeup.ikoba.payments.catalog import search |
---|
38 | |
---|
39 | |
---|
40 | grok.context(IIkobaObject) # Make IIkobaObject the default context |
---|
41 | grok.templatedir('templates') |
---|
42 | |
---|
43 | |
---|
44 | class PaymentsTab(PrimaryNavTab): |
---|
45 | """Payments tab in primary navigation. |
---|
46 | """ |
---|
47 | |
---|
48 | grok.context(IIkobaObject) |
---|
49 | grok.order(3) |
---|
50 | grok.require('waeup.viewPayments') |
---|
51 | grok.name('paymentstab') |
---|
52 | |
---|
53 | pnav = 5 |
---|
54 | tab_title = _(u'Payments') |
---|
55 | |
---|
56 | @property |
---|
57 | def link_target(self): |
---|
58 | return self.view.application_url('payments') |
---|
59 | |
---|
60 | |
---|
61 | class PaymentsBreadcrumb(Breadcrumb): |
---|
62 | """A breadcrumb for the payments container. |
---|
63 | """ |
---|
64 | grok.context(IPaymentsContainer) |
---|
65 | title = _('Payments') |
---|
66 | |
---|
67 | |
---|
68 | class PaymentBreadcrumb(Breadcrumb): |
---|
69 | """A breadcrumb for payment. |
---|
70 | """ |
---|
71 | grok.context(IPayment) |
---|
72 | parent = grok.getSite() |
---|
73 | target = None |
---|
74 | |
---|
75 | @property |
---|
76 | def title(self): |
---|
77 | id_part = self.context.payment_id[4:12] |
---|
78 | return _(u"Payment ${id}", mapping={'id': id_part}) |
---|
79 | |
---|
80 | @property |
---|
81 | def parent(self): |
---|
82 | """We display the payments payable as parent. |
---|
83 | """ |
---|
84 | payment = self.context |
---|
85 | payable = find_payable_from_payable_id(payment.payable_id) |
---|
86 | if payable is None: |
---|
87 | # fallback: display site home as parent |
---|
88 | return (grok.getSite(), 'index') |
---|
89 | return (payable, 'index') |
---|
90 | |
---|
91 | |
---|
92 | class PaymentsContainerPage(IkobaPage): |
---|
93 | """The standard view for payment containers. |
---|
94 | """ |
---|
95 | grok.context(IPaymentsContainer) |
---|
96 | grok.name('index') |
---|
97 | grok.require('waeup.viewPayments') |
---|
98 | grok.template('containerpage') |
---|
99 | label = _('Find payments') |
---|
100 | search_button = _('Find payment(s)') |
---|
101 | pnav = 5 |
---|
102 | |
---|
103 | @property |
---|
104 | def gateway_services(self): |
---|
105 | return get_payment_providers() |
---|
106 | |
---|
107 | @property |
---|
108 | def payment_states(self): |
---|
109 | return payment_states |
---|
110 | |
---|
111 | def update(self, *args, **kw): |
---|
112 | form = self.request.form |
---|
113 | self.hitlist = [] |
---|
114 | if 'searchterm' in form and form['searchterm']: |
---|
115 | self.searchterm = form['searchterm'] |
---|
116 | self.searchtype = form['searchtype'] |
---|
117 | elif 'old_searchterm' in form: |
---|
118 | self.searchterm = form['old_searchterm'] |
---|
119 | self.searchtype = form['old_searchtype'] |
---|
120 | else: |
---|
121 | if 'search' in form: |
---|
122 | self.flash(_('Empty search string'), type="warning") |
---|
123 | return |
---|
124 | self.hitlist = search(query=self.searchterm, |
---|
125 | searchtype=self.searchtype) |
---|
126 | if not self.hitlist: |
---|
127 | self.flash(_('No payment found.'), type="warning") |
---|
128 | return |
---|
129 | |
---|
130 | |
---|
131 | class PaymentsManageActionButton(ManageActionButton): |
---|
132 | grok.order(1) |
---|
133 | grok.context(IPaymentsContainer) |
---|
134 | grok.view(PaymentsContainerPage) |
---|
135 | grok.require('waeup.managePayments') |
---|
136 | text = _('Manage') |
---|
137 | target = 'manage' |
---|
138 | |
---|
139 | |
---|
140 | class PaymentsContainerManagePage(IkobaPage): |
---|
141 | """The manage page for payment containers. |
---|
142 | """ |
---|
143 | grok.context(IPaymentsContainer) |
---|
144 | grok.name('manage') |
---|
145 | grok.require('waeup.managePayments') |
---|
146 | grok.template('containermanagepage') |
---|
147 | pnav = 5 |
---|
148 | label = _('Manage payment section') |
---|
149 | search_button = _('Find payment(s)') |
---|
150 | remove_button = _('Remove selected') |
---|
151 | |
---|
152 | @property |
---|
153 | def gateway_services(self): |
---|
154 | return get_payment_providers() |
---|
155 | |
---|
156 | @property |
---|
157 | def payment_states(self): |
---|
158 | return payment_states |
---|
159 | |
---|
160 | def update(self, *args, **kw): |
---|
161 | form = self.request.form |
---|
162 | self.hitlist = [] |
---|
163 | if 'searchterm' in form and form['searchterm']: |
---|
164 | self.searchterm = form['searchterm'] |
---|
165 | self.searchtype = form['searchtype'] |
---|
166 | elif 'old_searchterm' in form: |
---|
167 | self.searchterm = form['old_searchterm'] |
---|
168 | self.searchtype = form['old_searchtype'] |
---|
169 | else: |
---|
170 | if 'search' in form: |
---|
171 | self.flash(_('Empty search string'), type="warning") |
---|
172 | return |
---|
173 | if not 'entries' in form: |
---|
174 | self.hitlist = search(query=self.searchterm, |
---|
175 | searchtype=self.searchtype) |
---|
176 | if not self.hitlist: |
---|
177 | self.flash(_('No payment found.'), type="warning") |
---|
178 | if 'remove' in form: |
---|
179 | self.flash(_('No item selected.'), type="warning") |
---|
180 | return |
---|
181 | entries = form['entries'] |
---|
182 | if isinstance(entries, basestring): |
---|
183 | entries = [entries] |
---|
184 | deleted = [] |
---|
185 | for entry in entries: |
---|
186 | if 'remove' in form: |
---|
187 | del self.context[entry] |
---|
188 | deleted.append(entry) |
---|
189 | self.hitlist = search(query=self.searchterm, |
---|
190 | searchtype=self.searchtype) |
---|
191 | if len(deleted): |
---|
192 | self.flash(_('Successfully removed: ${a}', |
---|
193 | mapping={'a': ','.join(deleted)})) |
---|
194 | ob_class = self.__implemented__.__name__.replace('waeup.ikoba.','') |
---|
195 | self.context.logger.info( |
---|
196 | '%s - removed: %s' % (ob_class, ','.join(deleted))) |
---|
197 | return |
---|