-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blacklist filters and actions on guilds
- Loading branch information
Showing
6 changed files
with
133 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,71 @@ | ||
from django.contrib import admin | ||
from typing import TYPE_CHECKING | ||
|
||
from ..models import GuildConfig | ||
from asgiref.sync import async_to_sync | ||
from django.contrib import admin, messages | ||
from django_admin_action_forms import action_with_form | ||
|
||
from admin_panel.webhook import notify_admins | ||
|
||
from ..forms import BlacklistActionForm, BlacklistedListFilter | ||
from ..models import BlacklistedGuild, BlacklistHistory, GuildConfig | ||
from ..utils import BlacklistTabular | ||
|
||
if TYPE_CHECKING: | ||
from django.db.models import QuerySet | ||
from django.http import HttpRequest | ||
|
||
|
||
@admin.register(GuildConfig) | ||
class GuildAdmin(admin.ModelAdmin): | ||
list_display = ("guild_id", "spawn_channel", "enabled", "silent") | ||
list_filter = ("enabled", "silent") | ||
list_display = ("guild_id", "spawn_channel", "enabled", "silent", "blacklisted") | ||
list_filter = ("enabled", "silent", BlacklistedListFilter) | ||
show_facets = admin.ShowFacets.NEVER | ||
|
||
search_fields = ("guild_id", "spawn_channel") | ||
search_help_text = "Search by guild ID or spawn channel ID" | ||
|
||
inlines = (BlacklistTabular,) | ||
actions = ("blacklist_guilds",) | ||
|
||
@admin.display(description="Is blacklisted", boolean=True) | ||
def blacklisted(self, obj: GuildConfig): | ||
return BlacklistedGuild.objects.filter(discord_id=obj.guild_id).exists() | ||
|
||
@action_with_form( | ||
BlacklistActionForm, description="Blacklist the selected guilds" | ||
) # type: ignore | ||
def blacklist_guilds( | ||
self, request: "HttpRequest", queryset: "QuerySet[GuildConfig]", data: dict | ||
): | ||
reason = ( | ||
data["reason"] | ||
+ f"\nDone through the admin panel by {request.user} ({request.user.pk})" | ||
) | ||
blacklists: list[BlacklistedGuild] = [] | ||
histories: list[BlacklistHistory] = [] | ||
for guild in queryset: | ||
if BlacklistedGuild.objects.filter(discord_id=guild.guild_id).exists(): | ||
self.message_user( | ||
request, f"Guild {guild.guild_id} is already blacklisted!", messages.ERROR | ||
) | ||
return | ||
blacklists.append( | ||
BlacklistedGuild(discord_id=guild.guild_id, reason=reason, moderator_id=None) | ||
) | ||
histories.append( | ||
BlacklistHistory( | ||
discord_id=guild.guild_id, reason=reason, moderator_id=0, id_type="guild" | ||
) | ||
) | ||
BlacklistedGuild.objects.bulk_create(blacklists) | ||
BlacklistHistory.objects.bulk_create(histories) | ||
|
||
self.message_user( | ||
request, | ||
f"Created blacklist for {queryset.count()} guild{"s" if queryset.count() > 1 else ""}. " | ||
"This will be applied after reloading the bot's cache.", | ||
) | ||
async_to_sync(notify_admins)( | ||
f"{request.user} blacklisted guilds " | ||
f'{", ".join([str(x.guild_id) for x in queryset])} for the reason: {data["reason"]}.' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from django import forms | ||
from django.contrib import admin | ||
from django.db.models import Exists, OuterRef | ||
from django_admin_action_forms import AdminActionForm | ||
|
||
from .models import BlacklistedGuild, BlacklistedID, Player | ||
|
||
if TYPE_CHECKING: | ||
from django.db.models import QuerySet | ||
from django.http import HttpRequest | ||
|
||
from .admin import GuildAdmin, PlayerAdmin | ||
from .models import GuildConfig | ||
|
||
|
||
class BlacklistActionForm(AdminActionForm): | ||
reason = forms.CharField(label="Reason", required=True) | ||
|
||
|
||
class BlacklistedListFilter(admin.SimpleListFilter): | ||
title = "blacklisted" | ||
parameter_name = "blacklisted" | ||
|
||
def lookups( | ||
self, request: "HttpRequest", model_admin: "PlayerAdmin | GuildAdmin" | ||
) -> list[tuple[Any, str]]: | ||
return [(True, "True"), (False, "False")] | ||
|
||
def queryset( | ||
self, request: "HttpRequest", queryset: "QuerySet[Player | GuildConfig]" | ||
) -> "QuerySet[Player | GuildConfig]": | ||
if self.value() is None: | ||
return queryset | ||
if queryset.model == Player: | ||
annotation = Exists(BlacklistedID.objects.filter(discord_id=OuterRef("discord_id"))) | ||
else: | ||
annotation = Exists(BlacklistedGuild.objects.filter(discord_id=OuterRef("guild_id"))) | ||
|
||
return queryset.annotate(listed=annotation).filter(listed=self.value()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters