-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Update Pydantic to v2 and make associated syntax changes to several models - Add validation, computed fields, and hybrid properties to support task creation - Add services and service methods to validate tasks - Add/update schemas for tasks
- Loading branch information
Showing
11 changed files
with
269 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
from pydantic import BaseModel | ||
from pydantic import ConfigDict, BaseModel | ||
from typing import Optional | ||
|
||
|
||
class Customer(BaseModel): | ||
id: int | ||
name: str | ||
customer_type: str | ||
url: Optional[str] | ||
url: Optional[str] = None | ||
sector_id: int | ||
|
||
class Config: | ||
orm_mode = True | ||
model_config = ConfigDict(from_attributes=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
from datetime import date | ||
|
||
from pydantic import BaseModel | ||
from pydantic import ConfigDict, BaseModel | ||
from typing import Optional | ||
|
||
|
||
class Project(BaseModel): | ||
id: str | ||
is_active: bool | ||
init: Optional[date] | ||
end: Optional[date] | ||
invoice: Optional[float] | ||
estimated_hours: Optional[float] | ||
moved_hours: Optional[float] | ||
description: Optional[str] | ||
project_type: Optional[str] | ||
schedule_type: Optional[str] | ||
init: Optional[date] = None | ||
end: Optional[date] = None | ||
invoice: Optional[float] = None | ||
estimated_hours: Optional[float] = None | ||
moved_hours: Optional[float] = None | ||
description: Optional[str] = None | ||
project_type: Optional[str] = None | ||
schedule_type: Optional[str] = None | ||
customer_id: int | ||
area_id: int | ||
|
||
class Config: | ||
orm_mode = True | ||
model_config = ConfigDict(from_attributes=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,129 @@ | ||
from datetime import date | ||
from pydantic import BaseModel, root_validator, constr | ||
from typing import Optional | ||
from pydantic import StringConstraints, ConfigDict, BaseModel, computed_field, model_validator | ||
from typing import Optional, Any | ||
from typing_extensions import Annotated | ||
|
||
|
||
class TaskTypeItem(BaseModel): | ||
slug: str | ||
name: str | ||
active: bool | ||
|
||
class Config: | ||
orm_mode = True | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
|
||
# Shared properties | ||
class TemplateBase(BaseModel): | ||
name: Optional[constr(max_length=80)] | ||
story: Optional[constr(max_length=80)] | ||
description: Optional[constr(max_length=8192)] | ||
task_type: Optional[constr(max_length=40)] | ||
init_time: Optional[int] | ||
end_time: Optional[int] | ||
name: Optional[Annotated[str, StringConstraints(max_length=80)]] | ||
story: Optional[Annotated[str, StringConstraints(max_length=80)]] | ||
description: Optional[Annotated[str, StringConstraints(max_length=8192)]] | ||
task_type: Optional[Annotated[str, StringConstraints(max_length=40)]] | ||
customer_id: Optional[int] | ||
user_id: Optional[int] | ||
project_id: Optional[int] | ||
is_global: Optional[bool] | ||
|
||
@root_validator(pre=True) | ||
def user_template_not_global(cls, values): | ||
user_id, is_global = values.get("user_id"), values.get("is_global") | ||
if is_global and user_id is not None: | ||
raise ValueError("Global templates should not have a user_id.") | ||
if not is_global and not user_id: | ||
raise ValueError("Private templates should have a user_id.") | ||
return values | ||
start_time: Optional[str] | ||
end_time: Optional[str] | ||
|
||
|
||
# Properties to receive on creation | ||
class TemplateNew(TemplateBase): | ||
name: constr(max_length=80) | ||
name: Annotated[str, StringConstraints(max_length=80)] | ||
is_global: bool = False | ||
|
||
@computed_field | ||
@property | ||
def init(self) -> Optional[int]: | ||
if not self.start_time: | ||
return None | ||
|
||
hours, minutes = self.start_time.split(":") | ||
return int(hours) * 60 + int(minutes) | ||
|
||
@computed_field | ||
@property | ||
def end(self) -> Optional[int]: | ||
if not self.end_time: | ||
return None | ||
|
||
hours, minutes = self.end_time.split(":") | ||
return int(hours) * 60 + int(minutes) | ||
|
||
|
||
# Properties shared by models stored in db | ||
class TemplateInDb(TemplateBase): | ||
id: int | ||
|
||
class Config: | ||
orm_mode = True | ||
init: Optional[int] | ||
end: Optional[int] | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
|
||
# Properties to return to client | ||
class Template(TemplateInDb): | ||
pass | ||
|
||
|
||
class Task(BaseModel): | ||
id: int | ||
# Shared properties | ||
class TaskBase(BaseModel): | ||
date: Optional[date] | ||
story: Optional[Annotated[str, StringConstraints(max_length=80)]] = None | ||
description: Optional[Annotated[str, StringConstraints(max_length=8192)]] = None | ||
task_type: Optional[Annotated[str, StringConstraints(max_length=40)]] = None | ||
project_id: Optional[int] = None | ||
user_id: Optional[int] = None | ||
start_time: Optional[str] = None | ||
end_time: Optional[str] = None | ||
|
||
@model_validator(mode="after") | ||
@classmethod | ||
def end_after_init_task(cls, data: Any) -> Any: | ||
end_time = data.end | ||
init_time = data.init | ||
if end_time and init_time: | ||
if end_time < init_time: | ||
raise ValueError("End time must be greater than or equal to start time.") | ||
if init_time < 0: | ||
raise ValueError("Start time must be greater than or equal to 0.") | ||
if end_time > 1439: | ||
raise ValueError("End time must be less than or equal to 1439 (23:59).") | ||
return data | ||
return data | ||
|
||
|
||
# Properties to receive on creation | ||
class TaskNew(TaskBase): | ||
date: date | ||
project_id: int | ||
user_id: int | ||
start_time: Annotated[str, StringConstraints(pattern=r"^[0-9]{2}:[0-9]{2}")] | ||
end_time: Annotated[str, StringConstraints(pattern=r"^[0-9]{2}:[0-9]{2}")] | ||
|
||
@computed_field | ||
@property | ||
def init(self) -> int: | ||
hours, minutes = self.start_time.split(":") | ||
return int(hours) * 60 + int(minutes) | ||
|
||
@computed_field | ||
@property | ||
def end(self) -> int: | ||
hours, minutes = self.end_time.split(":") | ||
return int(hours) * 60 + int(minutes) | ||
|
||
|
||
# Properties shared by models stored in db | ||
class TaskInDb(TaskBase): | ||
id: int | ||
init: int | ||
end: int | ||
story: Optional[str] | ||
description: Optional[str] | ||
task_type: Optional[str] | ||
project_id: int | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
|
||
# Properties to return to client | ||
class Task(TaskInDb): | ||
project_name: str | ||
customer_name: Optional[str] | ||
customer_name: Optional[str] = None | ||
|
||
|
||
class Config: | ||
orm_mode = True | ||
class TaskValidate(BaseModel): | ||
is_task_valid: bool | ||
message: Optional[str] = None |
Oops, something went wrong.