编写后端

This commit is contained in:
2026-03-28 22:18:43 +08:00
parent f2528fbc87
commit f5d26949c4
63 changed files with 1841 additions and 5 deletions

View File

@@ -0,0 +1 @@
# Schemas module

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,29 @@
"""
认证 Schema
"""
from pydantic import BaseModel, EmailStr, Field
class LoginRequest(BaseModel):
"""登录请求 Schema"""
username: str = Field(..., description="用户名或邮箱")
password: str = Field(..., description="密码")
class RegisterRequest(BaseModel):
"""注册请求 Schema"""
username: str = Field(..., min_length=3, max_length=50, description="用户名")
email: EmailStr = Field(..., description="邮箱")
password: str = Field(..., min_length=6, max_length=100, description="密码")
class TokenResponse(BaseModel):
"""令牌响应 Schema"""
access_token: str = Field(..., description="访问令牌")
refresh_token: str = Field(..., description="刷新令牌")
token_type: str = Field(default="bearer", description="令牌类型")
class RefreshTokenRequest(BaseModel):
"""刷新令牌请求 Schema"""
refresh_token: str = Field(..., description="刷新令牌")

View File

@@ -0,0 +1,55 @@
"""
评论 Schema
"""
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field
class CommentBase(BaseModel):
"""评论基础 Schema"""
content: str = Field(..., min_length=1, description="评论内容")
class CommentCreate(CommentBase):
"""评论创建 Schema"""
post_id: str = Field(..., description="文章ID")
parent_id: Optional[str] = Field(None, description="父评论ID")
class CommentUpdate(BaseModel):
"""评论更新 Schema"""
content: Optional[str] = Field(None, min_length=1)
class CommentAuthor(BaseModel):
"""评论作者 Schema"""
id: str
username: str
avatar: Optional[str] = None
class Config:
from_attributes = True
class CommentResponse(CommentBase):
"""评论响应 Schema"""
id: str
author: CommentAuthor
post_id: str
parent_id: Optional[str] = None
is_approved: bool
created_at: datetime
updated_at: datetime
replies: List["CommentResponse"] = []
class Config:
from_attributes = True
class CommentListResponse(BaseModel):
"""评论列表响应 Schema"""
items: List[CommentResponse]
total: int
page: int
page_size: int

115
backend/app/schemas/post.py Normal file
View File

@@ -0,0 +1,115 @@
"""
文章 Schema
"""
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field
class TagBase(BaseModel):
"""标签基础 Schema"""
name: str = Field(..., min_length=1, max_length=50, description="标签名")
slug: str = Field(..., min_length=1, max_length=50, description="URL别名")
class TagCreate(TagBase):
"""标签创建 Schema"""
pass
class TagResponse(TagBase):
"""标签响应 Schema"""
id: str
created_at: datetime
class Config:
from_attributes = True
class CategoryBase(BaseModel):
"""分类基础 Schema"""
name: str = Field(..., min_length=1, max_length=50, description="分类名")
slug: str = Field(..., min_length=1, max_length=50, description="URL别名")
description: Optional[str] = None
class CategoryCreate(CategoryBase):
"""分类创建 Schema"""
pass
class CategoryResponse(CategoryBase):
"""分类响应 Schema"""
id: str
created_at: datetime
class Config:
from_attributes = True
class PostBase(BaseModel):
"""文章基础 Schema"""
title: str = Field(..., min_length=1, max_length=200, description="标题")
slug: str = Field(..., min_length=1, max_length=200, description="URL别名")
content: str = Field(..., description="文章内容")
summary: Optional[str] = None
cover_image: Optional[str] = None
category_id: Optional[str] = None
status: str = Field(default="draft", description="状态: draft/published/archived")
class PostCreate(PostBase):
"""文章创建 Schema"""
tag_ids: Optional[List[str]] = []
meta_title: Optional[str] = None
meta_description: Optional[str] = None
class PostUpdate(BaseModel):
"""文章更新 Schema"""
title: Optional[str] = Field(None, min_length=1, max_length=200)
slug: Optional[str] = Field(None, min_length=1, max_length=200)
content: Optional[str] = None
summary: Optional[str] = None
cover_image: Optional[str] = None
category_id: Optional[str] = None
tag_ids: Optional[List[str]] = None
status: Optional[str] = None
meta_title: Optional[str] = None
meta_description: Optional[str] = None
class AuthorResponse(BaseModel):
"""作者响应 Schema"""
id: str
username: str
avatar: Optional[str] = None
class Config:
from_attributes = True
class PostResponse(PostBase):
"""文章响应 Schema"""
id: str
author: AuthorResponse
category: Optional[CategoryResponse] = None
tags: List[TagResponse] = []
view_count: int
like_count: int
comment_count: int
published_at: Optional[datetime] = None
created_at: datetime
updated_at: datetime
class Config:
from_attributes = True
class PostListResponse(BaseModel):
"""文章列表响应 Schema"""
items: List[PostResponse]
total: int
page: int
page_size: int
total_pages: int

View File

@@ -0,0 +1,50 @@
"""
用户 Schema
"""
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, EmailStr, Field
class UserBase(BaseModel):
"""用户基础 Schema"""
username: str = Field(..., min_length=3, max_length=50, description="用户名")
email: EmailStr = Field(..., description="邮箱")
class UserCreate(UserBase):
"""用户创建 Schema"""
password: str = Field(..., min_length=6, max_length=100, description="密码")
class UserUpdate(BaseModel):
"""用户更新 Schema"""
username: Optional[str] = Field(None, min_length=3, max_length=50)
email: Optional[EmailStr] = None
avatar: Optional[str] = None
bio: Optional[str] = None
class UserInDB(UserBase):
"""用户数据库 Schema"""
id: str
avatar: Optional[str] = None
bio: Optional[str] = None
is_active: bool
is_superuser: bool
created_at: datetime
updated_at: datetime
class Config:
from_attributes = True
class UserPublic(UserBase):
"""用户公开信息 Schema"""
id: str
avatar: Optional[str] = None
bio: Optional[str] = None
created_at: datetime
class Config:
from_attributes = True