# HG changeset patch
# User Houzefa Abbasbhay <houzefa.abba@xcg-consulting.fr>
# Date 1640790846 -3600
#      Wed Dec 29 16:14:06 2021 +0100
# Node ID 27db41a41e3f84d21fd5fdbb51b42b16a6332563
# Parent  591715545a0cd8c8e768e2c9a9be7bfff433f4a0
import_jsonrpc: Add --allow-missing-refs

diff --git a/NEWS.rst b/NEWS.rst
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -11,6 +11,8 @@
 
 ``docker_build`` also tag with any current tag if any.
 
+``import_jsonrpc``: Add ``--allow-missing-refs`` to log warnings on missing XMLIDs instead of stopping with a blocking error.
+
 13.3.0
 ------
 
diff --git a/odoo_scripts/import_jsonrpc.py b/odoo_scripts/import_jsonrpc.py
--- a/odoo_scripts/import_jsonrpc.py
+++ b/odoo_scripts/import_jsonrpc.py
@@ -20,9 +20,9 @@
 
 _logger = logging.getLogger(__name__)
 
-__version__ = "1.0.2"
+__version__ = "1.1.0"
 __date__ = "2020-02-17"
-__updated__ = "2020-09-11"
+__updated__ = "2021-12-29"
 
 
 def import_with_jsonrpc(
@@ -36,6 +36,7 @@
     model_filenames: List[Dict[str, str]],
     delimiter: str = ",",
     extra_context: Dict[str, str] = dict(),
+    allow_missing_refs: bool = False,
 ) -> int:
     o, session_id = odoo_login(
         database, host, login, password, port, protocol, timeout
@@ -51,12 +52,18 @@
 
     xmlid_cache: Dict[str, int] = dict()
 
-    def _ref(xmlid: str, raise_if_not_found: bool = True) -> int:
+    def _ref(xmlid: str, raise_if_not_found: bool = True) -> Union[bool, int]:
         if xmlid not in xmlid_cache:
             full_xmlid = xmlid if "." in xmlid else "." + xmlid
-            xmlid_cache[xmlid] = imd.xmlid_to_res_id(
-                full_xmlid, raise_if_not_found
-            )
+            try:
+                record_id = imd.xmlid_to_res_id(full_xmlid, raise_if_not_found)
+            except RPCError as error:
+                if allow_missing_refs:
+                    _logger.warning(str(error))
+                    record_id = False
+                else:
+                    raise
+            xmlid_cache[xmlid] = record_id
         return xmlid_cache[xmlid]
 
     def _get_imf(model: str, field_name: str):
@@ -310,6 +317,14 @@
     parser.add_argument(
         "--context", help="Extra context (key=value)", action="append"
     )
+    parser.add_argument(
+        "--allow-missing-refs",
+        help=(
+            "Log warnings on missing XMLIDs instead of stopping with a "
+            "blocking error."
+        ),
+        action="store_true",
+    )
 
     nmspc = parser.parse_args(argv)
     apply(nmspc)
@@ -333,6 +348,7 @@
         ),
         delimiter=nmspc.delimiter,
         extra_context=extra_context,
+        allow_missing_refs=nmspc.allow_missing_refs,
     )