Skip to content

app.utils — business import facade

app/utils/__init__.py re-exports the symbols business modules use most often, so business code never needs from app.core.xxx import ... / from app.system.xxx import ... everywhere.

Strong rule

Business modules (app/business/<x>/) import via app.utils only. Inside app/system/, to avoid circular deps, keep importing app/core/* directly.

One-line import cheat sheet

python
from app.utils import (
    # ORM bases & enums
    BaseModel, AuditMixin, TreeMixin, SoftDeleteMixin,
    StatusType, IntEnum, StrEnum,

    # Schema bases & response wrappers
    SchemaBase, PageQueryBase,
    Success, Fail, SuccessExtra,
    CommonIds, OfflineByRoleRequest,
    ResponseModel, PageResponseModel,
    Custom, make_optional,

    # CRUD
    CRUDBase, get_db_conn,
    CRUDRouter, SearchFieldConfig,

    # Business code & exceptions
    Code, BizError, SchemaValidationError,

    # Auth & context
    DependAuth, DependPermission,
    require_buttons, require_roles,
    CTX_USER_ID,
    get_current_user, get_current_user_id,
    is_super_admin, has_role_code, has_button_code,

    # Data scope
    DataScopeType, build_scope_filter,

    # Events & state machine
    emit, on,
    StateMachine,

    # Sqids & field constraints
    encode_id, decode_id,
    SqidId, SqidPath,
    Int16, Int32, Int64,

    # Config & constants & log
    APP_SETTINGS, SUPER_ADMIN_ROLE, log,

    # Utilities
    camel_case_convert, snake_case_convert,
    to_camel_case, to_snake_case,
    to_lower_camel_case, to_upper_camel_case,
    time_to_timestamp, timestamp_to_time, orjson_dumps,

    # Monitoring
    radar_log,

    # Security
    create_access_token, get_password_hash, verify_password,
)

Categorized index

ORM bases & enums

SymbolDoc
BaseModel / AuditMixin / TreeMixin / SoftDeleteMixinMixins
StatusTypegeneric enable / disable / invalid / all enum
IntEnum / StrEnumbase for custom business enums (with get_member_values / get_name_by_value)

Schema bases & response

See Schema base.

CRUD

SymbolDoc
CRUDBase / get_db_connCRUDBase
CRUDRouter / SearchFieldConfigCRUDRouter

Business code & exceptions

SymbolUse
Code.SUCCESS / Code.HR_INVALID_TRANSITION / ...Response codes
BizError(code, msg)raise from any layer; global handler returns Fail
SchemaValidationError(code, msg)raise inside Pydantic validators (bypasses Pydantic's own catch)

Auth & context

See Auth / dependencies and Auth / context utilities.

Data scope

See Data scope.

Events & state machine

SymbolDoc
emit(event, **kwargs) / on(event)Event bus
StateMachineState machine

Sqids & constraints

SymbolDoc
SqidId / SqidPath / encode_id / decode_idSqids
Int16 / Int32 / Int64Schema / constraint types

Config & constants & log

SymbolUse
APP_SETTINGSAPP_SETTINGS.SECRET_KEY / .DB_URL / ..., see Configuration
SUPER_ADMIN_ROLEstring "R_SUPER"
logLoguru instance

Monitoring

python
from app.utils import radar_log

radar_log("employee state changed", data={"empId": emp.id, "to": "active"})
radar_log("permission denied", level="ERROR", data={...})

Writes to the in-house Radar monitoring; visible at /manage/radar/*. See Monitoring (Radar).

Security

SymbolUse
get_password_hash(plain) -> strArgon2 hash
verify_password(plain, hashed) -> boolverify
create_access_token(payload, expires=...) -> strfor custom tokens (login uses build_tokens)

Not exported

Not exportedWhy / alternative
Specific functions in app.core.cache.*system-only; business writes its own cache_utils.py
app.core.redis.*get the Redis instance from request.app.state.redis
app.core.middlewares.*business shouldn't add middleware
app.core.init_app.*framework startup only
app.system.models.*keep modules decoupled; expose via system services if needed

If you do need them, import from the source path directly — but reconsider whether you're breaking the "business doesn't depend on system internals" boundary.

基于 MIT 协议发布