]> git.ipfire.org Git - location/libloc.git/commitdiff
importer: Write NULL into database when bool is not set
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 Jun 2020 13:34:26 +0000 (13:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 Jun 2020 13:34:26 +0000 (13:34 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/location-importer.in

index 05324b74abfab5eb4f351dcb17089721ba8f8fbf..63df5e82969515e8c248078acd54d556babe35be 100644 (file)
@@ -649,9 +649,9 @@ class CLI(object):
                                                                ON CONFLICT (network) DO NOTHING""",
                                                                "%s" % network,
                                                                block.get("country"),
-                                                               block.get("is-anonymous-proxy") == "yes",
-                                                               block.get("is-satellite-provider") == "yes",
-                                                               block.get("is-anycast") == "yes",
+                                                               self._parse_bool(block, "is-anonymous-proxy"),
+                                                               self._parse_bool(block, "is-satellite-provider"),
+                                                               self._parse_bool(block, "is-anycast"),
                                                        )
 
                                                elif type == "aut-num":
@@ -678,14 +678,36 @@ class CLI(object):
                                                                autnum,
                                                                block.get("name"),
                                                                block.get("country"),
-                                                               block.get("is-anonymous-proxy") == "yes",
-                                                               block.get("is-satellite-provider") == "yes",
-                                                               block.get("is-anycast") == "yes",
+                                                               self._parse_bool(block, "is-anonymous-proxy"),
+                                                               self._parse_bool(block, "is-satellite-provider"),
+                                                               self._parse_bool(block, "is-anycast"),
                                                        )
 
                                                else:
                                                        log.warning("Unsupport type: %s" % type)
 
+       @staticmethod
+       def _parse_bool(block, key):
+               val = block.get(key)
+
+               # There is no point to proceed when we got None
+               if val is None:
+                       return
+
+               # Convert to lowercase
+               val = val.lower()
+
+               # True
+               if val in ("yes", "1"):
+                       return True
+
+               # False
+               if val in ("no", "0"):
+                       return False
+
+               # Default to None
+               return None
+
        def handle_import_countries(self, ns):
                with self.db.transaction():
                        # Drop all data that we have