Raz 8 months ago
commit 6a86f8121a
  1. 3
      authentication/views.py
  2. 8
      sync/signals.py
  3. 2
      tournaments/admin.py
  4. 2
      tournaments/models/group_stage.py
  5. 2
      tournaments/models/tournament.py
  6. 38
      tournaments/views.py

@ -4,6 +4,7 @@ from django.contrib.auth import authenticate
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
@ -17,7 +18,7 @@ from .models import Device, LoginLog
from .serializers import ChangePasswordSerializer from .serializers import ChangePasswordSerializer
CustomUser=settings.AUTH_USER_MODEL CustomUser=get_user_model()
@method_decorator(csrf_exempt, name='dispatch') @method_decorator(csrf_exempt, name='dispatch')
class CustomAuthToken(APIView): class CustomAuthToken(APIView):

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

@ -13,7 +13,7 @@ class CustomUserAdmin(UserAdmin):
form = CustomUserChangeForm form = CustomUserChangeForm
add_form = CustomUserCreationForm add_form = CustomUserCreationForm
model = CustomUser model = CustomUser
list_display = ['email', 'first_name', 'last_name', 'latest_event_club_name', 'username', 'is_active', 'date_joined', 'event_count', 'origin'] list_display = ['email', 'first_name', 'last_name', 'username', 'date_joined', 'latest_event_club_name', 'is_active', 'event_count', 'origin']
list_filter = ['is_active', 'origin'] list_filter = ['is_active', 'origin']
ordering = ['-date_joined'] ordering = ['-date_joined']
fieldsets = [ fieldsets = [

@ -166,7 +166,7 @@ class GroupStage(SideStoreModel):
class LiveGroupStage: class LiveGroupStage:
def __init__(self, id, title, step, index): def __init__(self, id, title, step, index):
self.id = id self.id = str(id)
self.title = title self.title = title
self.teams = [] self.teams = []
self.start = None self.start = None

@ -1405,7 +1405,7 @@ class Tournament(BaseModel):
return serializable_match_groups return serializable_match_groups
def has_bracket(self): def has_bracket(self):
main_rounds = self.round_set.filter( main_rounds = self.rounds.filter(
parent=None, parent=None,
group_stage_loser_bracket=False group_stage_loser_bracket=False
) )

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