Merge webhook views

This commit is contained in:
2024-07-06 01:09:58 +02:00
parent b3d331af31
commit 9da33b402c
4 changed files with 71 additions and 56 deletions

View File

@ -15,10 +15,6 @@ class DiscordSettingForm(forms.Form):
regex=r"https://discord.com/api/webhooks/\d{18}/[a-zA-Z0-9_-]{68}",
message="The URL must be a valid Discord webhook URL.",
),
URLValidator(
regex=r"https://discordapp.com/api/webhooks/\d{18}/[a-zA-Z0-9_-]{68}",
message="The URL must be a valid Discord webhook URL.",
),
],
help_text="The URL can be found by right-clicking on the channel and selecting 'Edit Channel', then 'Integrations', and 'Webhooks'.", # noqa: E501
)

View File

@ -2,10 +2,7 @@
{% block content %}
<div class="container">
<h1 class="my-4">Add Discord Webhook</h1>
<form method="post"
action="{% url 'core:add_webhook' %}"
class="needs-validation"
novalidate>
<form method="post" class="needs-validation" novalidate>
{% csrf_token %}
{{ form.non_field_errors }}
<div class="mb-3">
@ -27,7 +24,17 @@
{% for webhook in webhooks %}
<li class="list-group-item d-flex justify-content-between align-items-center">
<div>
<a href="{{ webhook.url }}" target="_blank">{{ webhook.name }}</a> - <small>{{ webhook.status }}</small>
<img src="{{ webhook.avatar }}?size=32"
alt="{{ webhook.name }}"
class="rounded-circle"
height="32"
width="32">
<a href="{{ webhook.url }}" target="_blank">{{ webhook.name }}</a>
{% if webhook.status == 'Success' %}
<span class="badge bg-success">Working</span>
{% else %}
<span class="badge bg-danger">Failed</span>
{% endif %}
</div>
<form method="post" action="" class="mb-0">
{% csrf_token %}

View File

@ -14,14 +14,5 @@ urlpatterns: list[URLPattern | URLResolver] = [
view=views.GameView.as_view(),
name="games",
),
path(
route="webhooks/",
view=views.Webhooks.as_view(),
name="webhooks",
),
path(
route="webhooks/add/",
view=views.add_webhook,
name="add_webhook",
),
path("webhooks/", views.WebhooksView.as_view(), name="webhooks"),
]

View File

@ -1,19 +1,17 @@
from __future__ import annotations
import datetime
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any
import hishel
from django.conf import settings
from django.contrib import messages
from django.http import (
HttpRequest,
HttpResponse,
)
from django.http.response import HttpResponse
from django.template.response import TemplateResponse
from django.views.decorators.http import require_POST
from django.views.generic import ListView, TemplateView
from django.views.generic import FormView, ListView
from httpx._models import Response
from twitch_app.models import (
DropBenefit,
@ -25,7 +23,13 @@ from twitch_app.models import (
from .forms import DiscordSettingForm
if TYPE_CHECKING:
import httpx
from pathlib import Path
from django.http import (
HttpRequest,
HttpResponse,
)
from httpx import Response
logger: logging.Logger = logging.getLogger(__name__)
@ -210,18 +214,21 @@ class WebhookData:
name: str | None = None
url: str | None = None
avatar: str | None = None
status: str | None = None
response: str | None = None
class Webhooks(TemplateView):
class WebhooksView(FormView):
model = Game
template_name: str = "webhooks.html"
template_name = "webhooks.html"
form_class = DiscordSettingForm
context_object_name: str = "webhooks"
paginate_by = 100
def get_context_data(self, **kwargs) -> dict[str, list[WebhookData] | DiscordSettingForm]: # noqa: ANN003, ARG002
def get_context_data(self: WebhooksView, **kwargs: dict[str, WebhooksView] | DiscordSettingForm) -> dict[str, Any]:
"""Get the context data for the view."""
context: dict[str, DiscordSettingForm | list[WebhookData]] = super().get_context_data(**kwargs)
cookie: str = self.request.COOKIES.get("webhooks", "")
webhooks: list[str] = cookie.split(",")
webhooks = list(filter(None, webhooks))
@ -231,43 +238,57 @@ class Webhooks(TemplateView):
with hishel.CacheClient(storage=storage, controller=controller) as client:
for webhook in webhooks:
our_webhook = WebhookData(name="Unknown", url=webhook, status="Failed", response="No response")
response: httpx.Response = client.get(url=webhook, extensions={"cache_metadata": True})
response: Response = client.get(url=webhook, extensions={"cache_metadata": True})
if response.is_success:
our_webhook.name = response.json()["name"]
our_webhook.name = response.json().get("name", "Unknown")
our_webhook.status = "Success"
our_webhook.response = response.text
else:
our_webhook.status = "Failed"
our_webhook.response = response.text
# Add to the list of webhooks
our_webhook.response = response.text
if response.json().get("id") and response.json().get("avatar"):
avatar_url: str = f'https://cdn.discordapp.com/avatars/{response.json().get("id")}/{response.json().get("avatar")}.png'
our_webhook.avatar = avatar_url or "https://cdn.discordapp.com/embed/avatars/0.png"
webhook_responses.append(our_webhook)
return {"webhooks": webhook_responses, "form": DiscordSettingForm()}
context.update({
"webhooks": webhook_responses,
"form": DiscordSettingForm(),
})
return context
def form_valid(self: WebhooksView, form: DiscordSettingForm) -> HttpResponse:
"""Handle valid form submission."""
webhook = str(form.cleaned_data["webhook_url"])
@require_POST
def add_webhook(request: HttpRequest) -> HttpResponse:
"""Add a webhook to the list of webhooks."""
form = DiscordSettingForm(request.POST)
with hishel.CacheClient(storage=storage, controller=controller) as client:
webhook_response: Response = client.get(url=webhook, extensions={"cache_metadata": True})
if not webhook_response.is_success:
messages.error(self.request, "Failed to get webhook information. Is the URL correct?")
return self.render_to_response(self.get_context_data(form=form))
if form.is_valid():
webhook = str(form.cleaned_data["webhook"])
response = HttpResponse()
webhook_name: str | None = str(webhook_response.json().get("name")) if webhook_response.is_success else None
if "webhooks" in request.COOKIES:
cookie: str = request.COOKIES["webhooks"]
webhooks: list[str] = cookie.split(",")
webhooks = list(filter(None, webhooks))
if webhook in webhooks:
messages.error(request, "Webhook already exists.")
return response
webhooks.append(webhook)
webhook: str = ",".join(webhooks)
cookie: str = self.request.COOKIES.get("webhooks", "")
webhooks: list[str] = cookie.split(",")
webhooks = list(filter(None, webhooks))
if webhook in webhooks:
if webhook_name:
messages.error(self.request, f"Webhook {webhook_name} already exists.")
else:
messages.error(self.request, "Webhook already exists.")
return self.render_to_response(self.get_context_data(form=form))
response.set_cookie(key="webhooks", value=webhook, max_age=60 * 60 * 24 * 365)
webhooks.append(webhook)
response: HttpResponse = self.render_to_response(self.get_context_data(form=form))
response.set_cookie(key="webhooks", value=",".join(webhooks), max_age=60 * 60 * 24 * 365)
messages.info(request, "Webhook successfully added.")
messages.success(self.request, "Webhook successfully added.")
return response
return HttpResponse(status=400, content="Invalid form data.")
def form_invalid(self: WebhooksView, form: DiscordSettingForm) -> HttpResponse:
messages.error(self.request, "Failed to add webhook.")
return self.render_to_response(self.get_context_data(form=form))