-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPydanticTaskModels.py
141 lines (111 loc) · 3.72 KB
/
PydanticTaskModels.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from typing import Dict, List, Any, Optional, Union, Tuple
from typing_extensions import Annotated
from pydantic import BaseModel, Field
from datetime import datetime, timedelta
from enum import Enum
import json
def custom_json_serializer(obj):
if isinstance(obj, datetime):
return obj.isoformat()
elif isinstance(obj, timedelta):
return str(obj)
elif isinstance(obj, Enum):
return obj.value
elif isinstance(obj, tuple):
return list(obj)
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
class CustomJSONEncoder(json.JSONEncoder):
def default(self, obj):
try:
return custom_json_serializer(obj)
except TypeError:
return super().default(obj)
class BaseModelWithCustomJSON(BaseModel):
class Config:
json_encoders = {
datetime: lambda v: v.isoformat(),
timedelta: str,
Enum: lambda v: v.value,
tuple: list,
}
def json(self, **kwargs):
return json.dumps(self.dict(), cls=CustomJSONEncoder, **kwargs)
# Pydantic models
class TodoCreate(BaseModelWithCustomJSON):
content: str = Field(..., min_length=1, max_length=1000)
class TodoOut(BaseModelWithCustomJSON):
id: str
content: str
createdAt: datetime
updatedAt: datetime
# Simplified Pydantic models for OKR
class OKRCreate(BaseModelWithCustomJSON):
title: str = Field(..., min_length=1, max_length=200)
description: str = Field(..., min_length=1, max_length=1000)
class OKROut(BaseModelWithCustomJSON):
id: str
title: str
description: str
createdAt: datetime
updatedAt: datetime
class OKROutList(BaseModelWithCustomJSON):
okrs: List[OKROut]
class TaskCreate(BaseModelWithCustomJSON):
name: str
description: Optional[str] = None
estimated_time_mins: Optional[int] = None
priority: Optional[int] = None
tags: Optional[List[str]] = None
scheduled_date_utc: Optional[int] = None # Add this line
class TaskOut(BaseModelWithCustomJSON):
id: str
name: str
description: Optional[str] = None
estimated_time_mins: Optional[int] = None
priority: Optional[int] = None
tags: Optional[List[str]] = None
scheduled_date_utc: Optional[int] = None # Add this line
createdAt: datetime
updatedAt: datetime
class TaskList(BaseModelWithCustomJSON):
tasks: List[TaskOut]
class NullModel(BaseModelWithCustomJSON):
value: None = None
class DirectoryInput(BaseModelWithCustomJSON):
directory: str
class FileList(BaseModelWithCustomJSON):
files: List[str]
class CurrentDateTime(BaseModelWithCustomJSON):
current_datetime: datetime
class InputDatetimePlaintext(BaseModelWithCustomJSON):
input_dt: str
class DatetimeMillis(BaseModelWithCustomJSON):
datetime_millis: str
class DatetimeSeconds(BaseModelWithCustomJSON):
datetime_seconds: str
class DeleteTaskInput(BaseModelWithCustomJSON):
id: str
class TaskId(BaseModelWithCustomJSON):
id: str
class UpdateTaskInput(BaseModelWithCustomJSON):
id: str
name: Optional[str] = None
description: Optional[str] = None
estimated_time_mins: Optional[int] = None
priority: Optional[int] = None
tags: Optional[List[str]] = None
scheduled_date_utc: Optional[int] = None
class UTCSecondsList(BaseModelWithCustomJSON):
utc_seconds: List[int]
class HumanReadableDateList(BaseModelWithCustomJSON):
dates: List[str]
class HNStory(BaseModelWithCustomJSON):
title: str
url: str
class HNSearchResult(BaseModelWithCustomJSON):
hits: List[HNStory]
class HNBlob(BaseModelWithCustomJSON):
search_result: HNSearchResult
class DadJoke(BaseModelWithCustomJSON):
joke_id: str
joke_contents: str