diff --git a/Taskfile.yml b/Taskfile.yml index 850e924..19949a6 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -14,6 +14,31 @@ tasks: - uv sync - "{{.PYTHON}} manage.py migrate" + migrate: + desc: Применить миграции Django + cmds: + - "{{.PYTHON}} manage.py migrate {{.CLI_ARGS}}" + + makemigrations: + desc: Создать миграции Django + cmds: + - "{{.PYTHON}} manage.py makemigrations {{.CLI_ARGS}}" + + django: + desc: Запустить Django-сервер + cmds: + - "{{.PYTHON}} manage.py runserver" + + vite: + desc: Запустить Vite + dir: client + cmds: + - npm run dev + + dev: + desc: Запустить Django-сервер и Vite + deps: [django, vite] + superuser: desc: Создать суперпользователя (пароль передаётся в DJANGO_SUPERUSER_PASSWORD) requires: diff --git a/address/management/commands/import_addresses.py b/address/management/commands/import_addresses.py index 3d99f86..eb54abd 100644 --- a/address/management/commands/import_addresses.py +++ b/address/management/commands/import_addresses.py @@ -57,6 +57,9 @@ class Command(BaseCommand): for _, element in ElementTree.iterparse(path, events=("end",)): if element.tag.rsplit("}", 1)[-1] != "OBJECT": continue + if element.attrib.get("ISACTUAL") != "1": + element.clear() + continue objects.append(self._make_object(element.attrib, region_code)) element.clear() if len(objects) >= batch_size: @@ -87,7 +90,7 @@ class Command(BaseCommand): type_name=attributes["TYPENAME"].strip(), level=int(attributes["LEVEL"]), region_code=region_code, - is_active=attributes["ISACTUAL"] == "1" and attributes["ISACTIVE"] == "1", + is_active=attributes["ISACTIVE"] == "1", ) except (TypeError, ValueError) as error: record_id = attributes.get("ID", "неизвестен") @@ -95,6 +98,7 @@ class Command(BaseCommand): @staticmethod def _save(objects: list[AddressObject], batch_size: int) -> None: + objects = list({obj.fias_id: obj for obj in objects}.values()) AddressObject.objects.bulk_create( objects, batch_size=batch_size, diff --git a/address/tests/test_import_addresses.py b/address/tests/test_import_addresses.py index 1bad385..2cf6c3a 100644 --- a/address/tests/test_import_addresses.py +++ b/address/tests/test_import_addresses.py @@ -30,7 +30,7 @@ def test_imports_and_updates_fias_xml(tmp_path): assert city.name == "Владивосток" assert city.region_code == "25" assert city.is_active is True - assert AddressObject.objects.get(object_id=101).is_active is False + assert AddressObject.objects.count() == 1 xml.write_text( """ @@ -44,7 +44,31 @@ def test_imports_and_updates_fias_xml(tmp_path): city.refresh_from_db() assert city.name == "Владивосток обновлённый" - assert AddressObject.objects.count() == 2 + assert AddressObject.objects.count() == 1 + + +@pytest.mark.django_db +def test_keeps_last_record_when_fias_id_repeats_in_one_batch(tmp_path): + region_dir = tmp_path / "25" + region_dir.mkdir() + xml = region_dir / "AS_ADDR_OBJ.xml" + xml.write_text( + """ + + + +""", + encoding="utf-8", + ) + + call_command("import_addresses", xml, batch_size=10) + + city = AddressObject.objects.get(fias_id=UUID("11111111-1111-1111-1111-111111111111")) + assert AddressObject.objects.count() == 1 + assert city.name == "Владивосток" + assert city.is_active is True @pytest.mark.django_db