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
98d47549
Commit
98d47549
authored
3 years ago
by
Florent Aide
Browse files
Options
Downloads
Patches
Plain Diff
add a switch converter
parent
25ab9315
No related branches found
No related tags found
1 merge request
!13
add a switch converter
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
__init__.py
+1
-0
1 addition, 0 deletions
__init__.py
switch.py
+32
-0
32 additions, 0 deletions
switch.py
tests/test_switch.py
+53
-0
53 additions, 0 deletions
tests/test_switch.py
with
86 additions
and
0 deletions
__init__.py
+
1
−
0
View file @
98d47549
...
...
@@ -11,4 +11,5 @@
RelationToManyMap
,
RelationToOne
,
)
from
.switch
import
Switch
# noqa: F401
from
.xref
import
Xref
# noqa: F401
This diff is collapsed.
Click to expand it.
switch.py
0 → 100644
+
32
−
0
View file @
98d47549
from
typing
import
Any
,
Callable
,
List
,
Mapping
,
Optional
,
Tuple
from
odoo
import
models
from
.base
import
ContextBuilder
,
Converter
class
Switch
(
Converter
):
# example usage:
# AURION_REFERENTIAL: Switch(
# [(lambda e: e.is_xxx, DictConverter("__wave__", {})),
# (None, DictConverter("__event__", {}))]
# )
def
__init__
(
self
,
converters
:
List
[
Tuple
[
Callable
,
Converter
]],
context
:
Optional
[
ContextBuilder
]
=
None
,
):
super
()
self
.
_converters
=
converters
self
.
context
=
context
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
):
return
converter
.
odoo_to_message
(
self
,
instance
,
ctx
)
# raise or return None
This diff is collapsed.
Click to expand it.
tests/test_switch.py
0 → 100644
+
53
−
0
View file @
98d47549
##############################################################################
#
# Converter, for Odoo
# Copyright (C) 2021 XCG Consulting <https://odoo.consulting>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from
odoo
import
tests
from
odoo.addons.converter
import
DictConverter
,
Field
,
Switch
class
Test
(
tests
.
TransactionCase
):
def
setUp
(
self
):
super
(
Test
,
self
).
setUp
()
def
test_to_message
(
self
):
# first make sure we have an instance to test on
self
.
assertTrue
(
self
.
env
.
ref
(
"
base.bank_bnp
"
).
active
)
converter
=
Switch
(
[
(
lambda
e
:
e
.
active
,
DictConverter
(
"
__activebanks__
"
,
{
"
name
"
:
Field
(
"
name
"
),
"
active
"
:
Field
(
"
active
"
)},
),
),
(
None
,
DictConverter
(
"
__inactivebanks__
"
,
{
"
name
"
:
Field
(
"
name
"
),
"
active
"
:
Field
(
"
active
"
)},
),
),
]
)
msg
=
converter
.
odoo_to_message
()
self
.
assertEqual
(
"
__activebanks__
"
,
msg
.
get
(
"
type
"
))
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