From 5990583af369aad271ba3253832b0eee621b83fb Mon Sep 17 00:00:00 2001 From: Laurent Date: Sat, 22 Feb 2025 11:38:00 +0100 Subject: [PATCH] adds new import view --- tournaments/urls.py | 1 + tournaments/views.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/tournaments/urls.py b/tournaments/urls.py index 3b8e8e3..1be64d4 100644 --- a/tournaments/urls.py +++ b/tournaments/urls.py @@ -66,6 +66,7 @@ urlpatterns = [ path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'), path('profile/', views.ProfileUpdateView.as_view(), name='profile'), path('admin/tournament-import/', views.tournament_import_view, name='tournament_import'), + path('admin/tournament-import-tr/', views.tournament_import_team_reg, name='tournament_import'), path('admin/users-export/', views.UserListExportView.as_view(), name='users_export'), ] diff --git a/tournaments/views.py b/tournaments/views.py index ae0cbb0..45a2842 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -838,6 +838,66 @@ def tournament_import_view(request): else: return render(request, 'tournaments/admin/tournament_cleaner.html') +@staff_member_required +def tournament_import_team_reg(request): + if request.method == 'POST': + zip_file = request.FILES.get('tournament_zip') + if zip_file: + try: + tournament_id = os.path.splitext(zip_file.name)[0] + tournament = Tournament.objects.get(id=tournament_id) + + # Delete existing relationships + # tournament.round_set.all().delete() + # tournament.groupstage_set.all().delete() + # tournament.teamregistration_set.all().delete() + + 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 with preserved UUIDs + # for item in rounds_data: + # item['tournament'] = tournament.id + # 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 + serializer_mapping = { + 'group-stages.json': GroupStageSerializer, + # 'team-registrations.json': TeamRegistrationSerializer, + # 'matches.json': MatchSerializer, + # 'player-registrations.json': PlayerRegistrationSerializer, + # 'team-scores.json': TeamScoreSerializer + } + + # Process each remaining file + for filename, serializer_class in serializer_mapping.items(): + process_file(z, filename, tournament_id, tournament, serializer_class) + + return JsonResponse({'status': 'success'}) + + except Exception as e: + return JsonResponse({'status': 'error', 'message': str(e)}) + else: + return render(request, 'tournaments/admin/tournament_cleaner.html') + def process_file(zip_file, filename, tournament_id, tournament, serializer_class): """Helper function to process individual files""" try: