Browse Source

[ADD] explicit tests for UnquoteRecordset

pull/163/head^2
Holger Brunn 8 years ago
parent
commit
b5228c7752
  1. 55
      website_mail_qweb/controllers/main.py
  2. 16
      website_mail_qweb/tests/test_website_mail_qweb.py

55
website_mail_qweb/controllers/main.py

@ -4,34 +4,35 @@
from openerp.addons.website_mail.controllers.email_designer import\ from openerp.addons.website_mail.controllers.email_designer import\
WebsiteEmailDesigner WebsiteEmailDesigner
from openerp import http from openerp import http
from openerp.http import request
class UnquoteObject(str):
def __getattr__(self, name):
return UnquoteObject('%s.%s' % (self, name))
class UnquoteRecordset(object):
def __init__(self, recordset, name):
self.__recordset = recordset
self.__name = name
def __repr__(self):
return self
def __getitem__(self, key):
if isinstance(key, basestring) and key in self.__recordset._fields:
return self.__recordset[key]
return UnquoteRecordset(
self.__recordset[key], '%s[%s]' % (self.__name, key)
)
def __call__(self, *args, **kwargs):
return UnquoteObject(
'%s(%s)' % (
self,
','.join(
[
UnquoteObject(
a if not isinstance(a, basestring)
else "'%s'" % a
)
for a in args
] +
[
'%s=%s' % (UnquoteObject(k), v)
for (k, v) in kwargs.iteritems()
]
def __getattr__(self, name):
recordset = self.__recordset
if name in recordset._fields:
if recordset._fields[name].relational:
return UnquoteRecordset(
recordset[name], '%s.%s' % (self.__name, name)
) )
)
)
elif recordset._fields[name].type in ('char', 'text'):
return '%s.%s' % (self.__name, name)
elif recordset._fields[name].type in ('integer', 'float'):
return 42
else:
return recordset[name]
return getattr(recordset, name)
class Main(WebsiteEmailDesigner): class Main(WebsiteEmailDesigner):
@ -40,11 +41,15 @@ class Main(WebsiteEmailDesigner):
result = super(Main, self).index( result = super(Main, self).index(
model, res_id, template_model=template_model, **kw model, res_id, template_model=template_model, **kw
) )
env = request.env
qcontext = result.qcontext qcontext = result.qcontext
record = qcontext['record']
record = qcontext.get('record', env['email.template'].new())
if record.body_type == 'qweb': if record.body_type == 'qweb':
qcontext['body_field'] = 'body_view_id' qcontext['body_field'] = 'body_view_id'
qcontext['mode'] = 'email_designer' qcontext['mode'] = 'email_designer'
qcontext['object'] = UnquoteObject('object')
qcontext['object'] = UnquoteRecordset(
env[record.model_id.model].new(),
'object',
)
qcontext['email_template'] = record qcontext['email_template'] = record
return result return result

16
website_mail_qweb/tests/test_website_mail_qweb.py

@ -2,6 +2,7 @@
# © 2017 Therp BV <http://therp.nl> # © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp.tests.common import HttpCase from openerp.tests.common import HttpCase
from ..controllers.main import UnquoteRecordset
class TestWebsiteMailQweb(HttpCase): class TestWebsiteMailQweb(HttpCase):
@ -12,3 +13,18 @@ class TestWebsiteMailQweb(HttpCase):
self.env.ref('email_template_qweb.email_template_demo1').id self.env.ref('email_template_qweb.email_template_demo1').id
) )
self.assertIn('Dear object.name,', result.read()) self.assertIn('Dear object.name,', result.read())
def test_unquote_recordset(self):
record = UnquoteRecordset(self.env['res.partner'].new(), 'object')
self.assertEqual(record.name, 'object.name')
self.assertEqual(record.parent_id.name, 'object.parent_id.name')
self.assertEqual(record.id, 42)
self.assertEqual(
record.create_date,
self.env['res.partner']._fields['create_date'].null(self.env)
)
self.assertEqual(
record[:1]['create_date'],
self.env['res.partner']._fields['create_date'].null(self.env)
)
self.assertEqual(record._fields, self.env['res.partner']._fields)
Loading…
Cancel
Save