feat: migrate persistence from JSON to SQLModel (Phase 1)
- Integrated SQLModel with SQLite for robust data persistence - Refactored UserManager and WatchlistManager to use SQL queries - Migrated models to SQLModel with relationships and primary keys - Updated test suite with in-memory database isolation - Removed deprecated JSON storage files
This commit is contained in:
+36
-14
@@ -1,15 +1,41 @@
|
||||
"""Authentication models for user management"""
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
from typing import Optional
|
||||
"""Authentication models for user management with SQLModel support"""
|
||||
import uuid
|
||||
from pydantic import BaseModel, EmailStr, Field as PydanticField
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from sqlmodel import SQLModel, Field, Relationship
|
||||
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
"""Schema for user registration"""
|
||||
username: str = Field(..., min_length=3, max_length=50)
|
||||
email: Optional[EmailStr] = None
|
||||
password: str = Field(..., min_length=6)
|
||||
class UserBase(SQLModel):
|
||||
"""Base schema for user data"""
|
||||
username: str = Field(index=True, unique=True, min_length=3, max_length=50)
|
||||
email: Optional[str] = Field(default=None, index=True)
|
||||
full_name: Optional[str] = None
|
||||
is_active: bool = Field(default=True)
|
||||
|
||||
|
||||
class UserTable(UserBase, table=True):
|
||||
"""Database table for users"""
|
||||
__tablename__ = "users"
|
||||
|
||||
id: str = Field(
|
||||
default_factory=lambda: str(uuid.uuid4()),
|
||||
primary_key=True,
|
||||
index=True,
|
||||
nullable=False
|
||||
)
|
||||
hashed_password: str
|
||||
created_at: datetime = Field(default_factory=datetime.now)
|
||||
last_login: Optional[datetime] = None
|
||||
|
||||
# Relationships
|
||||
watchlist_items: List["WatchlistItemTable"] = Relationship(back_populates="user")
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
"""Schema for user registration"""
|
||||
password: str = PydanticField(..., min_length=6)
|
||||
email: Optional[EmailStr] = None
|
||||
|
||||
|
||||
class UserLogin(BaseModel):
|
||||
@@ -18,13 +44,9 @@ class UserLogin(BaseModel):
|
||||
password: str
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
"""Schema for user data"""
|
||||
class User(UserBase):
|
||||
"""Schema for user data (API Response)"""
|
||||
id: str
|
||||
username: str
|
||||
email: Optional[str] = None
|
||||
full_name: Optional[str] = None
|
||||
is_active: bool = True
|
||||
created_at: datetime
|
||||
last_login: Optional[datetime] = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user