fix(v2-import): namespace source identity for snapshot upserts
This commit is contained in:
@ -96,9 +96,26 @@ def _player_season_source_uid(record: dict[str, Any], source_name: str, snapshot
|
||||
)
|
||||
|
||||
|
||||
def _source_slug(*, source_name: str, base_name: str, fallback_prefix: str, fallback_external_id: str) -> str:
|
||||
base_slug = slugify(base_name) or f"{fallback_prefix}-{fallback_external_id}"
|
||||
source_slug = slugify(source_name) or "snapshot"
|
||||
return f"{source_slug}-{base_slug}"
|
||||
|
||||
|
||||
def _normalized_source_name(source_name: str) -> str:
|
||||
return source_name.strip().lower()
|
||||
|
||||
|
||||
def _upsert_record(record: dict[str, Any], *, source_name: str, snapshot_date: date) -> None:
|
||||
competition_slug = slugify(record["competition_name"]) or f"competition-{record['competition_external_id']}"
|
||||
source_key = _normalized_source_name(source_name)
|
||||
competition_slug = _source_slug(
|
||||
source_name=source_key,
|
||||
base_name=record["competition_name"],
|
||||
fallback_prefix="competition",
|
||||
fallback_external_id=record["competition_external_id"],
|
||||
)
|
||||
competition, _ = Competition.objects.update_or_create(
|
||||
source_name=source_key,
|
||||
source_uid=record["competition_external_id"],
|
||||
defaults={
|
||||
"name": record["competition_name"],
|
||||
@ -119,8 +136,14 @@ def _upsert_record(record: dict[str, Any], *, source_name: str, snapshot_date: d
|
||||
},
|
||||
)
|
||||
|
||||
team_slug = slugify(record["team_name"]) or f"team-{record['team_external_id']}"
|
||||
team_slug = _source_slug(
|
||||
source_name=source_key,
|
||||
base_name=record["team_name"],
|
||||
fallback_prefix="team",
|
||||
fallback_external_id=record["team_external_id"],
|
||||
)
|
||||
team, _ = Team.objects.update_or_create(
|
||||
source_name=source_key,
|
||||
source_uid=record["team_external_id"],
|
||||
defaults={
|
||||
"name": record["team_name"],
|
||||
@ -141,6 +164,7 @@ def _upsert_record(record: dict[str, Any], *, source_name: str, snapshot_date: d
|
||||
)
|
||||
|
||||
player, _ = Player.objects.update_or_create(
|
||||
source_name=source_key,
|
||||
source_uid=record["player_external_id"],
|
||||
defaults={
|
||||
"first_name": record["first_name"],
|
||||
@ -157,7 +181,7 @@ def _upsert_record(record: dict[str, Any], *, source_name: str, snapshot_date: d
|
||||
)
|
||||
|
||||
player_season, _ = PlayerSeason.objects.update_or_create(
|
||||
source_uid=_player_season_source_uid(record, source_name=source_name, snapshot_date=snapshot_date),
|
||||
source_uid=_player_season_source_uid(record, source_name=source_key, snapshot_date=snapshot_date),
|
||||
defaults={
|
||||
"player": player,
|
||||
"season": season,
|
||||
|
||||
Reference in New Issue
Block a user