Skip to content

Commit

Permalink
feat: Refactor admin.py and models.py in apps/proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Dec 21, 2023
1 parent e82293e commit 595a6b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 37 deletions.
52 changes: 20 additions & 32 deletions apps/proxy/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.conf import settings
from django.contrib import admin, messages
from django.forms import ModelForm
from django.utils.safestring import mark_safe

from apps.proxy import models
from apps.sspanel.models import User
from apps.utils import traffic_format


class SSConfigInline(admin.StackedInline):
Expand Down Expand Up @@ -33,6 +32,16 @@ class OccupancyConfigInline(admin.StackedInline):
"occupancy_user_limit",
]

def get_formset(self, request, obj=None, **kwargs):
if obj:
traffic = traffic_format(obj.occupancy_config.occupancy_traffic)
help_texts = {
"occupancy_traffic": f"={traffic}",
}
print("obj", obj, kwargs)
kwargs.update({"help_texts": help_texts})
return super().get_formset(request, obj, **kwargs)


class RelayRuleInline(admin.TabularInline):
model = models.RelayRule
Expand All @@ -48,37 +57,7 @@ class RelayRuleInline(admin.TabularInline):
]


class ProxyNodeAdminForm(ModelForm):
def __init__(self, *args, **kwargs):
if "instance" in kwargs and kwargs["instance"]:
# NOTE trans model traffic to GB
kwargs["instance"].total_traffic = (
kwargs["instance"].total_traffic // settings.GB
)
kwargs["instance"].used_traffic = (
kwargs["instance"].used_traffic // settings.GB
)

super(ProxyNodeAdminForm, self).__init__(*args, **kwargs)
self.fields["used_traffic"].help_text = (
self.fields["used_traffic"].help_text + "单位GB"
)
self.fields["total_traffic"].help_text = (
self.fields["total_traffic"].help_text + "单位GB"
)

def clean_used_traffic(self):
used_traffic = self.cleaned_data.get("used_traffic")
return used_traffic * settings.GB

def clean_total_traffic(self):
total_traffic = self.cleaned_data.get("total_traffic")
return total_traffic * settings.GB


class ProxyNodeAdmin(admin.ModelAdmin):
form = ProxyNodeAdminForm

list_display = [
"__str__",
"link_addr",
Expand All @@ -102,6 +81,15 @@ class ProxyNodeAdmin(admin.ModelAdmin):
list_filter = ["node_type", "country", "provider_remark"]
actions = ["reset_port", "clear_traffic_logs", "toggle_enable"]

def get_form(self, request, obj=None, **kwargs):
if obj:
help_texts = {
"total_traffic": f"={obj.human_total_traffic}",
"used_traffic": f"={obj.human_used_traffic}",
}
kwargs.update({"help_texts": help_texts})
return super().get_form(request, obj, **kwargs)

def get_inlines(self, request, instance):
if not instance:
return self.all_inlines
Expand Down
10 changes: 5 additions & 5 deletions apps/proxy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class BaseNodeModel(BaseModel):
server = models.CharField("服务器地址", help_text="服务器地址", max_length=256)
enable = models.BooleanField("是否开启", default=True, db_index=True)
cost_price = models.DecimalField(
max_digits=10, decimal_places=2, verbose_name="每月成本价格", default=0
max_digits=10, decimal_places=2, verbose_name="成本", default=0
)

class Meta:
Expand Down Expand Up @@ -190,8 +190,8 @@ class ProxyNode(BaseNodeModel, SequenceMixin):
country = models.CharField(
"国家", default="CN", max_length=5, choices=c.COUNTRIES_CHOICES
)
used_traffic = models.BigIntegerField("已用流量", default=0)
total_traffic = models.BigIntegerField("总流量", default=settings.GB)
used_traffic = models.BigIntegerField("已用流量(单位字节)", default=0)
total_traffic = models.BigIntegerField("总流量(单位字节)", default=settings.GB)
enlarge_scale = models.DecimalField(
"倍率",
default=Decimal("1.0"),
Expand Down Expand Up @@ -845,7 +845,7 @@ class OccupancyConfig(BaseModel):
occupancy_price = models.DecimalField(
max_digits=10, decimal_places=2, verbose_name="价格"
)
occupancy_traffic = models.BigIntegerField(default=0, verbose_name="流量")
occupancy_traffic = models.BigIntegerField(default=0, verbose_name="流量(单位字节)")
occupancy_user_limit = models.PositiveIntegerField(verbose_name="用户数", default=0)

class Meta:
Expand Down Expand Up @@ -875,7 +875,7 @@ class UserProxyNodeOccupancy(BaseModel):
)
start_time = models.DateTimeField(auto_now_add=True, verbose_name="开始占用时间")
end_time = models.DateTimeField(null=False, blank=False, verbose_name="结束占用时间")
traffic_used = models.BigIntegerField(default=0, verbose_name="已用流量")
traffic_used = models.BigIntegerField(default=0, verbose_name="流量(单位字节)")
out_of_traffic = models.BooleanField(default=False, verbose_name="流量溢出")
occupancy_config_snapshot = models.JSONField(verbose_name="快照", default=dict)

Expand Down

0 comments on commit 595a6b4

Please sign in to comment.