|
|
|
|
@ -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} |
|
|
|
|
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: 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']) |
|
|
|
|
# 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 |
|
|
|
|
|