File size: 2,294 Bytes
08af9fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from datetime import datetime
from typing import Optional, List, TYPE_CHECKING
from sqlmodel import Field, SQLModel, Relationship

if TYPE_CHECKING:
    from .task import Task
    from .chatbot import Conversation
from pydantic import validator
import re


class UserBase(SQLModel):
    email: str = Field(unique=True, nullable=False, max_length=255)

    @validator('email')
    def validate_email(cls, v):
        if not re.match(r'^[^@]+@[^@]+\.[^@]+$', v):
            raise ValueError('Invalid email address')
        return v.lower().strip()


class User(UserBase, table=True):
    __tablename__ = "auth_user"
    id: str = Field(primary_key=True)
    name: Optional[str] = None
    email: str = Field(unique=True, nullable=False, max_length=255)
    emailVerified: bool = Field(default=False)
    image: Optional[str] = None
    createdAt: datetime = Field(default_factory=datetime.utcnow)
    updatedAt: datetime = Field(default_factory=datetime.utcnow)

    # Relationship to tasks
    tasks: List["Task"] = Relationship(back_populates="user")
    conversations: List["Conversation"] = Relationship(back_populates="user")


class Account(SQLModel, table=True):
    __tablename__ = "auth_account"
    id: str = Field(primary_key=True)
    userId: str = Field(foreign_key="auth_user.id", nullable=False)
    accountId: str = Field(nullable=False)
    providerId: str = Field(nullable=False)
    accessToken: Optional[str] = None
    refreshToken: Optional[str] = None
    password: Optional[str] = None
    createdAt: datetime = Field(default_factory=datetime.utcnow)
    updatedAt: datetime = Field(default_factory=datetime.utcnow)


class Verification(SQLModel, table=True):
    __tablename__ = "auth_verification"
    id: str = Field(primary_key=True)
    identifier: str = Field(nullable=False)
    value: str = Field(nullable=False)
    expiresAt: datetime = Field(nullable=False)
    createdAt: datetime = Field(default_factory=datetime.utcnow)
    updatedAt: datetime = Field(default_factory=datetime.utcnow)


class UserCreate(UserBase):
    """
    Schema for creating a new user.
    """
    email: str
    password: str


class UserRead(UserBase):
    """
    Schema for reading user data (without password).
    """
    id: str
    createdAt: datetime
    updatedAt: datetime