Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions src/euring/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,37 @@

from __future__ import annotations

from .fields import (
EURING2020_ONLY_KEYS,
EURING_FIELDS,
NON_EURING2000_KEYS,
)
from .fields import EURING2020_ONLY_KEYS, EURING_KEY_NAME, NON_EURING2000_KEYS
from .formats import FORMAT_EURING2000, FORMAT_EURING2000PLUS, FORMAT_EURING2020

_FIELD_NAME_BY_KEY = {field["key"]: field["name"] for field in EURING_FIELDS}


def field_name_for_key(key: str) -> str:
"""Return the field name for a key, falling back to the key."""
return _FIELD_NAME_BY_KEY.get(key, key)
return EURING_KEY_NAME.get(key, key)


def accuracy_is_alpha(values_by_key: dict[str, str]) -> bool:
def accuracy_of_coordinates_is_alpha(values_by_key: dict[str, str]) -> bool:
"""Return True when accuracy_of_coordinates is alphabetic."""
accuracy = values_by_key.get("accuracy_of_coordinates", "")
return bool(accuracy) and accuracy.isalpha()
accuracy_of_coordinates = values_by_key.get("accuracy_of_coordinates", "")
return bool(accuracy_of_coordinates) and accuracy_of_coordinates.isalpha()


def matches_euring2000(values_by_key: dict[str, str]) -> bool:
"""Return True when values fit EURING2000."""
"""Return True when values are present only for fields in EURING2000 format."""
for key in NON_EURING2000_KEYS:
if values_by_key.get(key):
return False
return True


def requires_euring2000plus(values_by_key: dict[str, str]) -> bool:
"""Return True when values require EURING2000+."""
"""Return True when values are present for fields that are not in EURING2000 format."""
return not matches_euring2000(values_by_key)


def requires_euring2020(values_by_key: dict[str, str]) -> bool:
"""Return True when values require EURING2020."""
if accuracy_is_alpha(values_by_key):
if accuracy_of_coordinates_is_alpha(values_by_key):
return True
for key in EURING2020_ONLY_KEYS:
if values_by_key.get(key):
Expand All @@ -58,30 +52,30 @@ def _error(key, message):
}

if format == FORMAT_EURING2020:
geo_value = values_by_key.get("geographical_coordinates", "") or ""
lat_value = values_by_key.get("latitude", "") or ""
lng_value = values_by_key.get("longitude", "") or ""
if lat_value or lng_value:
if geo_value and geo_value != "." * 15:
geographical_coordinates = values_by_key.get("geographical_coordinates", "") or ""
latitude = values_by_key.get("latitude", "") or ""
longitude = values_by_key.get("longitude", "") or ""
if latitude or longitude:
if geographical_coordinates and geographical_coordinates != "." * 15:
errors.append(
_error(
key="geographical_coordinates",
message="When Latitude/Longitude are provided, Geographical Co-ordinates must be 15 dots.",
)
)
if lat_value and not lng_value:
if latitude and not longitude:
errors.append(
_error(key="longitude", message="Longitude is required when Latitude is provided."),
)
if lng_value and not lat_value:
if longitude and not latitude:
errors.append(
_error(
key="latitude",
message="Latitude is required when Longitude is provided.",
)
)
else:
if accuracy_is_alpha(values_by_key):
if accuracy_of_coordinates_is_alpha(values_by_key):
errors.append(
_error(
key="accuracy_of_coordinates",
Expand Down