## $Id: browser.py 12767 2015-03-15 09:42:41Z henrik $ ## ## Copyright (C) 2015 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """UI components for payments. """ import grok from waeup.ikoba.browser.breadcrumbs import Breadcrumb from waeup.ikoba.interfaces import IIkobaObject from waeup.ikoba.interfaces import MessageFactory as _ from waeup.ikoba.browser.viewlets import PrimaryNavTab, ManageActionButton from waeup.ikoba.browser.layout import ( IkobaPage, IkobaEditFormPage, IkobaDisplayFormPage, NullValidator, jsaction, action, UtilityView) from waeup.ikoba.utils.helpers import get_current_principal from waeup.ikoba.payments.interfaces import ( IPaymentsContainer, IPayment, payment_states ) from waeup.ikoba.payments.payment import ( find_payable_from_payable_id, get_payment_providers ) from waeup.ikoba.payments.catalog import search grok.context(IIkobaObject) # Make IIkobaObject the default context grok.templatedir('browser_templates') class PaymentsTab(PrimaryNavTab): """Payments tab in primary navigation. """ grok.context(IIkobaObject) grok.order(3) grok.require('waeup.viewPayments') grok.name('paymentstab') pnav = 5 tab_title = _(u'Payments') @property def link_target(self): return self.view.application_url('payments') class PaymentsBreadcrumb(Breadcrumb): """A breadcrumb for the payments container. """ grok.context(IPaymentsContainer) title = _('Payments') class PaymentBreadcrumb(Breadcrumb): """A breadcrumb for payment. """ grok.context(IPayment) parent = grok.getSite() target = None @property def title(self): id_part = self.context.payment_id[4:12] return _(u"Payment ${id}", mapping={'id': id_part}) @property def parent(self): """We display the payments payable as parent. """ payment = self.context payable = find_payable_from_payable_id(payment.payable_id) if payable is None: # fallback: display site home as parent return (grok.getSite(), 'index') return (payable, 'index') class PaymentsContainerPage(IkobaPage): """The standard view for payment containers. """ grok.context(IPaymentsContainer) grok.name('index') grok.require('waeup.viewPayments') grok.template('containerpage') label = _('Find payments') search_button = _('Find payment(s)') pnav = 5 @property def gateway_services(self): return get_payment_providers() @property def payment_states(self): return payment_states def update(self, *args, **kw): form = self.request.form self.hitlist = [] if 'searchterm' in form and form['searchterm']: self.searchterm = form['searchterm'] self.searchtype = form['searchtype'] elif 'old_searchterm' in form: self.searchterm = form['old_searchterm'] self.searchtype = form['old_searchtype'] else: if 'search' in form: self.flash(_('Empty search string'), type="warning") return self.hitlist = search(query=self.searchterm, searchtype=self.searchtype) if not self.hitlist: self.flash(_('No payment found.'), type="warning") return class PaymentsManageActionButton(ManageActionButton): grok.order(1) grok.context(IPaymentsContainer) grok.view(PaymentsContainerPage) grok.require('waeup.managePayments') text = _('Manage') target = 'manage' class PaymentsContainerManagePage(IkobaPage): """The manage page for payment containers. """ grok.context(IPaymentsContainer) grok.name('manage') grok.require('waeup.managePayments') grok.template('containermanagepage') pnav = 5 label = _('Manage payment section') search_button = _('Find payment(s)') remove_button = _('Remove selected') @property def gateway_services(self): return get_payment_providers() @property def payment_states(self): return payment_states def update(self, *args, **kw): form = self.request.form self.hitlist = [] if 'searchterm' in form and form['searchterm']: self.searchterm = form['searchterm'] self.searchtype = form['searchtype'] elif 'old_searchterm' in form: self.searchterm = form['old_searchterm'] self.searchtype = form['old_searchtype'] else: if 'search' in form: self.flash(_('Empty search string'), type="warning") return if not 'entries' in form: self.hitlist = search(query=self.searchterm, searchtype=self.searchtype) if not self.hitlist: self.flash(_('No payment found.'), type="warning") if 'remove' in form: self.flash(_('No item selected.'), type="warning") return entries = form['entries'] if isinstance(entries, basestring): entries = [entries] deleted = [] for entry in entries: if 'remove' in form: del self.context[entry] deleted.append(entry) self.hitlist = search(query=self.searchterm, searchtype=self.searchtype) if len(deleted): self.flash(_('Successfully removed: ${a}', mapping={'a': ','.join(deleted)})) ob_class = self.__implemented__.__name__.replace('waeup.ikoba.','') self.context.logger.info( '%s - removed: %s' % (ob_class, ','.join(deleted))) return