adds new import view

bracket-feature
Laurent 9 months ago
parent 2d877563ff
commit 5990583af3
  1. 1
      tournaments/urls.py
  2. 60
      tournaments/views.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'),
]

@ -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:

Loading…
Cancel
Save