fix tournament import

sync_v2
Laurent 8 months ago
parent 59318f1fd1
commit 6c9bc3c7f0
  1. 8
      sync/signals.py
  2. 38
      tournaments/views.py

@ -62,9 +62,9 @@ def synchronization_notifications(sender, instance, created=False, **kwargs):
process_foreign_key_changes(sender, instance, **kwargs) process_foreign_key_changes(sender, instance, **kwargs)
signal = kwargs.get('signal') # signal = kwargs.get('signal')
save_model_log_if_possible(instance, signal, created) # save_model_log_if_possible(instance, signal, created)
notify_impacted_users(instance) # notify_impacted_users(instance)
def notify_impacted_users(instance): def notify_impacted_users(instance):
user_ids = set() user_ids = set()
@ -251,7 +251,7 @@ def related_users(instance):
# look in hierarchy # look in hierarchy
related_instances = instance.related_instances() related_instances = instance.related_instances()
print(f'related_instances = {related_instances}') # print(f'related_instances = {related_instances}')
related_users = [ri.related_user for ri in related_instances if isinstance(ri, BaseModel)] related_users = [ri.related_user for ri in related_instances if isinstance(ri, BaseModel)]
users.update(related_users) users.update(related_users)

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

Loading…
Cancel
Save