|
|
|
|
@ -8,6 +8,7 @@ from django.utils.encoding import force_str |
|
|
|
|
from django.utils.http import urlsafe_base64_decode |
|
|
|
|
from django.urls import reverse |
|
|
|
|
from django.conf import settings |
|
|
|
|
from django.db import transaction |
|
|
|
|
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt |
|
|
|
|
from django.contrib.admin.views.decorators import staff_member_required |
|
|
|
|
@ -878,22 +879,50 @@ def tournament_import_view(request): |
|
|
|
|
if request.method == 'POST': |
|
|
|
|
zip_file = request.FILES.get('tournament_zip') |
|
|
|
|
if zip_file: |
|
|
|
|
print('yes') |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
tournament_id = os.path.splitext(zip_file.name)[0] |
|
|
|
|
tournament = Tournament.objects.get(id=tournament_id) |
|
|
|
|
|
|
|
|
|
# Delete existing relationships |
|
|
|
|
print('round delete') |
|
|
|
|
|
|
|
|
|
rounds = Round.objects.filter(tournament=tournament,parent=None) |
|
|
|
|
for round in rounds: |
|
|
|
|
round.delete_dependencies() |
|
|
|
|
for gs in tournament.group_stages.all(): |
|
|
|
|
gs.delete_dependencies() |
|
|
|
|
for tr in tournament.team_registrations.all(): |
|
|
|
|
tr.delete_dependencies() |
|
|
|
|
|
|
|
|
|
# with transaction.atomic(): |
|
|
|
|
# for round in tournament.rounds.all(): |
|
|
|
|
# round.delete() |
|
|
|
|
tournament.rounds.all().delete() |
|
|
|
|
tournament.group_stages.all().delete() |
|
|
|
|
tournament.team_registrations.all().delete() |
|
|
|
|
# tournament.player_registrations.all().delete() |
|
|
|
|
# tournament.matches.all().delete() |
|
|
|
|
# tournament.team_scores.all().delete() |
|
|
|
|
|
|
|
|
|
print(f'round count = {tournament.rounds.count()}') |
|
|
|
|
print(f'group_stages count = {tournament.group_stages.count()}') |
|
|
|
|
print(f'team_registrations count = {tournament.team_registrations.count()}') |
|
|
|
|
|
|
|
|
|
print(f'PR count = {PlayerRegistration.objects.count()}') |
|
|
|
|
print(f'TS count = {TeamScore.objects.count()}') |
|
|
|
|
print(f'M count = {Match.objects.count()}') |
|
|
|
|
|
|
|
|
|
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") |
|
|
|
|
# print(f'rounds count = {len(rounds_data)}') |
|
|
|
|
|
|
|
|
|
if rounds_data: |
|
|
|
|
# First pass: Create rounds with preserved UUIDs |
|
|
|
|
for item in rounds_data: |
|
|
|
|
# print('round #1 create') |
|
|
|
|
item['tournament'] = tournament.id |
|
|
|
|
round_id = item['id'] # Preserve the original UUID |
|
|
|
|
Round.objects.create( |
|
|
|
|
@ -908,6 +937,8 @@ def tournament_import_view(request): |
|
|
|
|
|
|
|
|
|
# Second pass: Set parent relationships |
|
|
|
|
for item in rounds_data: |
|
|
|
|
# print('round #2 set parent') |
|
|
|
|
|
|
|
|
|
if item.get('parent'): |
|
|
|
|
round_obj = Round.objects.get(id=item['id']) |
|
|
|
|
round_obj.parent_id = item['parent'] |
|
|
|
|
@ -915,6 +946,7 @@ def tournament_import_view(request): |
|
|
|
|
|
|
|
|
|
# Then process all other files |
|
|
|
|
serializer_mapping = { |
|
|
|
|
# 'rounds.json': RoundSerializer, |
|
|
|
|
'group-stages.json': GroupStageSerializer, |
|
|
|
|
'team-registrations.json': TeamRegistrationSerializer, |
|
|
|
|
'matches.json': MatchSerializer, |
|
|
|
|
@ -929,6 +961,7 @@ def tournament_import_view(request): |
|
|
|
|
return JsonResponse({'status': 'success'}) |
|
|
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
print(f'error = {str(e)}') |
|
|
|
|
return JsonResponse({'status': 'error', 'message': str(e)}) |
|
|
|
|
else: |
|
|
|
|
return render(request, 'tournaments/admin/tournament_cleaner.html') |
|
|
|
|
@ -936,13 +969,16 @@ def tournament_import_view(request): |
|
|
|
|
def process_file(zip_file, filename, tournament_id, tournament, serializer_class): |
|
|
|
|
"""Helper function to process individual files""" |
|
|
|
|
try: |
|
|
|
|
print(f'process {filename}') |
|
|
|
|
file_path = f"{tournament_id}/{filename}" |
|
|
|
|
json_data = get_file_data(zip_file, file_path) |
|
|
|
|
|
|
|
|
|
if json_data: |
|
|
|
|
# Add tournament to each item |
|
|
|
|
for item in json_data: |
|
|
|
|
# print(f'process {item}') |
|
|
|
|
item['tournament'] = tournament.id |
|
|
|
|
item['store_id'] = str(tournament.id) |
|
|
|
|
|
|
|
|
|
serializer = serializer_class(data=json_data, many=True) |
|
|
|
|
serializer.is_valid(raise_exception=True) |
|
|
|
|
|