This commit is contained in:
2024-07-01 05:56:36 +02:00
parent 3f7bacca2f
commit 219aee31af
30 changed files with 679 additions and 263 deletions

6
core/admin.py Normal file
View File

@ -0,0 +1,6 @@
from django.contrib import admin
from .models import DiscordSetting, Subscription
admin.site.register(DiscordSetting)
admin.site.register(Subscription)

View File

@ -10,15 +10,18 @@ if TYPE_CHECKING:
logger: logging.Logger = logging.getLogger(__name__)
def send(message: str) -> None:
def send(message: str, webhook_url: str | None = None) -> None:
"""Send a message to Discord.
Args:
message: The message to send.
webhook_url: The webhook URL to send the message to.
"""
webhook_url = str(settings.DISCORD_WEBHOOK_URL)
logger.info("Discord message: %s", message)
webhook_url = webhook_url or str(settings.DISCORD_WEBHOOK_URL)
if not webhook_url:
logger.error("No Discord webhook URL found.")
logger.error("No webhook URL provided.")
return
webhook = DiscordWebhook(

15
core/forms.py Normal file
View File

@ -0,0 +1,15 @@
from django import forms
class DiscordSettingForm(forms.Form):
name = forms.CharField(
max_length=255,
label="Name",
required=True,
help_text="Friendly name for knowing where the notification goes to.",
)
webhook_url = forms.URLField(
label="Webhook URL",
required=True,
help_text="The URL to the Discord webhook. The URL can be found by right-clicking on the channel and selecting 'Edit Channel', then 'Integrations', and 'Webhooks'.", # noqa: E501
)

View File

@ -0,0 +1,62 @@
# Generated by Django 5.0.6 on 2024-06-30 23:42
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
from django.db.migrations.operations.base import Operation
class Migration(migrations.Migration):
initial = True
dependencies: list[tuple[str, str]] = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations: list[Operation] = [
migrations.CreateModel(
name="NotificationType",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name="DiscordSetting",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("webhook_url", models.URLField()),
("disabled", models.BooleanField(default=False)),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
(
"notification_type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.notificationtype",
),
),
],
),
]

View File

@ -0,0 +1,26 @@
# Generated by Django 5.0.6 on 2024-07-01 01:10
from django.db import migrations, models
from django.db.migrations.operations.base import Operation
class Migration(migrations.Migration):
dependencies: list[tuple[str, str]] = [
("core", "0001_initial"),
]
operations: list[Operation] = [
migrations.RemoveField(
model_name="discordsetting",
name="notification_type",
),
migrations.AddField(
model_name="discordsetting",
name="name",
field=models.CharField(default="No name", max_length=255),
preserve_default=False,
),
migrations.DeleteModel(
name="NotificationType",
),
]

View File

@ -0,0 +1,53 @@
# Generated by Django 5.0.6 on 2024-07-01 03:28
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
from django.db.migrations.operations.base import Operation
class Migration(migrations.Migration):
dependencies: list[tuple[str, str]] = [
("core", "0002_remove_discordsetting_notification_type_and_more"),
("twitch_app", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations: list[Operation] = [
migrations.CreateModel(
name="Subscription",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
(
"discord_webhook",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="core.discordsetting",
),
),
(
"game",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="twitch_app.game",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]

24
core/models.py Normal file
View File

@ -0,0 +1,24 @@
from django.contrib.auth.models import User
from django.db import models
from twitch_app.models import Game
class DiscordSetting(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
webhook_url = models.URLField()
disabled = models.BooleanField(default=False)
def __str__(self) -> str:
return f"Discord: {self.user.username} - {self.name}"
class Subscription(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
game = models.ForeignKey(Game, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
discord_webhook = models.ForeignKey(DiscordSetting, on_delete=models.CASCADE)
def __str__(self) -> str:
return f"Subscription: {self.user.username} - {self.game.display_name} - {self.discord_webhook.name}"

View File

@ -0,0 +1,206 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="description" content="Twitch Drops">
<meta name="author" content="TheLovinator">
<meta name="keywords" content="Twitch, Drops, Twitch Drops">
<meta name="robots" content="index, follow">
<title>Add Discord Webhook</title>
<style>
:root {
--background-color: #121212;
--text-color: #e0e0e0;
--header-background: #1e1e1e;
--border-color: #333;
--button-background: #6441a5;
--button-hover-background: #503682;
--button-shadow: rgba(0, 0, 0, 0.2);
--button-padding: 0.5rem 1rem;
--button-margin: 0.5rem 0;
--button-radius: 0.5rem;
--button-font-size: 0.875rem;
--input-background: #2a2a2a;
--input-border: #444;
--input-focus-border: #6441a5;
--input-padding: 0.5rem;
--input-radius: 0.5rem;
--form-gap: 1rem;
}
html {
max-width: 88ch;
padding: calc(1vmin + 0.5rem);
margin-inline: auto;
font-size: clamp(1em, 0.909em + 0.45vmin, 1.25em);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: var(--background-color);
color: var(--text-color);
}
a {
text-decoration: none;
color: inherit;
}
header {
padding: 10px 30px;
background: var(--header-background);
display: flex;
flex-direction: column;
align-items: flex-start;
}
ul {
list-style-type: none;
padding: 0;
}
.game {
margin-bottom: 1rem;
border: 1px solid var(--border-color);
}
img {
margin: 10px;
}
button {
background-color: var(--button-background);
color: white;
border: none;
padding: var(--button-padding);
margin: var(--button-margin);
border-radius: var(--button-radius);
cursor: pointer;
transition: background-color 0.3s ease, box-shadow 0.3s ease;
font-size: var(--button-font-size);
box-shadow: 0 2px 4px var(--button-shadow);
}
button:hover {
background-color: var(--button-hover-background);
box-shadow: 0 3px 6px var(--button-shadow);
}
.navbar {
margin-bottom: 1rem;
text-align: center;
}
.logo {
text-align: center;
font-size: 2.5rem;
font-weight: 600;
margin: 0;
}
.messages {
list-style-type: none;
padding: 0;
margin-bottom: var(--form-gap);
}
.error {
color: red;
}
.success {
color: green;
}
form {
display: flex;
flex-direction: column;
gap: var(--form-gap);
background-color: var(--header-background);
padding: 1rem;
border: 1px solid var(--border-color);
}
form div {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 0.5rem;
font-weight: bold;
}
input, select, textarea {
background-color: var(--input-background);
border: 1px solid var(--input-border);
padding: var(--input-padding);
color: var(--text-color);
transition: border-color 0.3s ease;
}
input:focus, select:focus, textarea:focus {
border-color: var(--input-focus-border);
outline: none;
}
small {
color: #aaa;
margin-top: 0.5rem;
}
</style>
</head>
<body>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li {% if message.tags %}class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<h1 class="logo">
<a href="{% url 'core:index' %}">Twitch Drops</a>
</h1>
<div class="navbar">
<a href='{% url "api-1.0.0:openapi-view" %}'>API</a> |
<a href="https://github.com/sponsors/TheLovinator1">Donate</a> |
TheLovinator#9276
</div>
<h1>Add Discord Webhook</h1>
<form method="post">
{% csrf_token %}
{{ form.non_field_errors }}
<div>
{{ form.name.errors }}
<label for="{{ form.name.id_for_label }}">{{ form.name.label_tag }}</label>
{{ form.name }}
<small>{{ form.name.help_text }}</small>
</div>
<div>
{{ form.webhook_url.errors }}
<label for="{{ form.webhook_url.id_for_label }}">{{ form.webhook_url.label_tag }}</label>
{{ form.webhook_url }}
<small>{{ form.webhook_url.help_text }}</small>
</div>
<button type="submit">Add Webhook</button>
</form>
<h2>Webhooks</h2>
<ul>
{% for webhook in webhooks %}
<li>
<form method="post" action="{% url 'core:delete_discord_webhook' %}">
<strong>{{ webhook.name }}</strong>
<small>
<a href="{{ webhook.webhook_url }}">{{ webhook.webhook_url }}</a>
</small>
{% csrf_token %}
<input type="hidden" name="webhook_id" value="{{ webhook.id }}">
<input type="hidden" name="webhook_name" value="{{ webhook.name }}">
<input type="hidden" name="webhook_url" value="{{ webhook.webhook_url }}">
<button type="submit">Delete</button>
</form>
</li>
{% endfor %}
</ul>
</body>
</html>

View File

@ -1,3 +1,4 @@
{% load socialaccount %}
<!DOCTYPE html>
<html lang="en">
<head>
@ -17,9 +18,13 @@
--border-color: #333;
--button-background: #6441a5;
--button-hover-background: #503682;
--button-shadow: rgba(0, 0, 0, 0.2);
--button-padding: 0.5rem 1rem;
--button-margin: 0.5rem 0;
--button-radius: 0.5rem;
--button-font-size: 0.875rem;
}
/* Set some good defaults for the page */
html {
max-width: 88ch;
padding: calc(1vmin + 0.5rem);
@ -30,76 +35,67 @@
color: var(--text-color);
}
/* Don't underline links and remove the blue/purple color */
a {
text-decoration: none;
color: inherit;
}
/* Add a gray background for the game name header */
/* This header also contains the button to subscribe to the game */
header {
display: flex;
align-items: center;
justify-content: space-between; /* So the button is on the right, and the game name is on the left */
padding: 10px 30px;
background: var(--header-background);
display: flex;
flex-direction: column;
align-items: flex-start;
}
/* Remove dot in front of list items */
ul {
list-style-type: none;
padding: 0;
}
/* Add a border around each game to separate them */
.game {
margin-bottom: 1rem;
border: 1px solid var(--border-color);
}
/* Move images away from the border */
img {
margin: 10px;
}
/* Button to subscribe to a game */
/* For example: Subscribe to Rocket League */
button {
background-color: var(--button-background);
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 5px;
padding: var(--button-padding);
margin: var(--button-margin);
border-radius: var(--button-radius);
cursor: pointer;
transition: background-color 0.3s ease;
}
/* Make button darker when hovered */
button:hover {
background-color: var(--button-hover-background);
transition: background-color 0.3s ease, box-shadow 0.3s ease;
font-size: var(--button-font-size);
box-shadow: 0 2px 4px var(--button-shadow);
}
button:hover {
background-color: var(--button-hover-background);
box-shadow: 0 3px 6px var(--button-shadow);
}
/* Navbar at the top of the page */
.navbar {
margin-bottom: 1rem;
text-align: center;
}
/* Make the logo bigger and bolder and center it */
.logo {
text-align: center;
font-size: 2.5rem;
font-weight: 600;
margin: 0;
}
/* Django messages framework */
.messages {
list-style-type: none;
list-style-type: none;
}
/* Make error messages red and success messages green */
.error {
color: red;
}
@ -121,19 +117,34 @@
<div class="navbar">
<a href='{% url "api-1.0.0:openapi-view" %}'>API</a> |
<a href="https://github.com/sponsors/TheLovinator1">Donate</a> |
TheLovinator#9276
TheLovinator#9276 |
{% if user.is_authenticated %}
<a href='{% url "account_logout" %}'>Logout</a>
{% else %}
<a href='{% provider_login_url "twitch" %}'>Login</a>
{% endif %}
</div>
{% for organization, org_data in orgs_data.items %}
<ul>
{% for game, game_data in org_data.games.items %}
<li class="game">
<header>
<img src="{{ game.image_url }}"
alt="{{ game.display_name }}"
height="100"
width="100"
loading="lazy">
<h2>
<a href="https://www.twitch.tv/directory/category/{{ game.slug }}">{{ game.display_name }}</a>
</h2>
<form action="" method="post">
<form action='{% url "core:subscription_create" %}' method="post">
{% csrf_token %}
<input type="hidden" name="game_id" value="{{ game.id }}">
<select name="discord_webhook">
{% for discord_setting in discord_settings %}
<option value="{{ discord_setting.id }}">{{ discord_setting.name }}</option>
{% endfor %}
</select>
<button type="submit">Subscribe to {{ game.display_name }}</button>
</form>
<form action='{% url "core:test" %}' method="post">
@ -150,7 +161,8 @@
<img src="{{ drop_benefit.image_asset_url }}"
alt="{{ drop_benefit.name }}"
height="100"
width="100">
width="100"
loading="lazy">
<a href="{{ drop_benefit.details_url }}">{{ drop_benefit.name }}</a>
</li>
{% endfor %}

View File

@ -10,4 +10,19 @@ app_name: str = "core"
urlpatterns: list[URLPattern | URLResolver] = [
path(route="", view=views.index, name="index"),
path(route="test/", view=views.test_webhook, name="test"),
path(
route="add-discord-webhook/",
view=views.add_discord_webhook,
name="add_discord_webhook",
),
path(
route="delete_discord_webhook/",
view=views.delete_discord_webhook,
name="delete_discord_webhook",
),
path(
route="subscribe/",
view=views.subscription_create,
name="subscription_create",
),
]

View File

@ -1,12 +1,19 @@
import logging
from typing import TYPE_CHECKING
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db.models.manager import BaseManager
from django.http import HttpRequest, HttpResponse
from django.http import (
HttpRequest,
HttpResponse,
)
from django.shortcuts import redirect, render
from django.template.response import TemplateResponse
from core.discord import send
from twitch.models import (
from core.models import DiscordSetting
from twitch_app.models import (
DropBenefit,
DropCampaign,
Game,
@ -14,11 +21,15 @@ from twitch.models import (
TimeBasedDrop,
)
from .forms import DiscordSettingForm
if TYPE_CHECKING:
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import AnonymousUser
from django.db.models.manager import BaseManager
from django.views.decorators.http import require_POST
logger = logging.getLogger(__name__)
logger: logging.Logger = logging.getLogger(__name__)
def index(request: HttpRequest) -> HttpResponse:
@ -32,12 +43,7 @@ def index(request: HttpRequest) -> HttpResponse:
"""
organizations: BaseManager[Organization] = Organization.objects.all()
# Organize the data
orgs_data = {}
for org in organizations:
orgs_data[org] = {"games": {}, "drop_campaigns": []}
# Populate the games under each organization through DropBenefit and DropCampaign
orgs_data = {org: {"games": {}, "drop_campaigns": []} for org in organizations}
for org in organizations:
drop_benefits: BaseManager[DropBenefit] = DropBenefit.objects.filter(
owner_organization=org,
@ -70,25 +76,29 @@ def index(request: HttpRequest) -> HttpResponse:
for campaign in drop_campaigns:
orgs_data[org]["drop_campaigns"].append(campaign)
if request.user.is_authenticated:
discord_settings: BaseManager[DiscordSetting] = DiscordSetting.objects.filter(
user=request.user,
)
context = {
"orgs_data": orgs_data,
"discord_settings": discord_settings if request.user.is_authenticated else None,
}
return TemplateResponse(
request=request,
template="index.html",
context={"orgs_data": orgs_data},
context=context,
)
@require_POST
def test_webhook(
request: HttpRequest,
*args, # noqa: ANN002, ARG001
**kwargs, # noqa: ARG001, ANN003
) -> HttpResponse:
def test_webhook(request: HttpRequest) -> HttpResponse:
"""Test webhook.
Args:
request: The request.
args: Additional arguments.
kwargs: Additional keyword arguments.
Returns:
HttpResponse: Returns a response.
@ -99,12 +109,98 @@ def test_webhook(
campaign: DropCampaign = DropCampaign.objects.get(id=org_id)
msg: str = f"""
Found new drop for {campaign.game.display_name}:\n
{campaign.name}\n
{campaign.description}\n
<{campaign.details_url}>
"""
msg: str = f"Found new drop for {campaign.game.display_name}:\n{campaign.name}\n{campaign.description}"
send(msg.strip())
return HttpResponse(status=200)
@login_required
def add_discord_webhook(request: HttpRequest) -> HttpResponse:
"""Add Discord webhook."""
if request.method == "POST":
form = DiscordSettingForm(request.POST)
if form.is_valid():
DiscordSetting.objects.create(
user=request.user,
name=form.cleaned_data["name"],
webhook_url=form.cleaned_data["webhook_url"],
disabled=False,
)
messages.success(
request=request,
message=f"Webhook '{form.cleaned_data["name"]}' added ({form.cleaned_data["webhook_url"]})",
)
return redirect("core:add_discord_webhook")
else:
form = DiscordSettingForm()
webhooks: BaseManager[DiscordSetting] = DiscordSetting.objects.filter(
user=request.user,
)
return render(
request,
"add_discord_webhook.html",
{"form": form, "webhooks": webhooks},
)
@login_required
def delete_discord_webhook(request: HttpRequest) -> HttpResponse:
"""Delete Discord webhook."""
if request.method == "POST":
DiscordSetting.objects.filter(
id=request.POST.get("webhook_id"),
name=request.POST.get("webhook_name"),
webhook_url=request.POST.get("webhook_url"),
user=request.user,
).delete()
messages.success(
request=request,
message=f"Webhook '{request.POST.get("webhook_name")}' deleted ({request.POST.get("webhook_url")})",
)
return redirect("core:add_discord_webhook")
@login_required
def subscription_create(request: HttpRequest) -> HttpResponse:
"""Create subscription."""
if request.method == "POST":
game: Game = Game.objects.get(id=request.POST.get("game_id"))
user: AbstractBaseUser | AnonymousUser = request.user
webhook_id: str | None = request.POST.get("webhook_id")
if not webhook_id:
messages.error(request, "No webhook ID provided.")
return redirect("core:index")
if not user.is_authenticated:
messages.error(
request,
"You need to be logged in to create a subscription.",
)
return redirect("core:index")
logger.info(
"Current webhooks: %s",
DiscordSetting.objects.filter(user=user).values_list("id", flat=True),
)
discord_webhook: DiscordSetting = DiscordSetting.objects.get(
id=int(webhook_id),
user=user,
)
messages.success(request, "Subscription created")
send(
message=f"This channel will now receive a notification when a new Twitch drop for **{game}** is available.", # noqa: E501
webhook_url=discord_webhook.webhook_url,
)
return redirect("core:index")
messages.error(request, "Failed to create subscription")
return redirect("core:index")