refactor: 移除 SMTP 相关代码,简化用户创建流程,更新文档

This commit is contained in:
2026-04-13 12:27:14 +08:00
parent 919c561629
commit 66c254e0ff
13 changed files with 41 additions and 466 deletions
+3 -49
View File
@@ -12,28 +12,12 @@ from rich.table import Table
from tjhlp.client import GiteaClient, GiteaClientError
from tjhlp.config import get_gitea_config
from tjhlp.models import CredentialRecord, MemberRecord, MemberRole
from tjhlp.models import MemberRecord, MemberRole
app = typer.Typer(no_args_is_help=True)
console = Console()
def _generate_password(length: int = 12) -> str:
"""生成随机密码: 至少包含大小写字母、数字和特殊字符"""
if length < 8:
length = 8
alphabet = string.ascii_letters + string.digits + "!@#$%&*"
while True:
password = "".join(secrets.choice(alphabet) for _ in range(length))
if (
any(c.islower() for c in password)
and any(c.isupper() for c in password)
and any(c.isdigit() for c in password)
and any(c in "!@#$%&*" for c in password)
):
return password
def _read_members_csv(filepath: Path) -> list[MemberRecord]:
"""读取 members CSV 文件"""
records: list[MemberRecord] = []
@@ -48,10 +32,9 @@ def _read_members_csv(filepath: Path) -> list[MemberRecord]:
def create_users(
file: Path = typer.Option(..., "--file", "-f", help="成员名单 CSV 文件路径"),
org: str = typer.Option("", "--org", help="自动添加到指定组织的对应团队"),
output: Path = typer.Option(None, "--output", "-o", help="凭据输出文件路径(默认自动生成)"),
dry_run: bool = typer.Option(False, "--dry-run", help="仅预览, 不实际创建"),
) -> None:
"""从 CSV 批量创建用户账号, 生成随机密码并输出到凭据文件"""
"""从 CSV 批量创建用户账号(初始密码与用户名相同)"""
if not file.exists():
console.print(f"[red]文件不存在: {file}[/red]")
raise typer.Exit(code=1)
@@ -75,7 +58,6 @@ def create_users(
console.print(table)
return
credentials: list[CredentialRecord] = []
stats = {"created": 0, "skipped": 0, "failed": 0}
with GiteaClient(cfg) as client:
@@ -94,12 +76,10 @@ def create_users(
stats["skipped"] += 1
continue
password = _generate_password(cfg.password_length)
try:
client.create_user(
username=member.username,
password=password,
password=member.username,
email=member.email,
full_name=member.name,
must_change_password=True,
@@ -107,17 +87,6 @@ def create_users(
console.print(f" [green]创建[/green] {member.username}({member.name})")
stats["created"] += 1
credentials.append(
CredentialRecord(
id=member.id,
name=member.name,
role=member.role.value,
username=member.username,
password=password,
email=member.email,
)
)
# 添加到团队
if org:
team = ta_team if member.role == MemberRole.TA else students_team
@@ -131,21 +100,6 @@ def create_users(
console.print(f" [red]失败[/red] {member.username}({member.name}): {e.message}")
stats["failed"] += 1
# 写出凭据文件
if credentials:
if output is None:
ts = datetime.now(tz=UTC).strftime("%Y%m%d_%H%M%S")
output = Path(f"credentials_{ts}.csv")
with open(output, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(
f,
fieldnames=["id", "name", "role", "username", "password", "email"],
)
writer.writeheader()
for cred in credentials:
writer.writerow(cred.model_dump())
console.print(f"\n凭据已保存到 [bold]{output}[/bold]")
console.print(
f"\n完成: 创建 {stats['created']}, 跳过 {stats['skipped']}, 失败 {stats['failed']}"
)