Repository: fabiocaccamo/python-codicefiscale

Issue:
Unactive municipalities cannot be retrieved
**Python version**
3.9.9

**Package version**
0.4.0

**Current behavior (bug description)**
Decoding does not work for inactive municipalities, since only active ones are considered in
```
for municipality in municipalities:
   if not municipality["active"]:
      continue
```

**Expected behavior**
inactive municipalities data should be considered.
When multiple entries are available, take the one  where  **birthday** falls within **date_created** and **date_deleted**


Comments:

Patch:
diff --git a/codicefiscale/codicefiscale.py b/codicefiscale/codicefiscale.py
index 5b0ddc7..0539705 100644
--- a/codicefiscale/codicefiscale.py
+++ b/codicefiscale/codicefiscale.py
@@ -90,28 +90,24 @@ def _get_indexed_data():
     }
 
     for municipality in municipalities:
-        if not municipality["active"]:
-            continue
         code = municipality["code"]
         province = municipality["province"].lower()
         names = municipality["name_slugs"]
         for name in names:
             data["municipalities"][name] = municipality
             data["municipalities"][name + "-" + province] = municipality
-        assert (
-            code not in data["codes"]
-        ), "Found more than one country/municipality with the same code, expected a one-to-one relation."
-        data["codes"][code] = municipality
+        if code not in data["codes"]:
+            data["codes"][code] = []
+        data["codes"][code].append(municipality)
 
     for country in countries:
         code = country["code"]
         names = country["name_slugs"]
         for name in names:
             data["countries"][name] = country
-        assert (
-            code not in data["codes"]
-        ), "Found more than one country/municipality with the same code, expected a one-to-one relation."
-        data["codes"][code] = country
+        if code not in data["codes"]:
+            data["codes"][code] = []
+        data["codes"][code].append(country)
 
     return data
 
@@ -262,10 +258,14 @@ def encode_birthplace(birthplace):
         birthplace_data = _DATA["municipalities"].get(
             birthplace_slug,
             _DATA["countries"].get(
-                birthplace_slug, _DATA["codes"].get(birthplace_code, {})
+                birthplace_slug
             ),
         )
-        return birthplace_data.get("code", "")
+        if birthplace_data:
+            return birthplace_data.get("code", "")
+        if birthplace_code in _DATA["codes"]:
+            return birthplace_code
+        return ""
 
     birthplace_code = find_birthplace_code(birthplace) or find_birthplace_code(
         re.split(r",|\(", birthplace)[0]
@@ -409,9 +409,22 @@ def decode(code):
     except ValueError:
         raise ValueError(f"[codicefiscale] invalid date: {birthdate_str}")
 
-    birthplace = _DATA["codes"].get(
-        raw["birthplace"][0] + raw["birthplace"][1:].translate(_OMOCODIA_DECODE_TRANS)
-    )
+    codes = _DATA["codes"].get(raw["birthplace"][0] + raw["birthplace"][1:].translate(_OMOCODIA_DECODE_TRANS))
+    birthplace = None
+    for c in codes:
+        date_created = datetime.min
+        try:
+            date_created = datetime.strptime(c['date_created'], "%Y-%m-%d")
+        except ValueError:
+            date_created = datetime.min
+
+        try:
+            date_deleted = datetime.strptime(c['date_deleted'], "%Y-%m-%d")
+        except ValueError:
+            date_deleted = datetime.max
+        if date_created <= birthdate and date_deleted >= birthdate:
+            birthplace = c
+            break
 
     cin = raw["cin"]
     cin_check = encode_cin(code)


Test patch:
diff --git a/tests/tests.py b/tests/tests.py
index 750c0f8..2431241 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -436,15 +436,21 @@ class CodiceFiscaleTestCase(unittest.TestCase):
                 "input": "RSSMRA68A01H501Y",
                 "result": {"sex": "M", "birthdate": "01/01/1968", "birthplace": "Roma"},
             },
+            {
+                "input": "RSSMRA50B17A558W",
+                "result": {
+                    "sex": "M",
+                    "birthdate": "17/02/1950",
+                    "birthplace": "Porretta Terme",
+                },
+            },
         ]
 
         for obj in data:
             # with self.subTest(obj=obj):
-
             result = obj["result"]
             obj_decoded = codicefiscale.decode(obj["input"])
             #  print(obj_decoded)
-
             sex = obj_decoded.get("sex")
             self.assertFalse(sex is None)
             self.assertEqual(sex, result["sex"])