- FastAPI backend with SQLite database - JWT authentication (register, login) - User profiles with follow/unfollow - Posts with image upload and likes/dislikes - Comments with likes - Global and personalized feed - Comprehensive pytest test suite (37 tests) TASK-ID: 758f4029-702
110 lines
2.3 KiB
Python
110 lines
2.3 KiB
Python
"""Pydantic models for SocialPhoto API."""
|
|
from datetime import datetime
|
|
from typing import Optional, List
|
|
|
|
from pydantic import BaseModel, ConfigDict, EmailStr, Field
|
|
|
|
|
|
# Auth Models
|
|
class UserRegister(BaseModel):
|
|
"""Request model for user registration."""
|
|
username: str = Field(..., min_length=3, max_length=50)
|
|
email: EmailStr
|
|
password: str = Field(..., min_length=6)
|
|
|
|
|
|
class UserLogin(BaseModel):
|
|
"""Request model for user login."""
|
|
username: str
|
|
password: str
|
|
|
|
|
|
class Token(BaseModel):
|
|
"""Response model for JWT token."""
|
|
access_token: str
|
|
token_type: str = "bearer"
|
|
|
|
|
|
# User Models
|
|
class UserBase(BaseModel):
|
|
"""Base user model."""
|
|
username: str
|
|
email: str
|
|
avatar_url: Optional[str] = "/static/default-avatar.png"
|
|
bio: Optional[str] = ""
|
|
|
|
|
|
class UserResponse(UserBase):
|
|
"""Response model for user data."""
|
|
id: int
|
|
created_at: datetime
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
class UserStats(BaseModel):
|
|
"""User statistics model."""
|
|
posts_count: int
|
|
followers_count: int
|
|
following_count: int
|
|
|
|
|
|
# Post Models
|
|
class PostCreate(BaseModel):
|
|
"""Request model for creating a post."""
|
|
caption: Optional[str] = ""
|
|
|
|
|
|
class PostResponse(BaseModel):
|
|
"""Response model for post data."""
|
|
id: int
|
|
user_id: int
|
|
username: str
|
|
image_url: str
|
|
caption: str
|
|
likes_count: int
|
|
dislikes_count: int
|
|
comments_count: int
|
|
created_at: datetime
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
class PostDetail(PostResponse):
|
|
"""Detailed post response with user info."""
|
|
user: UserResponse
|
|
|
|
|
|
# Comment Models
|
|
class CommentCreate(BaseModel):
|
|
"""Request model for creating a comment."""
|
|
content: str = Field(..., min_length=1, max_length=500)
|
|
|
|
|
|
class CommentResponse(BaseModel):
|
|
"""Response model for comment data."""
|
|
id: int
|
|
post_id: int
|
|
user_id: int
|
|
username: str
|
|
content: str
|
|
likes_count: int
|
|
created_at: datetime
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
|
# Feed Models
|
|
class FeedResponse(BaseModel):
|
|
"""Response model for feed."""
|
|
posts: List[PostResponse]
|
|
total: int
|
|
limit: int
|
|
offset: int
|
|
|
|
|
|
# Error Models
|
|
class ErrorResponse(BaseModel):
|
|
"""Standard error response."""
|
|
detail: str
|