Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Xbus emitter
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
XCG
Odoo modules
Xbus emitter
Commits
03ec3f7be6b7
Commit
03ec3f7be6b7
authored
6 years ago
by
Christophe de Vienne
Browse files
Options
Downloads
Patches
Plain Diff
export: fix custom on relation, add SKIP support
parent
68aae895da8b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
export.py
+22
-13
22 additions, 13 deletions
export.py
tests/test_export.py
+32
-2
32 additions, 2 deletions
tests/test_export.py
with
54 additions
and
15 deletions
export.py
+
22
−
13
View file @
03ec3f7b
...
...
@@ -2,6 +2,9 @@
"""
SKIP
=
object
()
def
export
(
record
,
mapping
=
None
):
"""
Export a odoo record to a dictionnary of basic types, list and dict
...
...
@@ -62,6 +65,7 @@
):
raise
ValueError
(
"'
mapping
'
should be defined too
"
)
value_datatype
=
"
many2one
"
value
=
record
if
source
is
not
None
:
...
...
@@ -71,9 +75,10 @@
for
i
,
fname
in
enumerate
(
path
):
field
=
fields
[
fname
]
value_datatype
=
field
[
"
type
"
]
value
=
getattr
(
value
,
fname
)
if
(
field
[
"
type
"
]
in
(
"
many2one
"
,
"
many2many
"
,
"
one2many
"
)
and
len
(
value
)
==
0
):
...
...
@@ -74,14 +79,13 @@
value
=
getattr
(
value
,
fname
)
if
(
field
[
"
type
"
]
in
(
"
many2one
"
,
"
many2many
"
,
"
one2many
"
)
and
len
(
value
)
==
0
):
if
i
!=
path_len
-
1
:
value
=
None
value
=
None
break
if
field
[
"
type
"
]
in
(
"
char
"
,
"
text
"
)
and
value
is
False
:
value
=
None
break
...
...
@@ -82,8 +86,9 @@
break
if
field
[
"
type
"
]
in
(
"
char
"
,
"
text
"
)
and
value
is
False
:
value
=
None
break
datatype
=
field
[
"
type
"
]
if
datatype
is
None
:
datatype
=
value_datatype
...
...
@@ -89,4 +94,7 @@
if
custom
is
not
None
:
value
=
custom
(
value
)
if
custom
is
not
None
and
value
is
not
None
:
if
value_datatype
in
(
"
one2many
"
,
"
many2many
"
):
value
=
[
custom
(
item
)
for
item
in
value
]
else
:
value
=
custom
(
value
)
...
...
@@ -92,5 +100,5 @@
if
mapping
is
not
None
and
value
is
not
None
:
islist
=
len
(
value
)
>
1
if
mapping
and
value
is
not
SKIP
:
islist
=
value
and
len
(
value
)
>
1
if
datatype
==
"
many2one
"
:
...
...
@@ -95,8 +103,8 @@
if
datatype
==
"
many2one
"
:
if
len
(
value
)
!=
0
:
if
value
:
value
.
ensure_one
()
elif
datatype
in
(
"
one2many
"
,
"
many2many
"
):
islist
=
True
if
islist
:
...
...
@@ -98,7 +106,9 @@
value
.
ensure_one
()
elif
datatype
in
(
"
one2many
"
,
"
many2many
"
):
islist
=
True
if
islist
:
value
=
[
export
(
item
,
mapping
)
for
item
in
value
]
value
=
(
[
export
(
item
,
mapping
)
for
item
in
value
]
if
value
else
[]
)
else
:
...
...
@@ -104,6 +114,4 @@
else
:
if
len
(
value
)
==
0
:
value
=
None
else
:
if
value
is
not
None
:
value
=
export
(
value
,
mapping
)
...
...
@@ -108,5 +116,6 @@
value
=
export
(
value
,
mapping
)
output
[
key
]
=
value
if
value
is
not
SKIP
:
output
[
key
]
=
value
return
output
This diff is collapsed.
Click to expand it.
tests/test_export.py
+
32
−
2
View file @
03ec3f7b
import
odoo
from
..export
import
export
from
..export
import
SKIP
,
export
from
.util.odoo_tests
import
TestBase
...
...
@@ -154,7 +154,37 @@
record
=
self
.
env
[
"
res.partner
"
].
create
({
"
name
"
:
"
my name
"
})
self
.
assertEqual
(
{
"
name
"
:
"
my namemy name
"
},
export
(
record
,
{
"
name
"
:
{
"
source
"
:
"
name
"
,
"
custom
"
:
lambda
s
:
s
*
2
}}),
export
(
record
,
{
"
name
"
:
{
"
source
"
:
"
name
"
,
"
custom
"
:
lambda
s
:
s
*
2
}}
),
)
def
test_export_custom_skip
(
self
):
record
=
self
.
env
[
"
res.partner
"
].
create
({
"
name
"
:
"
my name
"
})
self
.
assertEqual
(
{},
export
(
record
,
{
"
name
"
:
{
"
source
"
:
"
name
"
,
"
custom
"
:
lambda
s
:
SKIP
}}
),
)
def
test_export_custom_with_relation_source
(
self
):
mapping
=
{
"
parent_name
"
:
{
"
source
"
:
"
parent_id
"
,
"
custom
"
:
lambda
p
:
p
.
name
,
"
datatype
"
:
"
char
"
,
}
}
record
=
self
.
env
[
"
res.partner
"
].
create
({
"
name
"
:
"
my name
"
})
self
.
assertEqual
({
"
parent_name
"
:
None
},
export
(
record
,
mapping
))
parent
=
self
.
env
[
"
res.partner
"
].
create
({
"
name
"
:
"
the parent name
"
})
record
.
parent_id
=
parent
.
id
self
.
assertEqual
(
{
"
parent_name
"
:
"
the parent name
"
},
export
(
record
,
mapping
)
)
def
test_export_nested_fields
(
self
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment