From fc99e525056e748f87bc7e666c426dc628369b50 Mon Sep 17 00:00:00 2001 From: chakcy <947105045@qq.com> Date: Wed, 29 Apr 2026 13:55:23 +0800 Subject: [PATCH] =?UTF-8?q?modify:=20=E4=BF=AE=E5=A4=8D=20static=5Ffiles.p?= =?UTF-8?q?y=20=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/core/constant.py | 49 +++++++++++++++++++ .../core/{static_file.py => static_files.py} | 0 2 files changed, 49 insertions(+) rename {{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/{static_file.py => static_files.py} (100%) diff --git a/{{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/constant.py b/{{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/constant.py index 0af4fb8..edc4468 100644 --- a/{{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/constant.py +++ b/{{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/constant.py @@ -44,3 +44,52 @@ class PermissionConstant: CUSTOMER_EXPORT = "customer:export" # 导出客户 ... + + @classmethod + def get_all(cls): + """ + 获取系统中定义的所有权限字符串列表 + + 该方法通过反射机制自动收集所有内部类中定义的权限常量, + 用于数据库同步、权限初始化等场景。 + + 工作流程: + 1. 遍历 Permission 类的所有属性 + 2. 跳过私有属性和非类属性 + 3. 对每个内部类,遍历其所有字符串类型的权限常量 + 4. 过滤出符合 "resource:action" 格式的权限标识 + + Returns: + list[str]: 所有权限字符串的列表,例如: + [ + 'sales_order:create', + 'sales_order:read', + 'customer:export', + ... + ] + + 使用示例: + >>> all_perms = Permission.get_all() + >>> len(all_perms) # 获取权限总数 + 85 + >>> 'sales_order:create' in all_perms # 检查权限是否存在 + True + + Note: + - 该方法会自动排除以下划线开头的私有属性 + - 只收集包含冒号的字符串值,确保是有效的权限标识 + - 返回的列表顺序取决于 dir() 函数的返回顺序 + """ + perms = [] + for attr_name in dir(cls): + attr = getattr(cls, attr_name) + if attr_name.startswith("_") or not hasattr(attr, "__dict__"): + continue + # 遍历内部类的属性 + for sub_attr in dir(attr): + if sub_attr.startswith("_"): + continue + val = getattr(attr, sub_attr) + if isinstance(val, str) and ":" in val: + perms.append(val) + return perms diff --git a/{{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/static_file.py b/{{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/static_files.py similarity index 100% rename from {{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/static_file.py rename to {{ cookiecutter.project_name }}/src/{{ cookiecutter.project_name }}/app/core/static_files.py