Skip to content

Commit

Permalink
feat: opt admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jan 12, 2024
1 parent 6403561 commit bc3c4f6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
38 changes: 33 additions & 5 deletions apps/proxy/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.contrib import admin, messages
from django.db import models
from django.db.models import F
from django.utils.safestring import mark_safe

from apps import utils
from apps.proxy import models
from apps.sspanel.models import User
from apps.utils import traffic_format
Expand Down Expand Up @@ -257,26 +260,51 @@ def get_form(self, request, obj=None, **kwargs):


class UserProxyNodeOccupancyAdmin(admin.ModelAdmin):
class StatusFilter(admin.SimpleListFilter):
title = "状态"
parameter_name = "status"

def lookups(self, request, model_admin):
return (
("valid", "正常"),
("invalid", "失效"),
)

def queryset(self, request, queryset):
value = self.value()
if value == "valid":
return queryset.filter(
end_time__gt=utils.get_current_datetime()
).filter(used_traffic__lt=F("total_traffic"))
elif value == "invalid":
return queryset.exclude(
end_time__gt=utils.get_current_datetime()
).filter(used_traffic__lt=F("total_traffic"))
return queryset

list_display = [
"proxy_node",
"user",
"start_time",
"end_time",
"traffic_info",
"out_of_usage",
"status",
]
search_fields = ["user__username"]
list_filter = ["proxy_node", "user"]
list_filter = ["proxy_node", "user", StatusFilter]
list_per_page = 10
show_full_result_count = False

@admin.display(description="已用/总流量")
def traffic_info(self, instance):
return f"{traffic_format(instance.used_traffic)}/{traffic_format(instance.total_traffic)}"

@admin.display(description="是否超出")
def out_of_usage(self, instance):
return instance.out_of_usage()
@admin.display(description="状态")
def status(self, instance):
if instance.out_of_usage():
return "失效"
else:
return "正常"

def get_form(self, request, obj=None, **kwargs):
if obj:
Expand Down
12 changes: 11 additions & 1 deletion apps/sspanel/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class TicketMessageInline(admin.TabularInline):
"created_at",
]
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):
Expand All @@ -139,7 +144,12 @@ def status_info(self, instance):
return instance.status_with_message_count

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


Expand Down
3 changes: 2 additions & 1 deletion configs/default/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

# FOR SIMPLE UI
SIMPLEUI_ANALYSIS = False
SIMPLEUI_HOME_ACTION = False
SIMPLEUI_HOME_QUICK = True
SIMPLEUI_HOME_INFO = bool(os.getenv("SIMPLEUI_HOME_INFO", False))
SIMPLEUI_DEFAULT_ICON = bool(os.getenv("SIMPLEUI_DEFAULT_ICON", True))
SIMPLEUI_ICON = {
Expand Down Expand Up @@ -86,7 +88,6 @@
"开放API": "fas fa-server",
}


# TG 相关配置
TELEGRAM_BOT_NAME = os.getenv("TELEGRAM_BOT_NAME")
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
Expand Down

0 comments on commit bc3c4f6

Please sign in to comment.