You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
5.5 KiB
142 lines
5.5 KiB
import random
|
|
import string
|
|
from django.db.models.signals import post_save, pre_delete, post_delete
|
|
from django.dispatch import receiver
|
|
from django.conf import settings
|
|
from tournaments.models.tournament import Tournament
|
|
from tournaments.models.unregistered_player import UnregisteredPlayer
|
|
from django.utils import timezone
|
|
|
|
from .models import Club, FailedApiCall, CustomUser, Log, TeamRegistration, PlayerRegistration, UnregisteredTeam, UnregisteredPlayer
|
|
import requests
|
|
from tournaments.services.email_service import TournamentEmailService
|
|
from tournaments.models import PlayerDataSource
|
|
|
|
def generate_unique_code():
|
|
characters = string.ascii_lowercase + string.digits
|
|
while True:
|
|
code = ''.join(random.sample(characters, 3))
|
|
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()
|
|
|
|
DISCORD_FAILED_CALLS_WEBHOOK_URL = 'https://discord.com/api/webhooks/1248191778134163486/sSoTL6cULCElWr2YFwyllsg7IXxHcCx_YMDJA_cUHtVUU4WOfN-5M7drCJuwNBBfAk9a'
|
|
DISCORD_LOGS_WEBHOOK_URL = 'https://discord.com/api/webhooks/1257987637449588736/TtOUwzYgSlQH2d3Ps7SfIKRcFALQVa3hfkC-j9K4_UAcWtsfiw4v8NUPbnX2_ZPOYzuv'
|
|
|
|
@receiver(post_save, sender=FailedApiCall)
|
|
def notify_discord_on_create(sender, instance, created, **kwargs):
|
|
notify_object_creation_on_discord(created, instance, DISCORD_FAILED_CALLS_WEBHOOK_URL)
|
|
|
|
# @receiver(post_save, sender=CustomUser)
|
|
# def notify_user_creation_on_discord(sender, instance, created, **kwargs):
|
|
# notify_object_creation_on_discord(created, instance, DISCORD_LOGS_WEBHOOK_URL)
|
|
|
|
@receiver(post_save, sender=Log)
|
|
def notify_log_creation_on_discord(sender, instance, created, **kwargs):
|
|
notify_object_creation_on_discord(created, instance, DISCORD_LOGS_WEBHOOK_URL)
|
|
|
|
# WARNING: using this method requires the instance to have a discord_string method
|
|
def notify_object_creation_on_discord(created, instance, webhook_url):
|
|
if created:
|
|
default_db_engine = settings.DATABASES['default']['ENGINE']
|
|
if default_db_engine != 'django.db.backends.sqlite3':
|
|
site_name = settings.SITE_NAME
|
|
message = f'{site_name} > New {instance.__class__.__name__} created: {instance.discord_string()}'
|
|
send_discord_message(webhook_url, message)
|
|
|
|
def send_discord_message(webhook_url, content):
|
|
data = {
|
|
"content": content
|
|
}
|
|
requests.post(webhook_url, json=data)
|
|
# if response.status_code != 204:
|
|
# raise ValueError(
|
|
# f'Error sending message to Discord webhook: {response.status_code}, {response.text}'
|
|
# )
|
|
|
|
@receiver(pre_delete, sender=TeamRegistration)
|
|
def unregister_team(sender, instance, **kwargs):
|
|
team_registration = instance
|
|
tournament = instance.tournament
|
|
if tournament.is_deleted is True:
|
|
return
|
|
|
|
# Create unregistered player records and track captain/other player
|
|
captain = None
|
|
other_player = None
|
|
for player in team_registration.playerregistration_set.all():
|
|
if player.captain is True:
|
|
captain = player
|
|
else:
|
|
other_player = player
|
|
|
|
# Send unregistration confirmation
|
|
if captain and captain.registered_online and captain.email:
|
|
TournamentEmailService.send_unregistration_confirmation(
|
|
captain,
|
|
tournament,
|
|
other_player
|
|
)
|
|
|
|
first_waiting_list_team = tournament.first_waiting_list_team()
|
|
print("first_waiting_list_team", first_waiting_list_team)
|
|
|
|
# Handle waiting list notifications
|
|
if first_waiting_list_team:
|
|
waiting_captain = None
|
|
waiting_other_player = None
|
|
for player in first_waiting_list_team.playerregistration_set.all():
|
|
if player.captain is True:
|
|
waiting_captain = player
|
|
else:
|
|
waiting_other_player = player
|
|
|
|
if waiting_captain and waiting_captain.registered_online and waiting_captain.email:
|
|
TournamentEmailService.send_out_of_waiting_list_confirmation(
|
|
waiting_captain,
|
|
tournament,
|
|
waiting_other_player
|
|
)
|
|
|
|
@receiver(post_save, sender=Tournament)
|
|
def notify_players_of_tournament_cancellation(sender, instance, **kwargs):
|
|
tournament = instance
|
|
|
|
if tournament.is_deleted is False:
|
|
return
|
|
|
|
# Get all team registrations
|
|
team_registrations = tournament.teamregistration_set.all()
|
|
|
|
for team_registration in team_registrations:
|
|
captain = None
|
|
other_player = None
|
|
|
|
# Get players who registered online and have email
|
|
for player in team_registration.playerregistration_set.all():
|
|
print(player, player.registered_online)
|
|
if player.captain:
|
|
captain = player
|
|
else:
|
|
other_player = player
|
|
|
|
# Send email to captain
|
|
if captain and captain.registered_online and captain.email:
|
|
TournamentEmailService.send_tournament_cancellation_notification(
|
|
captain,
|
|
tournament,
|
|
other_player
|
|
)
|
|
|
|
# Send email to other player if they exist and registered online
|
|
if other_player and other_player.registered_online and other_player.email:
|
|
TournamentEmailService.send_tournament_cancellation_notification(
|
|
other_player,
|
|
tournament,
|
|
captain
|
|
)
|
|
|