Initial commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import secrets
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
import bcrypt
|
||||
from sqlalchemy import DateTime, Integer, String
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class Application(Base):
|
||||
__tablename__ = "applications"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(255))
|
||||
token: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
||||
telegram_bot_token: Mapped[str] = mapped_column(String(255))
|
||||
telegram_chat_id: Mapped[str] = mapped_column(String(64))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
is_active: Mapped[bool] = mapped_column(default=True)
|
||||
|
||||
@staticmethod
|
||||
def generate_token() -> str:
|
||||
return secrets.token_hex(32)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"token": self.token,
|
||||
"telegram_bot_token": self.telegram_bot_token,
|
||||
"telegram_chat_id": self.telegram_chat_id,
|
||||
"created_at": self.created_at.isoformat() if self.created_at else None,
|
||||
"is_active": self.is_active,
|
||||
}
|
||||
|
||||
|
||||
class LogSettings(Base):
|
||||
__tablename__ = "log_settings"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
max_bytes: Mapped[int] = mapped_column(Integer, default=10485760)
|
||||
backup_count: Mapped[int] = mapped_column(Integer, default=5)
|
||||
log_level: Mapped[str] = mapped_column(String(20), default="INFO")
|
||||
|
||||
|
||||
class AdminUser(Base):
|
||||
__tablename__ = "admin_users"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
username: Mapped[str] = mapped_column(String(100), unique=True, index=True)
|
||||
password_hash: Mapped[str] = mapped_column(String(255))
|
||||
email: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
last_login: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
|
||||
@staticmethod
|
||||
def hash_password(password: str) -> str:
|
||||
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
||||
|
||||
def check_password(self, password: str) -> bool:
|
||||
return bcrypt.checkpw(password.encode(), self.password_hash.encode())
|
||||
|
||||
@staticmethod
|
||||
def create_default(db) -> "AdminUser":
|
||||
default_password = "admin123"
|
||||
user = AdminUser(
|
||||
username="admin",
|
||||
password_hash=AdminUser.hash_password(default_password),
|
||||
)
|
||||
db.add(user)
|
||||
db.commit()
|
||||
return user
|
||||
Reference in New Issue
Block a user