diff --git a/api/utils.py b/api/utils.py index bbe5dc2..5511be0 100644 --- a/api/utils.py +++ b/api/utils.py @@ -3,3 +3,14 @@ import re def is_valid_email(email): email_regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' return re.match(email_regex, email) is not None + +def check_version_smaller_than_1_1_12(version_str): + # Remove the parentheses part if it exists, example of version: 1.1.12 (2) + version_str = version_str.split()[0] + + # Split version into components + version_parts = [int(x) for x in version_str.split('.')] + target_parts = [1, 1, 12] + + # Compare version components + return version_parts < target_parts diff --git a/api/views.py b/api/views.py index 310c867..0f12567 100644 --- a/api/views.py +++ b/api/views.py @@ -19,11 +19,13 @@ from django.db.models import Q from django.core.exceptions import ObjectDoesNotExist from .permissions import IsClubOwner -from .utils import is_valid_email +from .utils import is_valid_email, check_version_smaller_than_1_1_12 from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator +from shared.discord import send_discord_log_message + @method_decorator(csrf_exempt, name='dispatch') class CustomAuthToken(APIView): permission_classes = [] @@ -112,6 +114,17 @@ class TournamentViewSet(SoftDeleteViewSet): return [] return self.queryset.filter(event__creator=self.request.user) + def perform_create(self, serializer): + serializer.save() + # version check + app_version = self.request.headers.get('App-Version') + self.warn_if_version_is_too_small(app_version) + + def warn_if_version_is_too_small(self, version): + if check_version_smaller_than_1_1_12(version): + message = f'{self.request.user.username} app version is {version}' + send_discord_log_message(message) + class PurchaseViewSet(SoftDeleteViewSet): queryset = Purchase.objects.all() serializer_class = PurchaseSerializer diff --git a/shared/discord.py b/shared/discord.py new file mode 100644 index 0000000..8ecbcc6 --- /dev/null +++ b/shared/discord.py @@ -0,0 +1,16 @@ +import requests + +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' + +def send_discord_failed_calls_message(message): + send_discord_message(DISCORD_FAILED_CALLS_WEBHOOK_URL, message) + +def send_discord_log_message(message): + send_discord_message(DISCORD_LOGS_WEBHOOK_URL, message) + +def send_discord_message(webhook_url, content): + data = { + "content": content + } + requests.post(webhook_url, json=data) diff --git a/tournaments/signals.py b/tournaments/signals.py index 848dbca..494a623 100644 --- a/tournaments/signals.py +++ b/tournaments/signals.py @@ -12,6 +12,8 @@ import requests from tournaments.services.email_service import TournamentEmailService from tournaments.models import PlayerDataSource +from shared.discord import send_discord_log_message, send_discord_failed_calls_message + def generate_unique_code(): characters = string.ascii_lowercase + string.digits while True: @@ -25,39 +27,37 @@ def assign_unique_code(sender, instance, created, **kwargs): 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) + notify_object_creation_on_discord(created, instance) @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) + notify_object_creation_on_discord(created, instance) # WARNING: using this method requires the instance to have a discord_string method -def notify_object_creation_on_discord(created, instance, webhook_url): +def notify_object_creation_on_discord(created, instance): 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}' - # ) + if isinstance(instance, FailedApiCall): + send_discord_failed_calls_message(message) + else: + send_discord_log_message(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):