|
1 | 1 | import uuid |
2 | 2 | from datetime import datetime |
3 | | -from typing import Optional |
| 3 | +from typing import Optional, List |
4 | 4 |
|
5 | 5 | from pydantic import BaseModel, EmailStr, Field, field_validator |
6 | | -from sqlalchemy import Boolean, Column, DateTime, String, Text |
7 | | -from sqlalchemy.dialects.postgresql import UUID |
8 | | -from sqlalchemy.orm import declarative_base, relationship |
9 | | - |
10 | | -Base = declarative_base() |
| 6 | +from sqlalchemy import Boolean, Column, DateTime, String, Text, func, TypeDecorator |
| 7 | +from sqlalchemy.dialects.postgresql import UUID as PG_UUID |
| 8 | +from sqlalchemy.orm import Mapped, mapped_column, relationship, declarative_base |
| 9 | + |
| 10 | +from models.base import Base |
| 11 | + |
| 12 | + |
| 13 | +class UUID(TypeDecorator): |
| 14 | + """ |
| 15 | + Platform-independent UUID type. |
| 16 | +
|
| 17 | + Uses PostgreSQL's UUID type, otherwise uses |
| 18 | + CHAR(32), storing as string. |
| 19 | + """ |
| 20 | + |
| 21 | + impl = PG_UUID |
| 22 | + cache_ok = True |
| 23 | + |
| 24 | + def load_dialect_impl(self, dialect): |
| 25 | + if dialect.name == "postgresql": |
| 26 | + return dialect.type_descriptor(PG_UUID()) |
| 27 | + else: |
| 28 | + return dialect.type_descriptor(String(32)) |
| 29 | + |
| 30 | + def process_bind_param(self, value, dialect): |
| 31 | + if value is None: |
| 32 | + return value |
| 33 | + elif dialect.name == "postgresql": |
| 34 | + return str(value) |
| 35 | + else: |
| 36 | + if not isinstance(value, uuid.UUID): |
| 37 | + return "%.32x" % uuid.UUID(value).int |
| 38 | + else: |
| 39 | + # hexstring |
| 40 | + return "%.32x" % value.int |
| 41 | + |
| 42 | + def process_result_value(self, value, dialect): |
| 43 | + if value is None: |
| 44 | + return value |
| 45 | + else: |
| 46 | + if not isinstance(value, uuid.UUID): |
| 47 | + value = uuid.UUID(value) |
| 48 | + return value |
11 | 49 |
|
12 | 50 |
|
13 | 51 | class UserTable(Base): |
14 | 52 | """SQLAlchemy User table model for PostgreSQL""" |
15 | 53 |
|
16 | 54 | __tablename__ = "users" |
17 | 55 |
|
18 | | - id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) |
| 56 | + id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4) |
19 | 57 | email = Column(String(255), unique=True, nullable=False, index=True) |
20 | | - name = Column(String(255), nullable=False) |
| 58 | + name: Mapped[str] = mapped_column(String(255), nullable=False) |
21 | 59 | avatar_url = Column(Text, nullable=True) |
22 | 60 | google_id = Column(String(255), unique=True, nullable=True, index=True) |
23 | | - is_active = Column(Boolean, default=True, nullable=False) |
24 | | - is_verified = Column(Boolean, default=False, nullable=False) |
25 | | - created_at = Column(DateTime, default=datetime.utcnow, nullable=False) |
26 | | - updated_at = Column( |
27 | | - DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False |
| 61 | + is_active: Mapped[bool] = mapped_column(Boolean, default=True) |
| 62 | + is_verified: Mapped[bool] = mapped_column(Boolean, default=False) |
| 63 | + |
| 64 | + # Timestamps |
| 65 | + created_at: Mapped[datetime] = mapped_column( |
| 66 | + DateTime(timezone=True), server_default=func.now() |
| 67 | + ) |
| 68 | + updated_at: Mapped[datetime] = mapped_column( |
| 69 | + DateTime(timezone=True), server_default=func.now(), onupdate=func.now() |
28 | 70 | ) |
29 | | - last_sign_in_at = Column(DateTime, nullable=True) |
30 | 71 |
|
31 | 72 | # Relationships |
32 | | - # projects = relationship( |
33 | | - # "ProjectTable", back_populates="user", cascade="all, delete" |
| 73 | + # projects: Mapped[List["ProjectTable"]] = relationship( |
| 74 | + # back_populates="user", cascade="all, delete-orphan" |
34 | 75 | # ) |
35 | | - # chat_messages = relationship( |
36 | | - # "ChatMessageTable", back_populates="user", cascade="all, delete" |
| 76 | + # chat_messages: Mapped[List["ChatMessageTable"]] = relationship( |
| 77 | + # back_populates="user", cascade="all, delete-orphan" |
37 | 78 | # ) |
38 | 79 |
|
39 | 80 | def __repr__(self): |
40 | | - return f"<User(id={self.id}, email={self.email}, name={self.name})>" |
41 | | - |
| 81 | + return f"<User(id={self.id}, email='{self.email}', name='{self.name}')>" |
42 | 82 |
|
43 | | -class UserCreate(BaseModel): |
44 | | - """Pydantic model for creating a user""" |
45 | 83 |
|
46 | | - email: EmailStr = Field(..., description="User email address") |
47 | | - name: str = Field(..., min_length=1, max_length=255, description="User full name") |
48 | | - avatar_url: Optional[str] = Field(None, description="User avatar URL") |
49 | | - google_id: Optional[str] = Field(None, description="Google OAuth ID") |
| 84 | +# Pydantic models for API validation and serialization |
50 | 85 |
|
51 | | - @field_validator("name") |
52 | | - @classmethod |
53 | | - def validate_name(cls, v): |
54 | | - if not v or not v.strip(): |
55 | | - raise ValueError("Name cannot be empty or just whitespace") |
56 | | - return v.strip() |
57 | 86 |
|
58 | | - @field_validator("avatar_url") |
59 | | - @classmethod |
60 | | - def validate_avatar_url(cls, v): |
61 | | - if v and not v.startswith(("http://", "https://")): |
62 | | - raise ValueError("Avatar URL must be a valid HTTP/HTTPS URL") |
63 | | - return v |
| 87 | +class UserBase(BaseModel): |
| 88 | + email: EmailStr |
| 89 | + name: Optional[str] = None |
| 90 | + avatar_url: Optional[str] = None |
| 91 | + is_active: bool = True |
| 92 | + is_verified: bool = False |
64 | 93 |
|
| 94 | + class Config: |
| 95 | + from_attributes = True |
65 | 96 |
|
66 | | -class UserUpdate(BaseModel): |
67 | | - """Pydantic model for updating a user""" |
68 | 97 |
|
69 | | - name: Optional[str] = Field(None, min_length=1, max_length=255) |
70 | | - avatar_url: Optional[str] = Field(None) |
71 | | - is_active: Optional[bool] = Field(None) |
72 | | - is_verified: Optional[bool] = Field(None) |
73 | | - last_sign_in_at: Optional[datetime] = Field(None) |
| 98 | +class UserCreate(UserBase): |
| 99 | + google_id: str |
| 100 | + name: str # Make name required for UserCreate |
74 | 101 |
|
75 | | - @field_validator("name") |
| 102 | + @field_validator("name", "google_id") |
76 | 103 | @classmethod |
77 | | - def validate_name(cls, v): |
78 | | - if v is not None and (not v or not v.strip()): |
79 | | - raise ValueError("Name cannot be empty or just whitespace") |
80 | | - return v.strip() if v else v |
| 104 | + def validate_non_empty(cls, v): |
| 105 | + if not v or not v.strip(): |
| 106 | + raise ValueError("Field cannot be empty") |
| 107 | + return v.strip() |
81 | 108 |
|
82 | | - @field_validator("avatar_url") |
83 | | - @classmethod |
84 | | - def validate_avatar_url(cls, v): |
85 | | - if v and not v.startswith(("http://", "https://")): |
86 | | - raise ValueError("Avatar URL must be a valid HTTP/HTTPS URL") |
87 | | - return v |
88 | 109 |
|
| 110 | +class UserUpdate(BaseModel): |
| 111 | + name: Optional[str] = None |
| 112 | + avatar_url: Optional[str] = None |
89 | 113 |
|
90 | | -class UserInDB(BaseModel): |
91 | | - """Pydantic model for user data from database""" |
92 | 114 |
|
| 115 | +class UserInDB(UserBase): |
93 | 116 | id: uuid.UUID |
94 | | - email: str |
95 | | - name: str |
96 | | - avatar_url: Optional[str] = None |
97 | | - google_id: Optional[str] = None |
98 | | - is_active: bool |
99 | | - is_verified: bool |
100 | 117 | created_at: datetime |
101 | 118 | updated_at: datetime |
102 | | - last_sign_in_at: Optional[datetime] = None |
103 | | - |
104 | | - model_config = {"from_attributes": True} |
105 | | - |
106 | 119 |
|
107 | | -class UserPublic(BaseModel): |
108 | | - """Pydantic model for public user data (API responses)""" |
109 | | - |
110 | | - id: str |
111 | | - email: str |
112 | | - name: str |
113 | | - avatar_url: Optional[str] = None |
114 | | - created_at: str |
115 | | - last_sign_in_at: Optional[str] = None |
116 | | - |
117 | | - @classmethod |
118 | | - def from_db_user(cls, user: UserInDB) -> "UserPublic": |
119 | | - """Convert database user to public user model""" |
120 | | - return cls( |
121 | | - id=str(user.id), |
122 | | - email=user.email, |
123 | | - name=user.name, |
124 | | - avatar_url=user.avatar_url, |
125 | | - created_at=user.created_at.isoformat() + "Z", |
126 | | - last_sign_in_at=( |
127 | | - user.last_sign_in_at.isoformat() + "Z" if user.last_sign_in_at else None |
128 | | - ), |
129 | | - ) |
| 120 | + class Config: |
| 121 | + from_attributes = True |
130 | 122 |
|
131 | 123 |
|
132 | 124 | class GoogleOAuthData(BaseModel): |
133 | | - """Pydantic model for Google OAuth data""" |
134 | | - |
135 | 125 | google_id: str |
136 | 126 | email: EmailStr |
137 | 127 | name: str |
138 | 128 | avatar_url: Optional[str] = None |
139 | | - email_verified: bool = False |
| 129 | + email_verified: bool = True |
140 | 130 |
|
141 | | - @field_validator("google_id") |
| 131 | + @field_validator("name", "google_id", "email") |
142 | 132 | @classmethod |
143 | | - def validate_google_id(cls, v): |
| 133 | + def strip_whitespace(cls, v): |
144 | 134 | if not v or not v.strip(): |
145 | | - raise ValueError("Google ID cannot be empty") |
| 135 | + raise ValueError("Field cannot be empty") |
146 | 136 | return v.strip() |
| 137 | + |
| 138 | + class Config: |
| 139 | + from_attributes = True |
0 commit comments