Files
Django/jituan/services/team_recruit_switch.py
T

56 lines
1.6 KiB
Python

"""俱乐部:是否允许打手组队(config_json)。
默认关闭:未配置或显式关闭时,打手不可开启/加入组队。
"""
import logging
logger = logging.getLogger(__name__)
CONFIG_KEY = 'dashou_team_recruit_enabled'
def _club(club_id: str):
cid = (club_id or '').strip()
if not cid:
return None
from jituan.models import Club
club = Club.query.filter(club_id=cid).first()
if not club and cid != cid.lower():
club = Club.query.filter(club_id=cid.lower()).first()
return club
def _as_bool(raw, default=False) -> bool:
if raw is None:
return default
if isinstance(raw, str):
return raw.strip().lower() in ('1', 'true', 'yes', 'on')
return bool(raw)
def is_dashou_team_recruit_enabled(club_id: str) -> bool:
"""开启=允许本俱乐部打手组队;关闭(默认)=不可开队/入队。"""
club = _club(club_id)
if not club:
return False
try:
cfg = club.config_json or {}
if CONFIG_KEY not in cfg:
return False
return _as_bool(cfg.get(CONFIG_KEY), False)
except Exception:
logger.exception('read dashou_team_recruit_enabled failed club=%s', club_id)
return False
def set_dashou_team_recruit_enabled(club, enabled: bool) -> None:
cfg = dict(getattr(club, 'config_json', None) or {})
cfg[CONFIG_KEY] = bool(enabled)
club.config_json = cfg
def ensure_dashou_team_recruit_enabled(club_id: str) -> None:
"""未开启则抛 ValueError,供组队服务统一拦截。"""
if not is_dashou_team_recruit_enabled(club_id):
raise ValueError('本俱乐部未开放组队功能')