Skip to content

Commit

Permalink
admin: opt TicketAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Feb 27, 2024
1 parent 21a51e5 commit 2819b2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
30 changes: 19 additions & 11 deletions apps/sspanel/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from django.contrib.auth.models import Group
from django.db.models.query import QuerySet
from django.http.request import HttpRequest
from django.utils.html import format_html

from apps.sub import UserSubManager

from . import models

Expand Down Expand Up @@ -160,10 +163,6 @@ class TicketMessageInline(admin.TabularInline):
extra = 0
raw_id_fields = ["user"]

def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.prefetch_related("user")


class TicketAdmin(admin.ModelAdmin):
ALREADY_REPLIED = " | 已回复"
Expand All @@ -172,7 +171,7 @@ class TicketAdmin(admin.ModelAdmin):
list_display = ["user_info", "title", "status_info", "updated_at"]
list_filter = ["status"]
search_fields = ["title", "user"]
readonly_fields = ["updated_at"]
readonly_fields = ["user_details"]
list_per_page = 10

@admin.display(description="状态")
Expand All @@ -190,13 +189,22 @@ def user_info(self, instance):
user = instance.user
return f"{user.username}-{user.level}-{user.balance}"

@admin.display(description="用户详细信息")
def user_details(self, instance):
user = instance.user
base_sub_link = user.sub_link
html = '<table class="table">'
html += "<thead><tr><th>Client</th><th>Link</th></tr></thead>"
html += "<tbody>"
for client in UserSubManager.CLIENT_SET:
link = f"{base_sub_link}&client={client}"
html += format_html(f"<tr><td>{client}</td><td>{link}</td>")
html += "</tbody>"
html += "</table>"
return format_html(html)

def get_queryset(self, request: HttpRequest) -> QuerySet[Any]:
qs = (
super()
.get_queryset(request)
.prefetch_related("user", "messages")
.prefetch_related("messages__user")
)
qs = super().get_queryset(request).prefetch_related("messages__user", "user")
return qs

def save_model(
Expand Down
10 changes: 7 additions & 3 deletions apps/sspanel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,11 @@ class Ticket(models.Model):
verbose_name="状态", choices=TICKET_CHOICE, default=1
)
updated_at = models.DateTimeField(
auto_now=True, db_index=True, help_text="更新时间", verbose_name="更新时间"
auto_now=True,
db_index=True,
help_text="更新时间",
verbose_name="更新时间",
editable=False,
)

def __str__(self):
Expand All @@ -1035,11 +1039,11 @@ class Meta:
@classmethod
def close_stale_tickets(cls):
dt = get_current_datetime().subtract(days=3)
tickets = cls.objects.filter(updated_at__lt=dt, status=1)
tickets = list(cls.objects.filter(updated_at__lt=dt, status=1))
for t in tickets:
t.title += " |3 天无更新自动关闭"
t.status = -1
t.save()
cls.objects.bulk_update(tickets, ["title", "status"])

def add_message(self, user, message):
return TicketMessage.create_message(user, self, message)
Expand Down

0 comments on commit 2819b2d

Please sign in to comment.