Fix owners not getting imported correctly
This commit is contained in:
parent
026bc57f77
commit
df2941cdbc
7 changed files with 138 additions and 9 deletions
|
|
@ -432,25 +432,34 @@ class Command(BaseCommand):
|
|||
def _get_or_create_game(
|
||||
self,
|
||||
game_data: GameSchema,
|
||||
org_obj: Organization,
|
||||
campaign_org_obj: Organization,
|
||||
) -> Game:
|
||||
"""Get or create a game from cache or database.
|
||||
"""Get or create a game from cache or database, using correct owner organization.
|
||||
|
||||
Args:
|
||||
game_data: Game data from Pydantic model.
|
||||
org_obj: Organization that owns this game.
|
||||
campaign_org_obj: Organization that owns the campaign (fallback).
|
||||
|
||||
Returns:
|
||||
Game instance.
|
||||
"""
|
||||
# Determine correct owner organization for the game
|
||||
owner_org_obj = campaign_org_obj
|
||||
if hasattr(game_data, "owner_organization") and game_data.owner_organization:
|
||||
owner_org_data = game_data.owner_organization
|
||||
if isinstance(owner_org_data, dict):
|
||||
# Convert dict to OrganizationSchema
|
||||
owner_org_data = OrganizationSchema.model_validate(owner_org_data)
|
||||
owner_org_obj = self._get_or_create_organization(owner_org_data)
|
||||
|
||||
if game_data.twitch_id in self.game_cache:
|
||||
game_obj: Game = self.game_cache[game_data.twitch_id]
|
||||
|
||||
update_fields: list[str] = []
|
||||
|
||||
# Ensure owner is correct without triggering a read
|
||||
if game_obj.owner_id != org_obj.pk: # type: ignore[attr-defined]
|
||||
game_obj.owner = org_obj
|
||||
if game_obj.owner_id != owner_org_obj.pk: # type: ignore[attr-defined]
|
||||
game_obj.owner = owner_org_obj
|
||||
update_fields.append("owner")
|
||||
|
||||
# Persist normalized display name when provided
|
||||
|
|
@ -485,7 +494,7 @@ class Command(BaseCommand):
|
|||
"name": game_data.name or "",
|
||||
"slug": game_data.slug or "",
|
||||
"box_art": game_data.box_art_url or "",
|
||||
"owner": org_obj,
|
||||
"owner": owner_org_obj,
|
||||
},
|
||||
)
|
||||
if created:
|
||||
|
|
@ -631,7 +640,7 @@ class Command(BaseCommand):
|
|||
|
||||
game_obj: Game = self._get_or_create_game(
|
||||
game_data=drop_campaign.game,
|
||||
org_obj=org_obj,
|
||||
campaign_org_obj=org_obj,
|
||||
)
|
||||
|
||||
start_at_dt: datetime | None = parse_date(drop_campaign.start_at)
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ class GameSchema(BaseModel):
|
|||
slug: str | None = None # Present in Inventory format
|
||||
name: str | None = None # Present in Inventory format (alternative to displayName)
|
||||
type_name: Literal["Game"] = Field(alias="__typename") # Present in both formats
|
||||
owner_organization: dict | None = Field(default=None, alias="ownerOrganization")
|
||||
|
||||
model_config = {
|
||||
"extra": "forbid",
|
||||
|
|
@ -58,7 +59,6 @@ class GameSchema(BaseModel):
|
|||
"""
|
||||
if isinstance(data, dict) and "displayName" not in data and "name" in data:
|
||||
data["displayName"] = data["name"]
|
||||
|
||||
return data
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ class RSSFeedTestCase(TestCase):
|
|||
game=self.game,
|
||||
start_at=timezone.now(),
|
||||
end_at=timezone.now() + timedelta(days=7),
|
||||
operation_name="DropCampaignDetails",
|
||||
)
|
||||
|
||||
def test_organization_feed(self) -> None:
|
||||
|
|
@ -94,6 +95,7 @@ class RSSFeedTestCase(TestCase):
|
|||
game=other_game,
|
||||
start_at=timezone.now(),
|
||||
end_at=timezone.now() + timedelta(days=7),
|
||||
operation_name="DropCampaignDetails",
|
||||
)
|
||||
|
||||
# Get feed for first game
|
||||
|
|
@ -126,6 +128,7 @@ class RSSFeedTestCase(TestCase):
|
|||
game=other_game,
|
||||
start_at=timezone.now(),
|
||||
end_at=timezone.now() + timedelta(days=7),
|
||||
operation_name="DropCampaignDetails",
|
||||
)
|
||||
|
||||
# Get feed for first organization
|
||||
|
|
|
|||
75
twitch/tests/test_game_owner_organization.py
Normal file
75
twitch/tests/test_game_owner_organization.py
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from twitch.management.commands.better_import_drops import Command
|
||||
from twitch.models import Game
|
||||
from twitch.models import Organization
|
||||
|
||||
|
||||
class GameOwnerOrganizationTests(TestCase):
|
||||
"""Tests for correct precedence of game owner organization during import."""
|
||||
|
||||
def test_game_owner_organization_precedence(self) -> None:
|
||||
"""If both owner and ownerOrganization are present, game owner should be ownerOrganization."""
|
||||
command = Command()
|
||||
command.pre_fill_cache()
|
||||
|
||||
payload = {
|
||||
"data": {
|
||||
"user": {
|
||||
"id": "17658559",
|
||||
"dropCampaign": {
|
||||
"id": "test-campaign-1",
|
||||
"name": "Rustmas 2025",
|
||||
"description": "Test campaign desc",
|
||||
"startAt": "2025-12-08T18:00:00Z",
|
||||
"endAt": "2026-01-01T07:59:59.999Z",
|
||||
"accountLinkURL": "https://www.twitch.tv/",
|
||||
"detailsURL": "https://help.twitch.tv/s/article/twitch-chat-badges-guide",
|
||||
"imageURL": "https://static-cdn.jtvnw.net/twitch-quests-assets/CAMPAIGN/495ebb6b-8134-4e51-b9d0-1f4a221b4f8d.png",
|
||||
"status": "ACTIVE",
|
||||
"self": {"isAccountConnected": True, "__typename": "DropCampaignSelfEdge"},
|
||||
"game": {
|
||||
"id": "263490",
|
||||
"slug": "rust",
|
||||
"displayName": "Rust",
|
||||
"__typename": "Game",
|
||||
"ownerOrganization": {
|
||||
"id": "d32de13d-937e-4196-8198-1a7f875f295a",
|
||||
"name": "Twitch Gaming",
|
||||
"__typename": "Organization",
|
||||
},
|
||||
},
|
||||
"owner": {"id": "other-org-id", "name": "Other Org", "__typename": "Organization"},
|
||||
"timeBasedDrops": [],
|
||||
"eventBasedDrops": [],
|
||||
"allow": {"channels": None, "isEnabled": False, "__typename": "DropCampaignACL"},
|
||||
"__typename": "DropCampaign",
|
||||
},
|
||||
"__typename": "User",
|
||||
},
|
||||
},
|
||||
"extensions": {"operationName": "DropCampaignDetails"},
|
||||
}
|
||||
|
||||
# Run import logic
|
||||
success, broken_dir = command.process_responses(
|
||||
responses=[payload],
|
||||
file_path=Path("test_owner_org.json"),
|
||||
options={},
|
||||
)
|
||||
assert success is True
|
||||
assert broken_dir is None
|
||||
|
||||
# Check game owner is Twitch Gaming, not Other Org
|
||||
game: Game = Game.objects.get(twitch_id="263490")
|
||||
org: Organization = Organization.objects.get(twitch_id="d32de13d-937e-4196-8198-1a7f875f295a")
|
||||
assert game.owner == org
|
||||
assert game.owner
|
||||
assert game.owner.name == "Twitch Gaming"
|
||||
|
||||
# Check both organizations exist
|
||||
Organization.objects.get(twitch_id="other-org-id")
|
||||
|
|
@ -42,6 +42,7 @@ class TestSearchView:
|
|||
name="Test Campaign",
|
||||
description="A test campaign",
|
||||
game=game,
|
||||
operation_name="DropCampaignDetails",
|
||||
)
|
||||
drop: TimeBasedDrop = TimeBasedDrop.objects.create(
|
||||
twitch_id="1011",
|
||||
|
|
@ -262,6 +263,7 @@ class TestChannelListView:
|
|||
twitch_id=f"campaign{i}",
|
||||
name=f"Campaign {i}",
|
||||
game=game,
|
||||
operation_name="DropCampaignDetails",
|
||||
)
|
||||
campaign.allow_channels.add(channel)
|
||||
campaigns.append(campaign)
|
||||
|
|
@ -350,6 +352,7 @@ class TestChannelListView:
|
|||
twitch_id=f"campaign_ch2_{i}",
|
||||
name=f"Campaign Ch2 {i}",
|
||||
game=game,
|
||||
operation_name="DropCampaignDetails",
|
||||
)
|
||||
campaign.allow_channels.add(channel2)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue