1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Business Applications
5 # Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 from urllib import quote as quote
25 from openerp.osv import osv, fields
26 from openerp.tools.translate import _
27 from openerp.tools import float_repr
29 _logger = logging.getLogger(__name__)
31 from mako.template import Template as MakoTemplate
33 _logger.warning("payment_acquirer: mako templates not available, payment acquirer will not work!")
36 class acquirer(osv.Model):
37 _name = 'portal.payment.acquirer'
38 _description = 'Online Payment Acquirer'
41 'name': fields.char('Name', required=True),
42 'form_template': fields.text('Payment form template (HTML)', translate=True, required=True),
43 'visible': fields.boolean('Visible', help="Make this payment acquirer available in portal forms (Customer invoices, etc.)"),
50 def render(self, cr, uid, id, object, reference, currency, amount, context=None, **kwargs):
51 """ Renders the form template of the given acquirer as a mako template """
52 if not isinstance(id, (int,long)):
54 this = self.browse(cr, uid, id)
58 i18n_kind = _(object._description) # may fail to translate, but at least we try
59 result = MakoTemplate(this.form_template).render_unicode(object=object,
65 # context kw would clash with mako internals
67 format_exceptions=True)
70 _logger.exception("failed to render mako template value for payment.acquirer %s: %r", this.name, this.form_template)
73 def _wrap_payment_block(self, cr, uid, html_block, amount, currency, context=None):
74 payment_header = _('Pay safely online')
75 amount_str = float_repr(amount, self.pool.get('decimal.precision').precision_get(cr, uid, 'Account'))
76 currency_str = currency.symbol or currency.name
77 amount = u"%s %s" % ((currency_str, amount_str) if currency.position == 'before' else (amount_str, currency_str))
78 result = """<div class="payment_acquirers">
79 <div class="payment_header">
80 <div class="payment_amount">%s</div>
84 </div>""" % (amount, payment_header)
85 return result % html_block
87 def render_payment_block(self, cr, uid, object, reference, currency, amount, context=None, **kwargs):
88 """ Renders all visible payment acquirer forms for the given rendering context, and
89 return them wrapped in an appropriate HTML block, ready for direct inclusion
90 in an OpenERP v7 form view """
91 acquirer_ids = self.search(cr, uid, [('visible', '=', True)])
95 for this in self.browse(cr, uid, acquirer_ids):
96 html_forms.append(this.render(object, reference, currency, amount, context=context, **kwargs))
97 html_block = '\n'.join(filter(None,html_forms))
98 return self._wrap_payment_block(cr, uid, html_block, amount, currency, context=context)