seed_admin_assignments 禁止默认批量;任职角色仅作标签说明

This commit is contained in:
XingQue
2026-06-24 23:13:03 +08:00
parent 77e1d3f293
commit 49482830f9
2 changed files with 53 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
"""为后台客服账号初始化 admin_assignment数据范围非功能权限"""
import logging
from django.core.management.base import BaseCommand
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from jituan.constants import CLUB_ID_DEFAULT, DATA_SCOPE_SINGLE, ADMIN_ROLE_LABELS
@@ -19,7 +19,15 @@ class Command(BaseCommand):
'--club-id', default=CLUB_ID_DEFAULT, help='目标俱乐部 ID默认 xq',
)
parser.add_argument(
'--role-code', default='CLUB_ADMIN', help='任职角色码',
'--role-code', default='CLUB_ADMIN', help='任职角色码(仅展示标签,不控制菜单功能权限)',
)
parser.add_argument(
'--phone', type=str, default='', help='仅给该手机号客服添加任职(推荐)',
)
parser.add_argument(
'--all-kefu',
action='store_true',
help='给全部客服账号批量添加(慎用,会扫所有有 KefuProfile 的账号)',
)
parser.add_argument(
'--dry-run', action='store_true', help='只打印不写入',
@@ -29,9 +37,24 @@ class Command(BaseCommand):
club_id = options['club_id']
role_code = options['role_code']
dry_run = options['dry_run']
target_phone = (options.get('phone') or '').strip()
all_kefu = options.get('all_kefu')
# UserType 是属性不是 DB 字段,通过 KefuProfile 关联筛选客服账号
kefu_users = User.query.filter(KefuProfile__isnull=False).select_related('KefuProfile')
if not target_phone and not all_kefu:
raise CommandError(
'请指定 --phone 13800138000只配一个人或 --all-kefu批量全部客服'
'默认不会自动给所有人加任职。'
)
if target_phone:
target = User.query.filter(Phone=target_phone).first()
if not target:
raise CommandError(f'未找到手机号 {target_phone}')
kefu_users = [target]
else:
kefu_users = list(
User.query.filter(KefuProfile__isnull=False).select_related('KefuProfile')
)
created = 0
skipped = 0

View File

@@ -1,19 +1,24 @@
"""一键初始化星之界(xzj)俱乐部及客服数据范围"""
"""一键初始化星之界(xzj)俱乐部(默认不批量给所有客服加任职)"""
from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import BaseCommand
from jituan.models import Club
class Command(BaseCommand):
help = '创建星之界电竞俱乐部(xzj)并分配客服数据范围(需先 seed_club_xq'
help = '创建星之界电竞俱乐部(xzj);任职请单独 seed_admin_assignments --phone ...'
def add_arguments(self, parser):
parser.add_argument(
'--wx-appid', default='wxdefa454152e78a03', help='星之界小程序 appid',
)
parser.add_argument(
'--skip-assignments', action='store_true', help='跳过客服 admin_assignment',
'--assignments',
action='store_true',
help='同时给 --phone 指定客服加 xzj 任职(需配合 --phone',
)
parser.add_argument(
'--phone', type=str, default='', help='与 --assignments 联用:客服手机号',
)
def handle(self, *args, **options):
@@ -32,15 +37,25 @@ class Command(BaseCommand):
wx_appid=wx_appid,
)
if not options.get('skip_assignments'):
self.stdout.write('分配客服数据范围 ...')
try:
call_command('seed_admin_assignments', club_id=club_id)
except Exception as e:
if options.get('assignments'):
phone = (options.get('phone') or '').strip()
if not phone:
self.stdout.write(self.style.WARNING(
f'客服数据范围分配失败(可稍后在后台「数据范围」页手动配置): {e}'
'未指定 --phone跳过任职。示例'
'python manage.py seed_club_xzj --assignments --phone 你的手机号'
))
else:
call_command(
'seed_admin_assignments',
club_id=club_id,
phone=phone,
)
else:
self.stdout.write(
'未加任职。需要时在后台「数据范围」配置,或:'
'python manage.py seed_admin_assignments --phone 手机号 --club-id xzj'
)
self.stdout.write(self.style.SUCCESS(
f'星之界 {club_id} 就绪。请在后台「俱乐部配置」检查支付密钥,并在 xzj 视图下单独改轮播/会员价'
f'星之界 {club_id} 就绪。请在后台「俱乐部配置」检查支付密钥。'
))