nex_buddy/server/app/models/user.py

67 lines
3.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""用户:账号、资料、角色与状态(01-总体设计.md 4.2)。"""
from __future__ import annotations
import uuid
from datetime import date, datetime, time, timezone
from typing import TYPE_CHECKING
from sqlalchemy import Date, DateTime, Numeric, String, Time, func
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.session import Base
if TYPE_CHECKING:
from app.models.device import Device
def _utcnow() -> datetime:
return datetime.now(timezone.utc)
class User(Base):
__tablename__ = "users"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
# 邮箱或手机号二选一,注册时按是否含 @ 判定
email: Mapped[str | None] = mapped_column(String(190), unique=True, index=True)
phone: Mapped[str | None] = mapped_column(String(32), unique=True, index=True)
password_hash: Mapped[str] = mapped_column(String(128))
nickname: Mapped[str] = mapped_column(String(64), default="")
real_name: Mapped[str | None] = mapped_column(String(64))
# 出生地:只存码表 code,名字另存一份是为了列表/设备侧免 join
birthday: Mapped[date | None] = mapped_column(Date)
birth_time: Mapped[time | None] = mapped_column(Time)
birth_city: Mapped[str | None] = mapped_column(String(64)) # 市级名,天气用它
birth_region_code: Mapped[str | None] = mapped_column(String(12))
birth_place: Mapped[str | None] = mapped_column(String(128)) # 完整路径:浙江省 杭州市 西湖区
# 居住地(决定日常天气与运势的「当月」,可与出生地不同)
residence_region_code: Mapped[str | None] = mapped_column(String(12))
residence_city: Mapped[str | None] = mapped_column(String(64))
residence_place: Mapped[str | None] = mapped_column(String(128))
# 居住时区(IANA 名,如 Asia/Shanghai)。空则按居住地码表时区,再兜底内地标准时。
# 只影响「今天是几号/几月」这类日常语义;星盘用的是出生地时区,与这里无关。
timezone: Mapped[str | None] = mapped_column(String(64))
# 星盘结果(保存资料时算好落库,设备直接读,不用现场算)
sun_sign: Mapped[str | None] = mapped_column(String(16))
moon_sign: Mapped[str | None] = mapped_column(String(16))
sun_degree: Mapped[float | None] = mapped_column(Numeric(6, 2))
moon_degree: Mapped[float | None] = mapped_column(Numeric(6, 2))
role: Mapped[str] = mapped_column(String(16), default="user") # user / admin
status: Mapped[str] = mapped_column(String(16), default="active") # active / disabled
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=_utcnow
)
devices: Mapped[list["Device"]] = relationship(back_populates="bound_user")