[ADD] ir.qweb FileSystemLoader
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import collections
|
||||
import copy
|
||||
import cStringIO
|
||||
import datetime
|
||||
import hashlib
|
||||
@@ -61,6 +62,26 @@ def raise_qweb_exception(etype=None, **kw):
|
||||
e.qweb['cause'] = original
|
||||
raise
|
||||
|
||||
class FileSystemLoader(object):
|
||||
def __init__(self, path):
|
||||
# TODO: support multiple files #add_file() + add cache
|
||||
self.path = path
|
||||
self.doc = etree.parse(path).getroot()
|
||||
|
||||
def __iter__(self):
|
||||
for node in self.doc:
|
||||
name = node.get('t-name')
|
||||
if name:
|
||||
yield name
|
||||
|
||||
def __call__(self, name):
|
||||
for node in self.doc:
|
||||
if node.get('t-name') == name:
|
||||
root = etree.Element('templates')
|
||||
root.append(copy.deepcopy(node))
|
||||
arch = etree.tostring(root, encoding='utf-8', xml_declaration=True)
|
||||
return arch
|
||||
|
||||
class QWebContext(dict):
|
||||
def __init__(self, cr, uid, data, loader=None, templates=None, context=None):
|
||||
self.cr = cr
|
||||
|
||||
@@ -7,7 +7,7 @@ import re
|
||||
import collections
|
||||
|
||||
from lxml import etree
|
||||
import openerp.addons.base.ir.ir_qweb
|
||||
from openerp.addons.base.ir.ir_qweb import QWebContext, FileSystemLoader
|
||||
import openerp.modules
|
||||
|
||||
from openerp.tests import common
|
||||
@@ -112,12 +112,13 @@ class TestQWeb(common.TransactionCase):
|
||||
return lambda: self.run_test_file(os.path.join(path, f))
|
||||
|
||||
def run_test_file(self, path):
|
||||
context = openerp.addons.base.ir.ir_qweb.QWebContext(self.cr, self.uid, {})
|
||||
qweb = self.env['ir.qweb']
|
||||
doc = etree.parse(path).getroot()
|
||||
qweb.load_document(doc, None, context)
|
||||
for template in context.templates:
|
||||
if template.startswith('_'): continue
|
||||
loader = FileSystemLoader(path)
|
||||
context = QWebContext(self.cr, self.uid, {}, loader=loader)
|
||||
qweb = self.env['ir.qweb']
|
||||
for template in loader:
|
||||
if not template or template.startswith('_'):
|
||||
continue
|
||||
param = doc.find('params[@id="{}"]'.format(template))
|
||||
# OrderedDict to ensure JSON mappings are iterated in source order
|
||||
# so output is predictable & repeatable
|
||||
|
||||
Reference in New Issue
Block a user