[IMP] field regularity
Make @file a more generic data source, add base64 type for b64 encoding bzr revid: xmo@openerp.com-20130927121325-mbjm4612qkaa0z0x
This commit is contained in:
parent
47c6314c44
commit
0b90087828
|
@ -206,8 +206,8 @@ Extra attributes can either preprocess the body or replace its use entirely:
|
||||||
``@type`` (optional)
|
``@type`` (optional)
|
||||||
|
|
||||||
One of ``char``, ``int``, ``float``, ``list``, ``tuple``, ``xml`` or
|
One of ``char``, ``int``, ``float``, ``list``, ``tuple``, ``xml`` or
|
||||||
``html`` or ``file``. Converts the ``field``'s body to the specified type
|
``html``, ``file`` or ``base64``. Converts the ``field``'s body to the
|
||||||
(or validates the body's content)
|
specified type (or validates the body's content)
|
||||||
|
|
||||||
* ``xml`` will join multiple XML nodes under a single ``<data>`` root
|
* ``xml`` will join multiple XML nodes under a single ``<data>`` root
|
||||||
* in ``xml`` and ``html``, external ids can be referenced using
|
* in ``xml`` and ``html``, external ids can be referenced using
|
||||||
|
@ -217,6 +217,13 @@ Extra attributes can either preprocess the body or replace its use entirely:
|
||||||
* ``file`` expects a module-local path and will save the path prefixed with
|
* ``file`` expects a module-local path and will save the path prefixed with
|
||||||
the current module's name, separated by a ``,`` (comma). For use with
|
the current module's name, separated by a ``,`` (comma). For use with
|
||||||
:py:func:`~openerp.modules.module.get_module_resource`.
|
:py:func:`~openerp.modules.module.get_module_resource`.
|
||||||
|
* ``base64`` expects binary data, encodes it to base64 and sets it. Mostly
|
||||||
|
useful with ``@file``
|
||||||
|
|
||||||
|
``@file``
|
||||||
|
|
||||||
|
Can be used with types ``char`` and ``base64``, sources the field's content
|
||||||
|
from the specified file instead of the field's text body.
|
||||||
|
|
||||||
``@model``
|
``@model``
|
||||||
|
|
||||||
|
|
|
@ -130,11 +130,18 @@
|
||||||
<rng:group>
|
<rng:group>
|
||||||
<rng:attribute name="type">
|
<rng:attribute name="type">
|
||||||
<rng:choice>
|
<rng:choice>
|
||||||
|
<rng:value>base64</rng:value>
|
||||||
<rng:value>char</rng:value>
|
<rng:value>char</rng:value>
|
||||||
<rng:value>file</rng:value>
|
<rng:value>file</rng:value>
|
||||||
</rng:choice>
|
</rng:choice>
|
||||||
</rng:attribute>
|
</rng:attribute>
|
||||||
<rng:text/>
|
<rng:choice>
|
||||||
|
<rng:group>
|
||||||
|
<rng:attribute name="file"/>
|
||||||
|
<rng:empty/>
|
||||||
|
</rng:group>
|
||||||
|
<rng:text/>
|
||||||
|
</rng:choice>
|
||||||
</rng:group>
|
</rng:group>
|
||||||
<rng:group>
|
<rng:group>
|
||||||
<rng:attribute name="type"><rng:value>int</rng:value></rng:attribute>
|
<rng:attribute name="type"><rng:value>int</rng:value></rng:attribute>
|
||||||
|
@ -167,10 +174,6 @@
|
||||||
<rng:ref name="any"/>
|
<rng:ref name="any"/>
|
||||||
</rng:oneOrMore>
|
</rng:oneOrMore>
|
||||||
</rng:group>
|
</rng:group>
|
||||||
<rng:group>
|
|
||||||
<rng:attribute name="file"/>
|
|
||||||
<rng:empty/>
|
|
||||||
</rng:group>
|
|
||||||
<rng:group>
|
<rng:group>
|
||||||
<rng:attribute name="ref"/>
|
<rng:attribute name="ref"/>
|
||||||
<rng:empty/>
|
<rng:empty/>
|
||||||
|
@ -186,9 +189,7 @@
|
||||||
<rng:optional><rng:attribute name="use"/></rng:optional>
|
<rng:optional><rng:attribute name="use"/></rng:optional>
|
||||||
<rng:empty/>
|
<rng:empty/>
|
||||||
</rng:group>
|
</rng:group>
|
||||||
<rng:group>
|
<rng:text/>
|
||||||
<rng:text/>
|
|
||||||
</rng:group>
|
|
||||||
</rng:choice>
|
</rng:choice>
|
||||||
</rng:element>
|
</rng:element>
|
||||||
</rng:define>
|
</rng:define>
|
||||||
|
|
|
@ -175,34 +175,38 @@ def _eval_xml(self, node, pool, cr, uid, idref, context=None):
|
||||||
if t == 'html':
|
if t == 'html':
|
||||||
return _process("".join([etree.tostring(n, encoding='utf-8')
|
return _process("".join([etree.tostring(n, encoding='utf-8')
|
||||||
for n in node]), idref)
|
for n in node]), idref)
|
||||||
|
|
||||||
|
data = node.text
|
||||||
if node.get('file'):
|
if node.get('file'):
|
||||||
import openerp.tools
|
with openerp.tools.file_open(node.get('file'), 'rb') as f:
|
||||||
import base64
|
data = f.read()
|
||||||
fp = openerp.tools.file_open(node.get('file'))
|
|
||||||
result = base64.b64encode(fp.read())
|
|
||||||
return result
|
|
||||||
|
|
||||||
if t == 'file':
|
if t == 'file':
|
||||||
from ..modules import module
|
from ..modules import module
|
||||||
path = node.text.strip()
|
path = data.strip()
|
||||||
if not module.get_module_resource(self.module, path):
|
if not module.get_module_resource(self.module, path):
|
||||||
raise IOError("No such file or directory: '%s' in %s" % (
|
raise IOError("No such file or directory: '%s' in %s" % (
|
||||||
path, self.module))
|
path, self.module))
|
||||||
return '%s,%s' % (self.module, path)
|
return '%s,%s' % (self.module, path)
|
||||||
if t in ('char', 'int', 'float'):
|
|
||||||
d = node.text
|
if t == 'char':
|
||||||
if t == 'int':
|
return data
|
||||||
d = d.strip()
|
|
||||||
if d == 'None':
|
if t == 'base64':
|
||||||
return None
|
return data.encode('base64')
|
||||||
else:
|
|
||||||
return int(d.strip())
|
if t == 'int':
|
||||||
elif t == 'float':
|
d = data.strip()
|
||||||
return float(d.strip())
|
if d == 'None':
|
||||||
return d
|
return None
|
||||||
elif t in ('list','tuple'):
|
return int(d)
|
||||||
|
|
||||||
|
if t == 'float':
|
||||||
|
return float(data.strip())
|
||||||
|
|
||||||
|
if t in ('list','tuple'):
|
||||||
res=[]
|
res=[]
|
||||||
for n in node.findall('./value'):
|
for n in node.iterchildren(tag='value'):
|
||||||
res.append(_eval_xml(self,n,pool,cr,uid,idref))
|
res.append(_eval_xml(self,n,pool,cr,uid,idref))
|
||||||
if t=='tuple':
|
if t=='tuple':
|
||||||
return tuple(res)
|
return tuple(res)
|
||||||
|
|
Loading…
Reference in New Issue