-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10fd0d4
commit 4d258b5
Showing
4 changed files
with
86 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Module for defining the database models mixins to be inherited from to provide specific fields | ||
and functionality. | ||
""" | ||
|
||
# pylint:disable=fixme | ||
# TODO: This file is identical to the one in inventory-management-system-api - Use common repo? | ||
|
||
from datetime import datetime, timezone | ||
from typing import Optional | ||
|
||
from pydantic import AwareDatetime, BaseModel, Field, model_validator | ||
|
||
|
||
class CreatedModifiedTimeInMixin(BaseModel): | ||
""" | ||
Input model mixin that provides creation and modified time fields | ||
For a create request an instance of the model should be created without supplying the `created_time` field | ||
as this will cause it to be assigned as the current time. | ||
When updating, the `created_time` time should be given so that it is kept the same. The `modified_time` will be | ||
assigned regardless as it is assumed that a new instance will be created only when creating/updating a | ||
database entry. | ||
""" | ||
|
||
created_time: AwareDatetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
modified_time: Optional[AwareDatetime] = None | ||
|
||
@model_validator(mode="after") | ||
def validator(self) -> "CreatedModifiedTimeInMixin": | ||
""" | ||
Validator that assigns the created_time and modified_time times. | ||
When `modified_time` is None, which occurs when not assigning data from an existing database model this | ||
assigns the `modified_time` time to be the same as the `created_time` to ensure they are identical. When | ||
`modified_time` is defined then it is reassigned as its assumed it already exists and is now being updated. | ||
""" | ||
if self.modified_time is None: | ||
self.modified_time = self.created_time | ||
else: | ||
self.modified_time = datetime.now(timezone.utc) | ||
return self | ||
|
||
|
||
class CreatedModifiedTimeOutMixin(BaseModel): | ||
""" | ||
Output model mixin that provides creation and modified time fields | ||
""" | ||
|
||
created_time: AwareDatetime | ||
modified_time: AwareDatetime |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Module for defining the schema mixins to be inherited from to provide specific fields | ||
""" | ||
|
||
from pydantic import AwareDatetime, BaseModel, Field | ||
|
||
# pylint:disable=fixme | ||
# TODO: This file is identical to the one in inventory-management-system-api - Use common repo? | ||
|
||
|
||
class CreatedModifiedSchemaMixin(BaseModel): | ||
""" | ||
Output schema mixin that provides creation and modified time fields | ||
""" | ||
|
||
created_time: AwareDatetime = Field(description="The date and time this entity was created") | ||
modified_time: AwareDatetime = Field(description="The date and time this entity was last updated") |