diff --git a/tournaments/apps.py b/tournaments/apps.py index b7a4603..1c6c698 100644 --- a/tournaments/apps.py +++ b/tournaments/apps.py @@ -1,6 +1,8 @@ from django.apps import AppConfig - class TournamentsConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' name = 'tournaments' + default_auto_field = 'django.db.models.BigAutoField' + + def ready(self): + import tournaments.signals # This will ensure the signals are registered diff --git a/tournaments/migrations/0065_club_broadcast_code.py b/tournaments/migrations/0065_club_broadcast_code.py new file mode 100644 index 0000000..2f203a5 --- /dev/null +++ b/tournaments/migrations/0065_club_broadcast_code.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.11 on 2024-06-04 11:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tournaments', '0064_failedapicall_user'), + ] + + operations = [ + migrations.AddField( + model_name='club', + name='broadcast_code', + field=models.CharField(blank=True, max_length=10, null=True), + ), + ] diff --git a/tournaments/migrations/0066_alter_club_broadcast_code.py b/tournaments/migrations/0066_alter_club_broadcast_code.py new file mode 100644 index 0000000..19f2ace --- /dev/null +++ b/tournaments/migrations/0066_alter_club_broadcast_code.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.11 on 2024-06-04 11:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tournaments', '0065_club_broadcast_code'), + ] + + operations = [ + migrations.AlterField( + model_name='club', + name='broadcast_code', + field=models.CharField(blank=True, max_length=10, null=True, unique=True), + ), + ] diff --git a/tournaments/models/club.py b/tournaments/models/club.py index 77d2053..3765b1b 100644 --- a/tournaments/models/club.py +++ b/tournaments/models/club.py @@ -17,6 +17,7 @@ class Club(models.Model): longitude = models.FloatField(null=True, blank=True) court_count = models.IntegerField(default=2) + broadcast_code = models.CharField(max_length=10, null=True, blank=True, unique=True) def __str__(self): return self.name diff --git a/tournaments/signals.py b/tournaments/signals.py new file mode 100644 index 0000000..cb3eb63 --- /dev/null +++ b/tournaments/signals.py @@ -0,0 +1,19 @@ +import random +import string +from django.db.models.signals import post_save +from django.dispatch import receiver +from .models import Club + +def generate_unique_code(): + characters = string.ascii_letters + string.digits + while True: + code = ''.join(random.sample(characters, 3)) + print(code) + if not Club.objects.filter(broadcast_code=code).exists(): + return code + +@receiver(post_save, sender=Club) +def assign_unique_code(sender, instance, created, **kwargs): + if created and not instance.broadcast_code: + instance.broadcast_code = generate_unique_code() + instance.save() diff --git a/tournaments/urls.py b/tournaments/urls.py index d507ab3..a6242c3 100644 --- a/tournaments/urls.py +++ b/tournaments/urls.py @@ -6,6 +6,7 @@ urlpatterns = [ path("", views.index, name="index"), path("clubs/", views.clubs, name="clubs"), path("clubs//", views.club, name="club"), + path("c/", views.club_broadcast, name="club-broadcast"), path("tournament//", include([ path('', views.tournament, name='tournament'), diff --git a/tournaments/views.py b/tournaments/views.py index ee55afb..ac2c077 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -243,6 +243,11 @@ def activate(request, uidb64, token): else: return HttpResponse('Le lien est invalide.') +def club_broadcast(request, broadcast_code): + club = get_object_or_404(Club, broadcast_code=broadcast_code) + return HttpResponse(club.name) + + ##### API ##### @api_view(['GET'])