Merge webhook views
This commit is contained in:
core
@ -15,10 +15,6 @@ class DiscordSettingForm(forms.Form):
|
|||||||
regex=r"https://discord.com/api/webhooks/\d{18}/[a-zA-Z0-9_-]{68}",
|
regex=r"https://discord.com/api/webhooks/\d{18}/[a-zA-Z0-9_-]{68}",
|
||||||
message="The URL must be a valid Discord webhook URL.",
|
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
|
help_text="The URL can be found by right-clicking on the channel and selecting 'Edit Channel', then 'Integrations', and 'Webhooks'.", # noqa: E501
|
||||||
)
|
)
|
||||||
|
@ -2,10 +2,7 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1 class="my-4">Add Discord Webhook</h1>
|
<h1 class="my-4">Add Discord Webhook</h1>
|
||||||
<form method="post"
|
<form method="post" class="needs-validation" novalidate>
|
||||||
action="{% url 'core:add_webhook' %}"
|
|
||||||
class="needs-validation"
|
|
||||||
novalidate>
|
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{ form.non_field_errors }}
|
{{ form.non_field_errors }}
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
@ -27,7 +24,17 @@
|
|||||||
{% for webhook in webhooks %}
|
{% for webhook in webhooks %}
|
||||||
<li class="list-group-item d-flex justify-content-between align-items-center">
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
||||||
<div>
|
<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>
|
</div>
|
||||||
<form method="post" action="" class="mb-0">
|
<form method="post" action="" class="mb-0">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
11
core/urls.py
11
core/urls.py
@ -14,14 +14,5 @@ urlpatterns: list[URLPattern | URLResolver] = [
|
|||||||
view=views.GameView.as_view(),
|
view=views.GameView.as_view(),
|
||||||
name="games",
|
name="games",
|
||||||
),
|
),
|
||||||
path(
|
path("webhooks/", views.WebhooksView.as_view(), name="webhooks"),
|
||||||
route="webhooks/",
|
|
||||||
view=views.Webhooks.as_view(),
|
|
||||||
name="webhooks",
|
|
||||||
),
|
|
||||||
path(
|
|
||||||
route="webhooks/add/",
|
|
||||||
view=views.add_webhook,
|
|
||||||
name="add_webhook",
|
|
||||||
),
|
|
||||||
]
|
]
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from typing import TYPE_CHECKING, Any
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
import hishel
|
import hishel
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.http import (
|
from django.http.response import HttpResponse
|
||||||
HttpRequest,
|
|
||||||
HttpResponse,
|
|
||||||
)
|
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.views.decorators.http import require_POST
|
from django.views.generic import FormView, ListView
|
||||||
from django.views.generic import ListView, TemplateView
|
from httpx._models import Response
|
||||||
|
|
||||||
from twitch_app.models import (
|
from twitch_app.models import (
|
||||||
DropBenefit,
|
DropBenefit,
|
||||||
@ -25,7 +23,13 @@ from twitch_app.models import (
|
|||||||
from .forms import DiscordSettingForm
|
from .forms import DiscordSettingForm
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
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__)
|
logger: logging.Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -210,18 +214,21 @@ class WebhookData:
|
|||||||
|
|
||||||
name: str | None = None
|
name: str | None = None
|
||||||
url: str | None = None
|
url: str | None = None
|
||||||
|
avatar: str | None = None
|
||||||
status: str | None = None
|
status: str | None = None
|
||||||
response: str | None = None
|
response: str | None = None
|
||||||
|
|
||||||
|
|
||||||
class Webhooks(TemplateView):
|
class WebhooksView(FormView):
|
||||||
model = Game
|
model = Game
|
||||||
template_name: str = "webhooks.html"
|
template_name = "webhooks.html"
|
||||||
|
form_class = DiscordSettingForm
|
||||||
context_object_name: str = "webhooks"
|
context_object_name: str = "webhooks"
|
||||||
paginate_by = 100
|
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."""
|
"""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", "")
|
cookie: str = self.request.COOKIES.get("webhooks", "")
|
||||||
webhooks: list[str] = cookie.split(",")
|
webhooks: list[str] = cookie.split(",")
|
||||||
webhooks = list(filter(None, webhooks))
|
webhooks = list(filter(None, webhooks))
|
||||||
@ -231,43 +238,57 @@ class Webhooks(TemplateView):
|
|||||||
with hishel.CacheClient(storage=storage, controller=controller) as client:
|
with hishel.CacheClient(storage=storage, controller=controller) as client:
|
||||||
for webhook in webhooks:
|
for webhook in webhooks:
|
||||||
our_webhook = WebhookData(name="Unknown", url=webhook, status="Failed", response="No response")
|
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:
|
if response.is_success:
|
||||||
our_webhook.name = response.json()["name"]
|
our_webhook.name = response.json().get("name", "Unknown")
|
||||||
our_webhook.status = "Success"
|
our_webhook.status = "Success"
|
||||||
our_webhook.response = response.text
|
|
||||||
else:
|
else:
|
||||||
our_webhook.status = "Failed"
|
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)
|
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
|
with hishel.CacheClient(storage=storage, controller=controller) as client:
|
||||||
def add_webhook(request: HttpRequest) -> HttpResponse:
|
webhook_response: Response = client.get(url=webhook, extensions={"cache_metadata": True})
|
||||||
"""Add a webhook to the list of webhooks."""
|
if not webhook_response.is_success:
|
||||||
form = DiscordSettingForm(request.POST)
|
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_name: str | None = str(webhook_response.json().get("name")) if webhook_response.is_success else None
|
||||||
webhook = str(form.cleaned_data["webhook"])
|
|
||||||
response = HttpResponse()
|
|
||||||
|
|
||||||
if "webhooks" in request.COOKIES:
|
cookie: str = self.request.COOKIES.get("webhooks", "")
|
||||||
cookie: str = request.COOKIES["webhooks"]
|
webhooks: list[str] = cookie.split(",")
|
||||||
webhooks: list[str] = cookie.split(",")
|
webhooks = list(filter(None, webhooks))
|
||||||
webhooks = list(filter(None, webhooks))
|
if webhook in webhooks:
|
||||||
if webhook in webhooks:
|
if webhook_name:
|
||||||
messages.error(request, "Webhook already exists.")
|
messages.error(self.request, f"Webhook {webhook_name} already exists.")
|
||||||
return response
|
else:
|
||||||
webhooks.append(webhook)
|
messages.error(self.request, "Webhook already exists.")
|
||||||
webhook: str = ",".join(webhooks)
|
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 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))
|
||||||
|
Reference in New Issue
Block a user