From 193ca2c0436c972cb2bc8997df6e75ad02715f6e Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 14 Feb 2025 22:16:26 +0100 Subject: [PATCH] fix tournament import --- tournaments/views.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/tournaments/views.py b/tournaments/views.py index c893323..343acb8 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -792,26 +792,27 @@ def tournament_import_view(request): with zipfile.ZipFile(zip_file) as z: # First, process rounds rounds_data = get_file_data(z, f"{tournament_id}/rounds.json") + rounds_data = get_file_data(z, f"{tournament_id}/rounds.json") if rounds_data: - # First pass: Create rounds without parent relationships - rounds_without_parent = [] + # First pass: Create rounds with preserved UUIDs for item in rounds_data: item['tournament'] = tournament.id - # Temporarily remove parent field - parent = item.pop('parent', None) - rounds_without_parent.append({'data': item, 'parent': parent}) - - serializer = RoundSerializer(data=[item['data'] for item in rounds_without_parent], many=True) - serializer.is_valid(raise_exception=True) - created_rounds = serializer.save() - - # Create a mapping of round IDs - round_mapping = {round_obj.id: round_obj for round_obj in created_rounds} - - # Second pass: Update parent relationships - for round_obj, original_data in zip(created_rounds, rounds_without_parent): - if original_data['parent']: - round_obj.parent = round_mapping.get(original_data['parent']) + round_id = item['id'] # Preserve the original UUID + Round.objects.create( + id=round_id, + tournament_id=tournament.id, + index=item['index'], + format=item.get('format'), + start_date=item.get('start_date'), + group_stage_loser_bracket=item.get('group_stage_loser_bracket', False), + loser_bracket_mode=item.get('loser_bracket_mode', 0) + ) + + # Second pass: Set parent relationships + for item in rounds_data: + if item.get('parent'): + round_obj = Round.objects.get(id=item['id']) + round_obj.parent_id = item['parent'] round_obj.save() # Then process all other files