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 22, 2024
1 parent d8d1da0 commit fbb3a90
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
44 changes: 40 additions & 4 deletions apps/sspanel/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from django.contrib import admin
from django.contrib import admin, messages
from django.contrib.auth.models import Group
from django.db.models.query import QuerySet
from django.http.request import HttpRequest
Expand Down Expand Up @@ -85,10 +85,44 @@ class PurchaseHistoryAdmin(admin.ModelAdmin):
class InviteCodeAdmin(admin.ModelAdmin):
list_display = ["code", "created_at", "used", "code_type"]
search_fields = ["code"]
list_per_page = 10
actions = ["batch_add"]
list_filter = ["used"]

def batch_add(self, request, queryset):
# todo support params
amount = request.POST.get("amount", 10)
number = request.POST.get("number", 10)
messages.add_message(
request,
messages.SUCCESS,
f"create code:{amount} x {number}",
)
models.InviteCode.batch_create(number, 1)

batch_add.short_description = "批量添加"
batch_add.type = "danger"


class MoneyCodeAdmin(admin.ModelAdmin):
list_display = ["user", "code", "isused"]
list_display = ["code", "number", "isused", "user"]
list_per_page = 10
actions = ["batch_add"]
list_filter = ["isused"]

def batch_add(self, request, queryset):
# todo support params
amount = request.POST.get("amount", 10)
number = request.POST.get("number", 10)
messages.add_message(
request,
messages.SUCCESS,
f"create code:={amount}x{number}",
)
models.MoneyCode.batch_create(amount, number)

batch_add.short_description = "批量添加"
batch_add.type = "danger"


class DonateAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -169,13 +203,15 @@ def save_model(


# Register your models here.
admin.site.register(models.MoneyCode, MoneyCodeAdmin)
admin.site.register(models.InviteCode, InviteCodeAdmin)


admin.site.register(models.User, UserAdmin)
admin.site.register(models.UserOrder, UserOrderAdmin)
admin.site.register(models.UserCheckInLog, UserCheckInAdmin)
admin.site.register(models.UserRefLog, UserRefLogAdmin)
admin.site.register(models.InviteCode, InviteCodeAdmin)
admin.site.register(models.Donate, DonateAdmin)
admin.site.register(models.MoneyCode, MoneyCodeAdmin)
admin.site.register(models.Goods, GoodsAdmin)
admin.site.register(models.PurchaseHistory, PurchaseHistoryAdmin)
admin.site.register(models.Announcement)
Expand Down
14 changes: 12 additions & 2 deletions apps/sspanel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ class InviteCode(models.Model):
used = models.BooleanField(verbose_name="是否使用", default=False)
created_at = models.DateTimeField(editable=False, auto_now_add=True)

@classmethod
def batch_create(cls, number, code_type):
models = [cls(code_type=code_type) for _ in range(number)]
cls.objects.bulk_create(models)

def __str__(self):
return f"<{self.user_id}>-<{self.code}>"

Expand Down Expand Up @@ -727,7 +732,7 @@ def get_most_donated_user_by_count(cls, count):
class MoneyCode(models.Model):
"""充值码"""

user = models.CharField(verbose_name="用户名", max_length=128, blank=True, null=True)
user = models.CharField(verbose_name="使用人", max_length=128, blank=True, null=True)
time = models.DateTimeField("捐赠时间", editable=False, auto_now_add=True)
code = models.CharField(
verbose_name="充值码",
Expand All @@ -737,7 +742,7 @@ class MoneyCode(models.Model):
default=get_long_random_string,
)
number = models.DecimalField(
verbose_name="捐赠金额",
verbose_name="金额",
decimal_places=2,
max_digits=10,
default=10,
Expand Down Expand Up @@ -766,6 +771,11 @@ def __str__(self):
def isused_cn(self):
return "已使用" if self.isused else "未使用"

@classmethod
def batch_create(cls, number, amount):
models = [cls(number=amount) for _ in range(number)]
cls.objects.bulk_create(models)


class Goods(models.Model):
"""商品"""
Expand Down

0 comments on commit fbb3a90

Please sign in to comment.