fix tournament import

bracket-feature
Laurent 9 months ago
parent 508fd8ea31
commit 193ca2c043
  1. 33
      tournaments/views.py

@ -792,26 +792,27 @@ def tournament_import_view(request):
with zipfile.ZipFile(zip_file) as z: with zipfile.ZipFile(zip_file) as z:
# First, process rounds # 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")
rounds_data = get_file_data(z, f"{tournament_id}/rounds.json")
if rounds_data: if rounds_data:
# First pass: Create rounds without parent relationships # First pass: Create rounds with preserved UUIDs
rounds_without_parent = []
for item in rounds_data: for item in rounds_data:
item['tournament'] = tournament.id item['tournament'] = tournament.id
# Temporarily remove parent field round_id = item['id'] # Preserve the original UUID
parent = item.pop('parent', None) Round.objects.create(
rounds_without_parent.append({'data': item, 'parent': parent}) id=round_id,
tournament_id=tournament.id,
serializer = RoundSerializer(data=[item['data'] for item in rounds_without_parent], many=True) index=item['index'],
serializer.is_valid(raise_exception=True) format=item.get('format'),
created_rounds = serializer.save() start_date=item.get('start_date'),
group_stage_loser_bracket=item.get('group_stage_loser_bracket', False),
# Create a mapping of round IDs loser_bracket_mode=item.get('loser_bracket_mode', 0)
round_mapping = {round_obj.id: round_obj for round_obj in created_rounds} )
# Second pass: Update parent relationships # Second pass: Set parent relationships
for round_obj, original_data in zip(created_rounds, rounds_without_parent): for item in rounds_data:
if original_data['parent']: if item.get('parent'):
round_obj.parent = round_mapping.get(original_data['parent']) round_obj = Round.objects.get(id=item['id'])
round_obj.parent_id = item['parent']
round_obj.save() round_obj.save()
# Then process all other files # Then process all other files

Loading…
Cancel
Save