|
1 | | -from models import db |
| 1 | +import questionary |
| 2 | +from rich.console import Console |
| 3 | +from rich.panel import Panel |
| 4 | +from rich.text import Text |
2 | 5 | from werkzeug.security import generate_password_hash |
3 | 6 |
|
| 7 | +from models import db |
| 8 | + |
4 | 9 | # 导入相关数据表模型 |
5 | 10 | from .users import Users |
6 | 11 | from .departments import Departments |
7 | 12 | from configs import AuthConfig |
8 | 13 |
|
| 14 | +# 创建rich console实例 |
| 15 | +console = Console() |
| 16 | + |
9 | 17 | # 创建表(如果表不存在) |
10 | 18 | db.create_tables([Users, Departments]) |
11 | 19 |
|
| 20 | +# 统一的questionary样式 |
| 21 | +custom_style = questionary.Style( |
| 22 | + [ |
| 23 | + ("qmark", "fg:#FFD700 bold"), |
| 24 | + ("question", "bold cyan"), |
| 25 | + ("answer", "bold green"), |
| 26 | + ("pointer", "fg:#FFD700 bold"), |
| 27 | + ("highlighted", "fg:#00FFFF bold"), |
| 28 | + ("selected", "fg:#00FF00 bold"), |
| 29 | + ("instruction", "fg:#888"), |
| 30 | + ] |
| 31 | +) |
| 32 | + |
| 33 | + |
12 | 34 | if __name__ == "__main__": |
13 | | - # 重置数据库users表,并初始化管理员用户 |
14 | | - # 命令:python -m models.init_db |
| 35 | + # 显示标题面板 |
| 36 | + title = Text("数据库初始化工具", style="bold cyan") |
| 37 | + subtitle = Text("magic-dash-pro", style="dim") |
| 38 | + console.print( |
| 39 | + Panel(title, subtitle=subtitle, border_style="bright_blue", padding=(1, 2)) |
| 40 | + ) |
| 41 | + |
| 42 | + # 记录执行结果和管理员信息 |
| 43 | + executed_operations = [] |
| 44 | + admin_created = False |
15 | 45 |
|
16 | 46 | # 1. 询问是否重置部门表 |
17 | | - confirm_departments = input( |
18 | | - "是否重置 \033[93m部门信息\033[0m 表?(输入 \033[93myes\033[0m 确认,其他内容跳过): " |
19 | | - ) |
20 | | - if confirm_departments == "yes": |
21 | | - Departments.truncate_departments(execute=True) |
22 | | - print("\033[93m部门信息\033[0m 表已重置") |
23 | | - else: |
24 | | - print("已跳过 \033[93m部门信息\033[0m 表重置") |
| 47 | + console.print("\n[dim]请确认以下数据库操作:[/dim]\n") |
| 48 | + confirm_departments = questionary.confirm( |
| 49 | + "是否重置部门信息表?", |
| 50 | + default=False, |
| 51 | + style=custom_style, |
| 52 | + ).ask() |
25 | 53 |
|
26 | | - print("\n") |
| 54 | + if confirm_departments: |
| 55 | + try: |
| 56 | + Departments.truncate_departments(execute=True) |
| 57 | + executed_operations.append(("部门信息表", "已重置", "yellow", "green")) |
| 58 | + except Exception as e: |
| 59 | + executed_operations.append( |
| 60 | + ("部门信息表", f"重置失败: {str(e)}", "yellow", "red") |
| 61 | + ) |
| 62 | + else: |
| 63 | + executed_operations.append(("部门信息表", "已跳过", "dim", "dim")) |
27 | 64 |
|
28 | 65 | # 2. 询问是否重置用户表 |
29 | | - confirm_users = input( |
30 | | - "是否重置 \033[93m用户信息\033[0m 表?(输入 \033[93myes\033[0m 确认,其他内容跳过): " |
31 | | - ) |
32 | | - if confirm_users == "yes": |
33 | | - Users.truncate_users(execute=True) |
34 | | - print("\033[93m用户信息\033[0m 表已重置") |
35 | | - Users.add_user( |
36 | | - user_id="admin", |
37 | | - user_name="admin", |
38 | | - password_hash=generate_password_hash("admin123"), |
39 | | - user_role=AuthConfig.admin_role, |
40 | | - ) |
41 | | - print( |
42 | | - "管理员用户 \033[93madmin\033[0m 初始化完成,初始密码:\033[93madmin123\033[0m" |
43 | | - ) |
| 66 | + confirm_users = questionary.confirm( |
| 67 | + "是否重置用户信息表?", |
| 68 | + default=False, |
| 69 | + style=custom_style, |
| 70 | + ).ask() |
| 71 | + |
| 72 | + if confirm_users: |
| 73 | + try: |
| 74 | + Users.truncate_users(execute=True) |
| 75 | + |
| 76 | + # 初始化管理员用户 |
| 77 | + Users.add_user( |
| 78 | + user_id="admin", |
| 79 | + user_name="admin", |
| 80 | + password_hash=generate_password_hash("admin123"), |
| 81 | + user_role=AuthConfig.admin_role, |
| 82 | + ) |
| 83 | + |
| 84 | + admin_created = True |
| 85 | + executed_operations.append( |
| 86 | + ("用户信息表", "已重置 + 管理员初始化", "yellow", "green") |
| 87 | + ) |
| 88 | + except Exception as e: |
| 89 | + executed_operations.append( |
| 90 | + ("用户信息表", f"重置失败: {str(e)}", "yellow", "red") |
| 91 | + ) |
44 | 92 | else: |
45 | | - print("已跳过 \033[93m用户信息\033[0m 表重置") |
| 93 | + executed_operations.append(("用户信息表", "已跳过", "dim", "dim")) |
| 94 | + |
| 95 | + # 显示操作结果汇总 |
| 96 | + console.print("\n") |
| 97 | + result_text = Text() |
| 98 | + for i, (table, status, table_style, status_style) in enumerate( |
| 99 | + executed_operations, 1 |
| 100 | + ): |
| 101 | + result_text.append(f" {i}. ", style="dim") |
| 102 | + result_text.append(f"{table}: ", style=table_style) |
| 103 | + result_text.append(f"{status}\n", style=status_style) |
| 104 | + |
| 105 | + # 如果有管理员账号创建,在汇总中添加账号信息 |
| 106 | + if admin_created: |
| 107 | + result_text.append("\n", style="default") |
| 108 | + result_text.append(" 初始管理员账号", style="cyan") |
| 109 | + result_text.append(":\n", style="default") |
| 110 | + result_text.append(" 用户名: ", style="dim") |
| 111 | + result_text.append("admin\n", style="yellow bold") |
| 112 | + result_text.append(" 初始密码: ", style="dim") |
| 113 | + result_text.append("admin123\n", style="yellow bold") |
| 114 | + |
| 115 | + console.print( |
| 116 | + Panel( |
| 117 | + result_text, |
| 118 | + title="[bold green]\u2713 操作完成[/bold green]", |
| 119 | + border_style="bright_green", |
| 120 | + padding=(1, 2), |
| 121 | + ) |
| 122 | + ) |
0 commit comments