Publish reviewed history-free open-source snapshot (2026-10-12)
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import transaction
|
||||
|
||||
from control.models import PlanApplication
|
||||
|
||||
from .models import LoginAttempt, User, WeComBinding, WeComBindingEvent
|
||||
|
||||
|
||||
@admin.register(WeComBinding)
|
||||
class WeComBindingAdmin(admin.ModelAdmin):
|
||||
list_display = ("user", "kind", "created_at", "last_login_at")
|
||||
list_filter = ("kind",)
|
||||
search_fields = ("user__username", "user__email")
|
||||
readonly_fields = ("user", "corp_id", "kind", "subject_id", "created_at", "last_login_at")
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
|
||||
@admin.register(WeComBindingEvent)
|
||||
class WeComBindingEventAdmin(admin.ModelAdmin):
|
||||
list_display = ("user", "action", "created_at")
|
||||
readonly_fields = ("user", "action", "identity_digest", "created_at")
|
||||
list_filter = ("action",)
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
|
||||
def has_change_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
|
||||
def _approve_users_with_plan(modeladmin, request, queryset, identity_type):
|
||||
success = 0
|
||||
for selected_user in queryset:
|
||||
try:
|
||||
with transaction.atomic():
|
||||
user = User.objects.select_for_update().get(pk=selected_user.pk)
|
||||
applications = list(
|
||||
PlanApplication.objects.select_for_update()
|
||||
.filter(user=user, status=PlanApplication.Status.PENDING)
|
||||
.order_by("-created_at", "-pk")
|
||||
[:2]
|
||||
)
|
||||
if not applications:
|
||||
raise ValidationError("没有待审核的套餐申请,账号未启用。")
|
||||
if len(applications) > 1:
|
||||
raise ValidationError("存在多个待审核套餐申请,请核对后单独审核。")
|
||||
application = applications[0]
|
||||
user.identity_type = identity_type
|
||||
user.save(update_fields=["identity_type"])
|
||||
application.approve(request.user)
|
||||
success += 1
|
||||
except ValidationError as exc:
|
||||
modeladmin.message_user(request, f"{selected_user}: {exc}", level=messages.ERROR)
|
||||
if success:
|
||||
modeladmin.message_user(
|
||||
request,
|
||||
f"已审核 {success} 个账号,并自动创建或更新订阅。",
|
||||
level=messages.SUCCESS,
|
||||
)
|
||||
|
||||
|
||||
@admin.action(description="设为内部员工并启用")
|
||||
def approve_as_employee(modeladmin, request, queryset):
|
||||
_approve_users_with_plan(modeladmin, request, queryset, User.IdentityType.EMPLOYEE)
|
||||
|
||||
|
||||
@admin.action(description="设为外部联系人并启用")
|
||||
def approve_as_external(modeladmin, request, queryset):
|
||||
_approve_users_with_plan(modeladmin, request, queryset, User.IdentityType.EXTERNAL)
|
||||
|
||||
|
||||
@admin.action(description="暂停服务")
|
||||
def suspend_users(modeladmin, request, queryset):
|
||||
queryset.update(account_status=User.AccountStatus.SUSPENDED)
|
||||
|
||||
|
||||
@admin.action(description="标记离职")
|
||||
def mark_departed(modeladmin, request, queryset):
|
||||
for user in queryset:
|
||||
user.account_status = User.AccountStatus.DEPARTED
|
||||
user.save(update_fields=["account_status"])
|
||||
for subscription in user.subscriptions.exclude(status__in=["cancelled", "expired"]):
|
||||
subscription.cancel(reason="用户被标记为离职")
|
||||
|
||||
|
||||
@admin.register(User)
|
||||
class TopLCUserAdmin(UserAdmin):
|
||||
list_display = (
|
||||
"username",
|
||||
"display_name",
|
||||
"email",
|
||||
"identity_type",
|
||||
"account_status",
|
||||
"is_staff",
|
||||
"date_joined",
|
||||
)
|
||||
list_filter = ("identity_type", "account_status", "is_staff", "is_active")
|
||||
search_fields = ("username", "display_name", "email", "phone", "company_name")
|
||||
ordering = ("-date_joined",)
|
||||
actions = (approve_as_employee, approve_as_external, suspend_users, mark_departed)
|
||||
fieldsets = UserAdmin.fieldsets + (
|
||||
(
|
||||
"业务身份",
|
||||
{
|
||||
"fields": (
|
||||
"display_name",
|
||||
"phone",
|
||||
"company_name",
|
||||
"identity_type",
|
||||
"account_status",
|
||||
"approved_at",
|
||||
"approved_by",
|
||||
)
|
||||
},
|
||||
),
|
||||
("未来 Odoo 对接", {"fields": ("odoo_user_id", "odoo_partner_id"), "classes": ("collapse",)}),
|
||||
)
|
||||
add_fieldsets = UserAdmin.add_fieldsets + (
|
||||
(
|
||||
"业务身份",
|
||||
{
|
||||
"fields": (
|
||||
"display_name",
|
||||
"email",
|
||||
"phone",
|
||||
"company_name",
|
||||
"identity_type",
|
||||
"account_status",
|
||||
)
|
||||
},
|
||||
),
|
||||
)
|
||||
readonly_fields = ("approved_at", "approved_by")
|
||||
|
||||
|
||||
@admin.register(LoginAttempt)
|
||||
class LoginAttemptAdmin(admin.ModelAdmin):
|
||||
list_display = ("key_preview", "failures", "blocked_until", "updated_at")
|
||||
readonly_fields = ("key", "failures", "window_started_at", "blocked_until", "updated_at")
|
||||
actions = ("clear_blocks",)
|
||||
|
||||
def key_preview(self, obj):
|
||||
return f"{obj.key[:16]}…"
|
||||
|
||||
key_preview.short_description = "来源摘要"
|
||||
|
||||
@admin.action(description="清除所选登录限制")
|
||||
def clear_blocks(self, request, queryset):
|
||||
queryset.delete()
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
Reference in New Issue
Block a user