Skip to content

Commit

Permalink
Add created and modified times #11
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Sep 23, 2024
1 parent 10fd0d4 commit 4d258b5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
22 changes: 15 additions & 7 deletions object_storage_api/models/attachment.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
"""
Module for defining the database models for representing attachments
Module for defining the database models for representing attachments.
"""

from typing import Optional

from pydantic import BaseModel, ConfigDict, Field

from object_storage_api.models.custom_object_id_data_types import CustomObjectIdField, StringObjectIdField
from object_storage_api.models.mixins import CreatedModifiedTimeInMixin, CreatedModifiedTimeOutMixin


class AttachmentIn(BaseModel):
class AttachmentBase(BaseModel):
"""
Base database model for an attachment.
"""

file_name: str
object_key: str
title: Optional[str] = None
description: Optional[str] = None


class AttachmentIn(CreatedModifiedTimeInMixin, AttachmentBase):
"""
Input database model for an attachment.
"""
Expand All @@ -18,13 +30,9 @@ class AttachmentIn(BaseModel):
# `object_key``) we must manually specify the id rather than relying on MongoDB to do it.
id: CustomObjectIdField = Field(serialization_alias="_id")
entity_id: CustomObjectIdField
file_name: str
object_key: str
title: Optional[str] = None
description: Optional[str] = None


class AttachmentOut(AttachmentIn):
class AttachmentOut(CreatedModifiedTimeOutMixin, AttachmentBase):
"""
Output database model for an attachment.
"""
Expand Down
51 changes: 51 additions & 0 deletions object_storage_api/models/mixins.py
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
4 changes: 3 additions & 1 deletion object_storage_api/schemas/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from pydantic import BaseModel, Field, HttpUrl

from object_storage_api.schemas.mixins import CreatedModifiedSchemaMixin


class AttachmentPostSchema(BaseModel):
"""
Expand All @@ -18,7 +20,7 @@ class AttachmentPostSchema(BaseModel):
description: Optional[str] = Field(default=None, description="Description of the attachment")


class AttachmentPostResponseSchema(AttachmentPostSchema):
class AttachmentPostResponseSchema(CreatedModifiedSchemaMixin, AttachmentPostSchema):
"""
Schema model for the response to an attachment creation request
"""
Expand Down
17 changes: 17 additions & 0 deletions object_storage_api/schemas/mixins.py
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")

0 comments on commit 4d258b5

Please sign in to comment.