Skip to content

Commit a6e69a0

Browse files
committed
Remove custom field type #74
1 parent b7e7b1d commit a6e69a0

File tree

3 files changed

+16
-38
lines changed

3 files changed

+16
-38
lines changed

inventory_management_system_api/models/custom_object_id_data_types.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
Module for defining custom `ObjectId` data type classes used by Pydantic models.
33
"""
44

5-
from dataclasses import dataclass
6-
from typing import Any, Optional
5+
from typing import Any
76

87
from bson import ObjectId
98
from pydantic import GetCoreSchemaHandler
@@ -33,29 +32,6 @@ def validate(cls, value: str, _: core_schema.ValidationInfo) -> CustomObjectId:
3332
return CustomObjectId(value)
3433

3534

36-
@dataclass(frozen=True)
37-
class CustomObjectIdFieldType:
38-
"""
39-
Custom data type for handling MongoDB ObjectId validation.
40-
"""
41-
42-
entity_type: Optional[str] = None
43-
not_found_if_invalid: bool = False
44-
45-
def __get_pydantic_core_schema__(self, _source_type: Any, _handler: GetCoreSchemaHandler) -> CoreSchema:
46-
return core_schema.with_info_plain_validator_function(self.validate)
47-
48-
def validate(self, value: str, _: core_schema.ValidationInfo) -> CustomObjectId:
49-
"""
50-
Validate if the string value is a valid `ObjectId`.
51-
52-
:param value: The string value to be validated.
53-
:param _: Unused
54-
:return: The validated `ObjectId`.
55-
"""
56-
return CustomObjectId(value, entity_type=self.entity_type, not_found_if_invalid=self.not_found_if_invalid)
57-
58-
5935
class StringObjectIdField(str):
6036
"""
6137
Custom data type for handling MongoDB ObjectId as string.

inventory_management_system_api/models/system.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,20 @@
22
Module for defining the database models for representing systems.
33
"""
44

5-
from typing import Annotated, Optional
5+
from typing import Optional
66

77
from pydantic import BaseModel, ConfigDict, Field
88

9-
from inventory_management_system_api.core.custom_object_id import CustomObjectId
10-
from inventory_management_system_api.models.custom_object_id_data_types import (
11-
CustomObjectIdFieldType,
12-
StringObjectIdField,
13-
)
9+
from inventory_management_system_api.models.custom_object_id_data_types import CustomObjectIdField, StringObjectIdField
1410
from inventory_management_system_api.models.mixins import CreatedModifiedTimeInMixin, CreatedModifiedTimeOutMixin
1511

16-
ParentSystemIdType = Annotated[
17-
CustomObjectId, CustomObjectIdFieldType(entity_type="parent system", not_found_if_invalid=False)
18-
]
19-
2012

2113
class SystemBase(BaseModel):
2214
"""
2315
Base database model for a system
2416
"""
2517

26-
parent_id: Optional[ParentSystemIdType] = None
18+
parent_id: Optional[CustomObjectIdField] = None
2719
name: str
2820
description: Optional[str] = None
2921
location: Optional[str] = None

inventory_management_system_api/services/system.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from fastapi import Depends
99

1010
from inventory_management_system_api.core.config import config
11+
from inventory_management_system_api.core.custom_object_id import CustomObjectId
1112
from inventory_management_system_api.core.exceptions import ChildElementsExistError
1213
from inventory_management_system_api.core.object_storage_api_client import ObjectStorageAPIClient
1314
from inventory_management_system_api.models.system import SystemIn, SystemOut
@@ -39,12 +40,16 @@ def create(self, system: SystemPostSchema) -> SystemOut:
3940
:param system: System to be created
4041
:return: Created system
4142
"""
42-
parent_id = system.parent_id
43+
44+
# Check here so can raise appropriate error (alternative methods would be to have a custom type in
45+
# SystemIn or move the parent_id check to the service)
46+
if system.parent_id is not None:
47+
CustomObjectId(system.parent_id, entity_type="parent system")
4348

4449
code = utils.generate_code(system.name, "system")
4550
return self._system_repository.create(
4651
SystemIn(
47-
parent_id=parent_id,
52+
parent_id=system.parent_id,
4853
description=system.description,
4954
name=system.name,
5055
location=system.location,
@@ -94,6 +99,11 @@ def update(self, system_id: str, system: SystemPatchSchema) -> SystemOut:
9499

95100
update_data = system.model_dump(exclude_unset=True)
96101

102+
# Check here so can raise appropriate error (alternative methods would be to have a custom type in
103+
# SystemIn or move the parent_id check to the service)
104+
if "parent_id" in update_data and system.parent_id != stored_system.parent_id and system.parent_id is not None:
105+
CustomObjectId(system.parent_id, entity_type="parent system")
106+
97107
if "name" in update_data and system.name != stored_system.name:
98108
update_data["code"] = utils.generate_code(system.name, "system")
99109

0 commit comments

Comments
 (0)