source: main/waeup.ikoba/trunk/src/waeup/ikoba/payments/browser.py @ 15796

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

Henrik ist mutig.

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