Skip to content

Commit

Permalink
Fix validation error with valid code. #162 (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo authored Feb 19, 2024
2 parents aaeba7f + 478f76f commit ca1c443
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 154 deletions.
37 changes: 33 additions & 4 deletions codicefiscale/codicefiscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import re
import string
from datetime import datetime
from datetime import datetime, timedelta
from itertools import combinations
from typing import Any, Literal, Pattern

Expand Down Expand Up @@ -116,6 +116,7 @@ def _get_date(
if not date:
return None
if isinstance(date, datetime):
date = date.replace(tzinfo=None)
return date
date_slug = slugify(date)
date_parts = date_slug.split("-")[:3]
Expand All @@ -133,6 +134,7 @@ def _get_date(
date_slug,
parserinfo=date_parser.parserinfo(**date_parser_options),
)
date_obj = date_obj.replace(tzinfo=None)
return date_obj
except ValueError:
return None
Expand All @@ -141,7 +143,7 @@ def _get_date(
def _get_birthplace(
birthplace: str,
birthdate: datetime | str | None = None,
) -> dict[str, dict[str, bool | datetime | str | list[str]]] | None | None:
) -> dict[str, dict[str, Any]] | None:
birthplace_slug = slugify(birthplace)
birthplace_code = birthplace_slug.upper()
birthplaces_options = _DATA["municipalities"].get(
Expand All @@ -160,15 +162,42 @@ def _get_birthplace(
if not birthdate_date:
return birthplaces_options[0].copy()

# search birthplace that has been created before / deleted after birthdate
for birthplace_option in birthplaces_options:
date_created = _get_date(birthplace_option["date_created"]) or datetime.min
date_created = date_created.replace(tzinfo=None)
date_deleted = _get_date(birthplace_option["date_deleted"]) or datetime.max
date_deleted = date_deleted.replace(tzinfo=None)
# print(birthdate_date, date_created, date_deleted)
if birthdate_date >= date_created and birthdate_date <= date_deleted:
return birthplace_option.copy()

return _get_birthplace_fallback(birthplaces_options, birthdate_date)


def _get_birthplace_fallback(
birthplaces_options: list[dict[str, Any]],
birthdate_date: datetime,
) -> dict[str, dict[str, Any]] | None:
# avoid wrong birthplace code error when birthdate falls in
# missing date-range in the data-source even if birthplace code is valid
if len(birthplaces_options) > 1:
for index in range(len(birthplaces_options) - 1):
birthplace_option = birthplaces_options[index]
birthplace_option_next = birthplaces_options[(index + 1)]
date_deleted = _get_date(birthplace_option["date_deleted"])
date_created = _get_date(birthplace_option_next["date_created"])
if date_deleted and date_created:
if birthdate_date >= date_deleted and date_deleted <= date_created:
# the birthdate is in between a deleted munipality and a created one
# if the deleted one has a very short active time delta,
# it means that probably the deleted_at value is wrong
date_created = _get_date(birthplace_option["date_created"])
date_deleted = _get_date(birthplace_option["date_deleted"])
if date_created and date_deleted:
date_delta = date_deleted - date_created
if date_delta <= timedelta(days=1):
return birthplace_option.copy()
return birthplace_option_next.copy()

return None


Expand Down
70 changes: 0 additions & 70 deletions codicefiscale/data/municipalities-patch.json
Original file line number Diff line number Diff line change
@@ -1,72 +1,2 @@
[
{
"active": false,
"code": "F383",
"date_created": "1928-05-12T00:00:00",
"date_deleted": "1991-12-09T00:00:00",
"name": "Monserrato",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"monserrato"
],
"name_trans": "Monserrato",
"province": "CA"
},
{
"active": false,
"code": "F469",
"date_created": "1929-06-02T00:00:00",
"date_deleted": "1946-07-01T00:00:00",
"name": "Montechiaro D'Acqui",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"montechiaro-d-acqui"
],
"name_trans": "Montechiaro D'Acqui",
"province": "AL"
},
{
"active": false,
"code": "G133",
"date_created": "1928-11-28T00:00:00",
"date_deleted": "1948-04-07T00:00:00",
"name": "Ortacesus",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"ortacesus"
],
"name_trans": "Ortacesus",
"province": "CA"
},
{
"active": false,
"code": "H059",
"date_created": "1929-03-26T00:00:00",
"date_deleted": "1947-03-29T00:00:00",
"name": "Priero",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"priero"
],
"name_trans": "Priero",
"province": "CN"
},
{
"active": false,
"code": "L513",
"date_created": "1927-11-04T00:00:00",
"date_deleted": "1946-02-27T00:00:00",
"name": "Ussaramanna",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"ussaramanna"
],
"name_trans": "Ussaramanna",
"province": "CA"
}
]
79 changes: 4 additions & 75 deletions codicefiscale/data/municipalities.json
Original file line number Diff line number Diff line change
Expand Up @@ -3382,7 +3382,7 @@
"active": false,
"code": "A134",
"date_created": "1961-03-09T00:00:00",
"date_deleted": "2023-12-12T00:00:00",
"date_deleted": "2023-11-17T00:00:00",
"name": "Albaredo Arnaboldi",
"name_alt": "",
"name_alt_trans": "",
Expand Down Expand Up @@ -85886,7 +85886,7 @@
"date_created": "2009-01-29T00:00:00",
"date_deleted": "",
"name": "Duino Aurisina-Devin Nabre\u017eina",
"name_alt": "Comune Di Duino Aurisina-Obcina Devin Nabre\u017eina",
"name_alt": "Comune Di Duino Aurisina-Ob\u010dina Devin Nabre\u017eina",
"name_alt_trans": "Comune Di Duino Aurisina-Obcina Devin Nabrezina",
"name_slugs": [
"comune-di-duino-aurisina-obcina-devin-nabrezina",
Expand Down Expand Up @@ -139098,20 +139098,6 @@
"name_trans": "Monserrato",
"province": "CA"
},
{
"active": false,
"code": "F383",
"date_created": "1928-05-12T00:00:00",
"date_deleted": "1991-12-09T00:00:00",
"name": "Monserrato",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"monserrato"
],
"name_trans": "Monserrato",
"province": "CA"
},
{
"active": false,
"code": "F384",
Expand Down Expand Up @@ -142276,20 +142262,6 @@
"name_trans": "Montechiaro D'Acqui",
"province": "AL"
},
{
"active": false,
"code": "F469",
"date_created": "1929-06-02T00:00:00",
"date_deleted": "1946-07-01T00:00:00",
"name": "Montechiaro D'Acqui",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"montechiaro-d-acqui"
],
"name_trans": "Montechiaro D'Acqui",
"province": "AL"
},
{
"active": false,
"code": "F468",
Expand Down Expand Up @@ -158611,20 +158583,6 @@
"name_trans": "Ortacesus",
"province": "SU"
},
{
"active": false,
"code": "G133",
"date_created": "1928-11-28T00:00:00",
"date_deleted": "1948-04-07T00:00:00",
"name": "Ortacesus",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"ortacesus"
],
"name_trans": "Ortacesus",
"province": "CA"
},
{
"active": false,
"code": "G135",
Expand Down Expand Up @@ -183423,20 +183381,6 @@
"name_trans": "Priero",
"province": "CN"
},
{
"active": false,
"code": "H059",
"date_created": "1929-03-26T00:00:00",
"date_deleted": "1947-03-29T00:00:00",
"name": "Priero",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"priero"
],
"name_trans": "Priero",
"province": "CN"
},
{
"active": false,
"code": "H060",
Expand Down Expand Up @@ -204419,7 +204363,7 @@
"date_created": "1923-02-08T00:00:00",
"date_deleted": "",
"name": "San Dorligo Della Valle-Dolina",
"name_alt": "Comune Di San Dorligo Della Valle-Obcina Dolina",
"name_alt": "Comune Di San Dorligo Della Valle-Ob\u010dina Dolina",
"name_alt_trans": "Comune Di San Dorligo Della Valle-Obcina Dolina",
"name_slugs": [
"comune-di-san-dorligo-della-valle-obcina-dolina",
Expand Down Expand Up @@ -204984,10 +204928,9 @@
"date_created": "2009-01-29T00:00:00",
"date_deleted": "",
"name": "San Floriano Del Collio-\u00c5 Teverjan",
"name_alt": "Comune Di San Floriano Del Collio-Obcina \u00c5 Teverjan",
"name_alt": "Comune Di San Floriano Del Collio-Ob\u010dina \u0160teverjan",
"name_alt_trans": "Comune Di San Floriano Del Collio-Obcina Steverjan",
"name_slugs": [
"comune-di-san-floriano-del-collio-obcina-a-teverjan",
"comune-di-san-floriano-del-collio-obcina-steverjan",
"san-floriano-del-collio-a-teverjan",
"san-floriano-del-collio-steverjan"
Expand Down Expand Up @@ -251869,20 +251812,6 @@
"name_trans": "Ussaramanna",
"province": "SU"
},
{
"active": false,
"code": "L513",
"date_created": "1927-11-04T00:00:00",
"date_deleted": "1946-02-27T00:00:00",
"name": "Ussaramanna",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"ussaramanna"
],
"name_trans": "Ussaramanna",
"province": "CA"
},
{
"active": false,
"code": "L514",
Expand Down
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,10 @@ ignore_missing_imports = true
strict = true

[tool.ruff]
ignore = []
line-length = 88
select = ["B", "B9", "C", "E", "F", "W"]

[tool.ruff.mccabe]
max-complexity = 10
lint.ignore = []
lint.mccabe.max-complexity = 10
lint.select = ["B", "B9", "C", "E", "F", "W"]

[tool.setuptools.packages.find]
include = ["codicefiscale*"]
Expand Down
46 changes: 46 additions & 0 deletions tests/test_issue_0162.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import datetime
import unittest

from codicefiscale import codicefiscale


class Issue0162TestCase(unittest.TestCase):
def test_issue_0162(self):
"""
Wrong birthplace code error (missing date-range in the data-source).
"""
self.maxDiff = None
code = "DFLNTN42T20B860H"
self.assertTrue(codicefiscale.is_valid(code))
code_data = codicefiscale.decode(code)
code_data.pop("omocodes")
# print(code_data)
expected_code_data = {
"code": "DFLNTN42T20B860H",
"gender": "M",
"birthdate": datetime.datetime(1942, 12, 20, 0, 0),
"birthplace": {
"active": False,
"code": "B860",
"date_created": "1927-01-12T00:00:00",
"date_deleted": "1928-06-27T00:00:00",
"name": "Casagiove",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": ["casagiove"],
"name_trans": "Casagiove",
"province": "NA",
},
"raw": {
"code": "DFLNTN42T20B860H",
"lastname": "DFL",
"firstname": "NTN",
"birthdate": "42T20",
"birthdate_year": "42",
"birthdate_month": "T",
"birthdate_day": "20",
"birthplace": "B860",
"cin": "H",
},
}
self.assertEqual(code_data, expected_code_data)

0 comments on commit ca1c443

Please sign in to comment.