40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from typing import Optional
|
|
from sqlalchemy import Column, Integer, String, Float, DateTime
|
|
from sqlalchemy.sql import func
|
|
from pydantic import BaseModel, Field
|
|
from .database import Base
|
|
|
|
# 数据库模型
|
|
class CommunityDB(Base):
|
|
__tablename__ = "communities"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
name = Column(String(100), nullable=False)
|
|
address = Column(String(200), nullable=False)
|
|
longitude = Column(Float, nullable=False) # 经度
|
|
latitude = Column(Float, nullable=False) # 纬度
|
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
|
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
# Pydantic 模型
|
|
class CommunityCreate(BaseModel):
|
|
name: str = Field(..., max_length=100)
|
|
address: str = Field(..., max_length=200)
|
|
longitude: float = Field(..., ge=-180, le=180)
|
|
latitude: float = Field(..., ge=-90, le=90)
|
|
|
|
class CommunityUpdate(BaseModel):
|
|
name: Optional[str] = Field(None, max_length=100)
|
|
address: Optional[str] = Field(None, max_length=200)
|
|
longitude: Optional[float] = Field(None, ge=-180, le=180)
|
|
latitude: Optional[float] = Field(None, ge=-90, le=90)
|
|
|
|
class CommunityInfo(BaseModel):
|
|
id: int
|
|
name: str
|
|
address: str
|
|
longitude: float
|
|
latitude: float
|
|
|
|
class Config:
|
|
from_attributes = True |