Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Converter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
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
Converter
Commits
d5b3bcb01e3a
Commit
d5b3bcb01e3a
authored
2 years ago
by
Vincent Hatakeyama
Browse files
Options
Downloads
Patches
Plain Diff
Update switch converter from afp_sync
parent
b512d148cb78
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!17
Update switch converter from afp_sync
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
switch.py
+104
-12
104 additions, 12 deletions
switch.py
tests/test_switch.py
+6
-0
6 additions, 0 deletions
tests/test_switch.py
with
110 additions
and
12 deletions
switch.py
+
104
−
12
View file @
d5b3bcb0
from
typing
import
Any
,
Callable
,
List
,
Mapping
,
Optional
,
Tuple
from
typing
import
Any
,
Callable
,
Dict
,
List
,
Mapping
,
Optional
,
Tuple
from
odoo
import
api
,
exceptions
,
models
...
...
@@ -2,3 +4,4 @@
from
odoo
import
models
from
.base
import
ContextBuilder
,
Converter
,
Skip
from
.validate
import
NotInitialized
,
Validator
...
...
@@ -4,5 +7,5 @@
from
.base
import
ContextBuilder
,
Converter
import
jsonschema
class
Switch
(
Converter
):
...
...
@@ -12,11 +15,20 @@
The function argument is the model instance.
Example usage:
AURION_REFERENTIAL: Switch(
[(lambda e: e.is_xxx, Model(
"
__wave__
"
, {})),
(None, Model(
"
__event__
"
, {}))]
)
.. code-block:: python
AURION_REFERENTIAL: Switch(
[
(
lambda e: e.is_xxx,
lambda p:
"
wave_code
"
in p,
Model(
"
__wave__
"
, {}),
),
(None, None, Model(
"
__event__
"
, {})),
]
)
"""
def
__init__
(
self
,
...
...
@@ -19,7 +31,14 @@
"""
def
__init__
(
self
,
converters
:
List
[
Tuple
[
Callable
[[
models
.
Model
],
bool
],
Converter
]],
converters
:
List
[
Tuple
[
Callable
[[
models
.
Model
],
bool
],
Callable
[[
Any
],
bool
],
Converter
,
]
],
validator
:
Optional
[
Validator
]
=
None
,
context
:
Optional
[
ContextBuilder
]
=
None
,
):
...
...
@@ -24,6 +43,11 @@
context
:
Optional
[
ContextBuilder
]
=
None
,
):
"""
:param converters: is a 3 tuple composed of:
out condition, in condition, and chosen converter
:param validator:
:param context:
"""
super
()
self
.
_converters
=
converters
self
.
context
=
context
...
...
@@ -27,7 +51,16 @@
super
()
self
.
_converters
=
converters
self
.
context
=
context
if
validator
:
if
validator
.
initialized
:
self
.
validator
=
validator
else
:
raise
NotInitialized
(
"
you must initialize() the validator before passing it
"
)
else
:
self
.
validator
=
None
def
odoo_to_message
(
self
,
instance
:
models
.
Model
,
ctx
:
Optional
[
Mapping
]
=
None
)
->
Any
:
...
...
@@ -30,10 +63,10 @@
def
odoo_to_message
(
self
,
instance
:
models
.
Model
,
ctx
:
Optional
[
Mapping
]
=
None
)
->
Any
:
for
cond
,
converter
in
self
.
_converters
:
if
cond
is
None
or
cond
(
instance
):
for
out_cond
,
_in_
cond
,
converter
in
self
.
_converters
:
if
out_
cond
is
None
or
out_
cond
(
instance
):
return
converter
.
odoo_to_message
(
instance
,
ctx
)
return
None
...
...
@@ -36,5 +69,64 @@
return
converter
.
odoo_to_message
(
instance
,
ctx
)
return
None
# TODO this model is missing the message_to_odoo implementation
def
message_to_odoo
(
self
,
odoo_env
:
api
.
Environment
,
phase
:
str
,
message_value
:
Any
,
instance
:
models
.
Model
,
value_present
:
bool
=
True
,
)
->
Dict
:
for
_out_cond
,
in_cond
,
converter
in
self
.
_converters
:
if
in_cond
is
None
or
in_cond
(
message_value
):
return
converter
.
message_to_odoo
(
odoo_env
,
phase
,
message_value
,
instance
,
value_present
=
value_present
,
)
return
Skip
def
is_instance_getter
(
self
)
->
bool
:
for
_out_cond
,
_in_cond
,
converter
in
self
.
_converters
:
if
converter
.
is_instance_getter
():
return
True
return
False
def
get_instance
(
self
,
odoo_env
,
message_data
):
for
_out_cond
,
in_cond
,
converter
in
self
.
_converters
:
if
in_cond
is
None
or
in_cond
(
message_data
):
return
converter
.
get_instance
(
odoo_env
,
message_data
)
def
odoo_to_message_validate
(
self
,
instance
:
models
.
Model
,
synchronizer
:
Optional
[
models
.
Model
]
=
None
,
validation
:
bool
=
True
,
):
data
=
self
.
odoo_to_message
(
instance
)
for
out_cond
,
_in_cond
,
converter
in
self
.
_converters
:
if
out_cond
is
None
or
out_cond
(
instance
):
if
validation
and
converter
.
_jsonschema
is
not
None
:
if
not
synchronizer
:
raise
exceptions
.
UserError
(
"
missing synchronizer for validation
"
)
if
self
.
validator
is
None
:
self
.
validator
=
synchronizer
.
get_validator
()
if
isinstance
(
converter
.
_jsonschema
,
str
):
try
:
self
.
validator
.
validate
(
converter
.
_jsonschema
,
data
)
except
jsonschema
.
exceptions
.
ValidationError
as
e
:
return
data
,
str
(
e
)
return
data
,
None
This diff is collapsed.
Click to expand it.
tests/test_switch.py
+
6
−
0
View file @
d5b3bcb0
...
...
@@ -22,6 +22,10 @@
from
..
import
Field
,
Model
,
Switch
def
falser
(
*
a
,
**
kw
)
->
bool
:
return
False
class
Test
(
tests
.
TransactionCase
):
def
test_to_message
(
self
):
# first make sure we have an instance to test on
...
...
@@ -31,6 +35,7 @@
[
(
lambda
e
:
e
.
active
,
falser
,
Model
(
"
__activebanks__
"
,
{
"
name
"
:
Field
(
"
name
"
),
"
active
"
:
Field
(
"
active
"
)},
...
...
@@ -38,6 +43,7 @@
),
(
None
,
falser
,
Model
(
"
__inactivebanks__
"
,
{
"
name
"
:
Field
(
"
name
"
),
"
active
"
:
Field
(
"
active
"
)},
...
...
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