Skip to content

Commit

Permalink
guild: display catch history
Browse files Browse the repository at this point in the history
  • Loading branch information
laggron42 committed Jan 18, 2025
1 parent d8c37ca commit 83c6ac3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
45 changes: 43 additions & 2 deletions admin_panel/bd_models/admin/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,60 @@

from asgiref.sync import async_to_sync
from django.contrib import admin, messages
from django.contrib.admin.utils import quote
from django.urls import reverse
from django.utils.html import format_html
from django_admin_action_forms import action_with_form
from django_admin_inline_paginator.admin import InlinePaginated
from nonrelated_inlines.admin import NonrelatedInlineMixin

from admin_panel.webhook import notify_admins

from ..forms import BlacklistActionForm, BlacklistedListFilter
from ..models import BlacklistedGuild, BlacklistHistory, GuildConfig
from ..models import BallInstance, BlacklistedGuild, BlacklistHistory, GuildConfig
from ..utils import BlacklistTabular

if TYPE_CHECKING:
from django.db.models import QuerySet
from django.http import HttpRequest


class BallInstanceGuildTabular(InlinePaginated, NonrelatedInlineMixin, admin.TabularInline):
model = BallInstance
fk_name = "player"
per_page = 50
ordering = ("-catch_date",)
fields = ("description", "player", "catch_time", "catch_date")
readonly_fields = ("description", "player", "catch_time", "catch_date")
show_change_link = True
classes = ("collapse",)
can_delete = False

def get_form_queryset(self, obj: GuildConfig):
return BallInstance.objects.filter(server_id=obj.guild_id).prefetch_related("player")

@admin.display(description="Time to catch")
def catch_time(self, obj: BallInstance):
if obj.spawned_time:
return str(obj.spawned_time - obj.catch_date)
return "-"

# adding a countryball cannot work from here since all fields are readonly
def has_add_permission(self, request: "HttpRequest", obj: GuildConfig) -> bool:
return False

@admin.display(description="Player")
def player(self, obj: BallInstance):
opts = obj.player._meta
admin_url = reverse(
"%s:%s_%s_change" % (self.admin_site.name, opts.app_label, opts.model_name),
None,
(quote(obj.player.pk),),
)
# Display a link to the admin page.
return format_html(f'<a href="{admin_url}">{obj.player}</a>')


@admin.register(GuildConfig)
class GuildAdmin(admin.ModelAdmin):
list_display = ("guild_id", "spawn_channel", "enabled", "silent", "blacklisted")
Expand All @@ -24,7 +65,7 @@ class GuildAdmin(admin.ModelAdmin):
search_fields = ("guild_id", "spawn_channel")
search_help_text = "Search by guild ID or spawn channel ID"

inlines = (BlacklistTabular,)
inlines = (BlacklistTabular, BallInstanceGuildTabular)
actions = ("blacklist_guilds",)

@admin.display(description="Is blacklisted", boolean=True)
Expand Down
2 changes: 1 addition & 1 deletion admin_panel/bd_models/admin/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def catch_time(self, obj: BallInstance):
return "-"

# adding a countryball cannot work from here since all fields are readonly
def has_add_permission(self, request: "HttpRequest", obj: "Player | GuildConfig") -> bool:
def has_add_permission(self, request: "HttpRequest", obj: "Player") -> bool:
return False

@admin.display(description="Server")
Expand Down

0 comments on commit 83c6ac3

Please sign in to comment.