Files
emailsystem/app/models/domain.py
2025-02-25 19:50:00 +08:00

35 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from sqlalchemy import Column, Integer, String, Boolean, DateTime
from sqlalchemy.orm import relationship
from datetime import datetime
from . import Base
class Domain(Base):
"""邮件域名模型"""
__tablename__ = 'domains'
id = Column(Integer, primary_key=True)
name = Column(String(255), unique=True, nullable=False, index=True)
description = Column(String(500), nullable=True)
active = Column(Boolean, default=True)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# 关系
mailboxes = relationship("Mailbox", back_populates="domain", cascade="all, delete-orphan")
def __repr__(self):
return f"<Domain {self.name}>"
def to_dict(self):
"""转换为字典用于API响应"""
return {
"id": self.id,
"name": self.name,
"description": self.description,
"active": self.active,
"created_at": self.created_at.isoformat() if self.created_at else None,
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
"mailbox_count": len(self.mailboxes) if self.mailboxes else 0
}