[FIX] edi: improve generation/usage of edi_web_url_view for template rendering

- wrap edi_web_url_view generation to _edi_get_object_web_url_view() method.
  - automatically add 'edi_web_url_view' to all email.template rendering context

  - introduce LazyEdiWebUrlViewGetter class, to allow resolving record wihtin
    template rendering context - only when necessary. This to prevent generating
    unrequested edi_web_url_view links

bzr revid: xal@openerp.com-20120820083456-teqk4kdqe8mv59p8
This commit is contained in:
Xavier ALT
2012-08-20 10:34:56 +02:00
parent dc7ed565d6
commit bd5bc1ae18
3 changed files with 80 additions and 14 deletions
+1
View File
@@ -23,5 +23,6 @@ import edi
import res_partner
import res_company
import res_currency
import email_template
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+9 -14
View File
@@ -472,8 +472,7 @@ class EDIMixin(object):
_logger.warning('Ignoring EDI mail notification, template %s cannot be located', template_ext_id)
return
for edi_record in self.browse(local_cr, uid, ids, context=context):
edi_token = self.pool.get('edi.document').export_edi(local_cr, uid, [edi_record], context = context)[0]
edi_context = dict(context, edi_web_url_view=EDI_VIEW_WEB_URL % (web_root_url, local_cr.dbname, edi_token))
edi_context = dict(context, edi_web_url_view=self._edi_get_object_web_url_view(cr, uid, edi_record, context=context))
self.pool.get('email.template').send_mail(local_cr, uid, mail_tmpl.id, edi_record.id,
force_send=False, context=edi_context)
_logger.info('EDI export successful for %s #%s, email notification sent.', self._name, edi_record.id)
@@ -487,6 +486,14 @@ class EDIMixin(object):
threading.Thread(target=email_task, name='EDI ExportAndEmail for %s %r' % (self._name, ids)).start()
return True
def _edi_get_object_web_url_view(self, cr, uid, record, context=None):
web_root_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
if web_root_url:
_logger.warning('Ignoring EDI mail notification, web.base.url not defined in parameters')
return ''
edi_token = self.pool.get('edi.document').export_edi(cr, uid, [record], context=context)[0]
return EDI_VIEW_WEB_URL % (web_root_url, cr.dbname, edi_token)
def _edi_get_object_by_name(self, cr, uid, name, model_name, context=None):
model = self.pool.get(model_name)
search_results = model.name_search(cr, uid, name, operator='=', context=context)
@@ -681,17 +688,5 @@ class EDIMixin(object):
self._edi_import_attachments(cr, uid, record_id, edi_document, context=context)
return record_id
class mail_compose_message(osv.osv_memory):
_inherit = 'mail.compose.message'
def _pre_render_template(self, cr, uid, active_model, active_id, context):
if active_model in ['account.invoice', 'sale.order', 'purchase.order']:
web_root_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
edi_record = self.pool.get(active_model).browse(cr, uid, active_id, context)
edi_token = self.pool.get('edi.document').export_edi(cr, uid, [edi_record])[0]
ctx = dict(context, edi_web_url_view=EDI_VIEW_WEB_URL % (web_root_url, cr.dbname, edi_token))
context.update(ctx)
return super(mail_compose_message,self)._pre_render_template(cr, uid, active_model, active_id, context)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+70
View File
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Business Applications
# Copyright (c) 2011-2012 OpenERP S.A. <http://openerp.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv, fields
from edi import EDIMixin
class LazyEdiWebUrlViewGetter(object):
"""
This class allow to generate EDI web view url only when the template
really use it (lazy mode)
"""
def __init__(self, cr, uid, model_obj, res_id, context):
self.cr = cr
self.uid = uid
self.model_obj = model_obj
self.res_id = res_id
self.context = context
self.web_view_url = None
def get_web_view_url(self):
"""
generate and return web_view_url of the current object
:return: unicode string
"""
if self.web_view_url is None:
record = self.model_obj.browse(self.cr, self.uid, self.res_id, context=self.context)
self.web_view_url = self.model_obj._edi_get_object_web_url_view(self.cr, self.uid, record, context=self.context)
return self.web_view_url
def __unicode__(self):
return self.get_web_view_url()
__str__ = __unicode__
def __nonzero__(self):
# handle ctx.get('edi_web_view_url') or 'n/a' things
return self.get_web_view_url() and True or False
class email_template(osv.osv):
_inherit = 'email.template'
def _prepare_render_template_context(self, cr, uid, model, res_id, context=None):
render_context = super(email_template, self)._prepare_render_template_context(cr, uid, model, res_id, context=context)
# add 'edi_web_url_view' lazy object for all EDI objects, but do not override it
# if it's already present (forced by someone else)
model_obj = self.pool.get(model)
if isinstance(model_obj, EDIMixin) and not 'edi_web_url_view' in render_context:
render_context = render_context.copy()
render_context['edi_web_url_view'] = LazyEdiWebUrlViewGetter(cr, uid, model_obj, res_id, context)
return render_context