70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
"""
|
|
사용자 모델
|
|
"""
|
|
|
|
import uuid
|
|
from typing import Dict, Any
|
|
from extensions import db
|
|
from utils.func import get_timestamp_ms
|
|
import constants
|
|
|
|
class Users(db.Model):
|
|
"""사용자 모델"""
|
|
|
|
__tablename__ = "users"
|
|
|
|
# 기본 필드
|
|
user_uuid = db.Column(db.String(255), primary_key=True, nullable=False, comment="유저 UUID")
|
|
id = db.Column(db.String(255), unique=True, nullable=False, index=True, comment="유저 ID")
|
|
user_name = db.Column(db.String(50), nullable=True, comment="유저명")
|
|
password = db.Column(db.String(255), nullable=False, comment="비밀번호")
|
|
auth_level = db.Column(db.String(255), nullable=True, default=constants.AUTH_LEVEL_USER, comment="권한 레벨 (10: 관리자, 20: 사용자)")
|
|
count = db.Column(db.SmallInteger, default=0, nullable=True, comment="로그인 싫패 횟수")
|
|
|
|
# 타임스탬프 (BIGINT - Unix Timestamp in milliseconds)
|
|
created_at = db.Column(db.BigInteger, nullable=True, comment="생성일 (unix timestamp, ms)")
|
|
updated_at = db.Column(db.BigInteger, nullable=True, comment="수정일 (unix timestamp, ms)")
|
|
|
|
def __init__(self, id: str, hash_password: str, **kwargs: Any) -> None:
|
|
"""사용자 초기화"""
|
|
self.user_uuid = str(uuid.uuid4())
|
|
self.id = id
|
|
self.password = hash_password
|
|
|
|
# 선택적 필드
|
|
self.user_name = kwargs.get("user_name")
|
|
|
|
# 타임스탬프
|
|
current_time = int(get_timestamp_ms())
|
|
self.created_at = current_time
|
|
self.updated_at = current_time
|
|
|
|
def increment_failed_login(self) -> None:
|
|
"""로그인 실패 횟수 증가"""
|
|
self.count = (self.count or 0) + 1
|
|
self.updated_at = int(get_timestamp_ms())
|
|
|
|
def reset_failed_login(self) -> None:
|
|
"""로그인 실패 횟수 초기화"""
|
|
self.count = 0
|
|
self.updated_at = int(get_timestamp_ms())
|
|
|
|
def update_timestamp(self) -> None:
|
|
"""타임스탬프 업데이트"""
|
|
self.updated_at = int(get_timestamp_ms())
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
"""딕셔너리로 변환"""
|
|
return {
|
|
"user_uuid": self.user_uuid,
|
|
"id": self.id,
|
|
"user_name": self.user_name,
|
|
"auth_level": self.auth_level,
|
|
"count": self.count,
|
|
"created_at": self.created_at,
|
|
"updated_at": self.updated_at
|
|
}
|
|
|
|
def __repr__(self) -> str:
|
|
"""디버깅, 로깅용"""
|
|
return f"<User {self.id}>" |