|
|
|
|
@ -2,7 +2,10 @@ import random |
|
|
|
|
import string |
|
|
|
|
from django.db.models.signals import post_save |
|
|
|
|
from django.dispatch import receiver |
|
|
|
|
from .models import Club |
|
|
|
|
from django.conf import settings |
|
|
|
|
|
|
|
|
|
from .models import Club, FailedApiCall |
|
|
|
|
import requests |
|
|
|
|
|
|
|
|
|
def generate_unique_code(): |
|
|
|
|
characters = string.ascii_letters + string.digits |
|
|
|
|
@ -16,3 +19,26 @@ def assign_unique_code(sender, instance, created, **kwargs): |
|
|
|
|
if created and not instance.broadcast_code: |
|
|
|
|
instance.broadcast_code = generate_unique_code() |
|
|
|
|
instance.save() |
|
|
|
|
|
|
|
|
|
DISCORD_WEBHOOK_URL = 'https://discord.com/api/webhooks/1248191778134163486/sSoTL6cULCElWr2YFwyllsg7IXxHcCx_YMDJA_cUHtVUU4WOfN-5M7drCJuwNBBfAk9a' |
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=FailedApiCall) |
|
|
|
|
def notify_discord_on_create(sender, instance, created, **kwargs): |
|
|
|
|
if created: |
|
|
|
|
|
|
|
|
|
default_db_engine = settings.DATABASES['default']['ENGINE'] |
|
|
|
|
print(default_db_engine) |
|
|
|
|
if default_db_engine != 'django.db.backends.sqlite3': |
|
|
|
|
message = f'New FailedApiCall created: {instance}' |
|
|
|
|
send_discord_message(DISCORD_WEBHOOK_URL, message) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_discord_message(webhook_url, content): |
|
|
|
|
data = { |
|
|
|
|
"content": content |
|
|
|
|
} |
|
|
|
|
response = 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}' |
|
|
|
|
) |
|
|
|
|
|