From 19f337793faa3a1196e68d12f1f4b46d14b8794e Mon Sep 17 00:00:00 2001 From: Ali RajabNezhad Date: Sun, 28 Jul 2024 01:15:47 +0330 Subject: [PATCH] Improvement 4.2.5 (#98) * Fix an issue on nested models in `save()` * Fix an issue on nested models in `update()` --- .github/workflows/code_quality.yaml | 1 + panther/__init__.py | 2 +- panther/db/queries/mongodb_queries.py | 2 ++ panther/db/queries/pantherdb_queries.py | 2 ++ panther/db/queries/queries.py | 8 +++++++- 5 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/code_quality.yaml b/.github/workflows/code_quality.yaml index 0a3af1d..9c9ee0b 100644 --- a/.github/workflows/code_quality.yaml +++ b/.github/workflows/code_quality.yaml @@ -5,6 +5,7 @@ on: push: branches: - 'release-*' + - 'improvement-*' jobs: qodana: diff --git a/panther/__init__.py b/panther/__init__.py index 463ca25..be7e06e 100644 --- a/panther/__init__.py +++ b/panther/__init__.py @@ -1,6 +1,6 @@ from panther.main import Panther # noqa: F401 -__version__ = '4.2.4' +__version__ = '4.2.5' def version(): diff --git a/panther/db/queries/mongodb_queries.py b/panther/db/queries/mongodb_queries.py index 3e468f2..9bfa43b 100644 --- a/panther/db/queries/mongodb_queries.py +++ b/panther/db/queries/mongodb_queries.py @@ -114,6 +114,8 @@ async def update(self, _update: dict | None = None, /, **kwargs) -> None: else: update_query['$set'] = update_query.get('$set', {}) update_query['$set'][field] = value + if isinstance(value, dict): + value = type(getattr(self, field))(**value) setattr(self, field, value) await db.session[self.__class__.__name__].update_one({'_id': self._id}, update_query) diff --git a/panther/db/queries/pantherdb_queries.py b/panther/db/queries/pantherdb_queries.py index caa2eae..a509026 100644 --- a/panther/db/queries/pantherdb_queries.py +++ b/panther/db/queries/pantherdb_queries.py @@ -96,6 +96,8 @@ async def update(self, _update: dict | None = None, /, **kwargs) -> None: self._validate_data(data=kwargs, is_updating=True) for field, value in document.items(): + if isinstance(value, dict): + value = type(getattr(self, field))(**value) setattr(self, field, value) db.session.collection(self.__class__.__name__).update_one({'_id': self._id}, **document) diff --git a/panther/db/queries/queries.py b/panther/db/queries/queries.py index 21bda24..dd51423 100644 --- a/panther/db/queries/queries.py +++ b/panther/db/queries/queries.py @@ -2,6 +2,7 @@ from typing import Sequence, Iterable from pantherdb import Cursor as PantherDBCursor +from pydantic import BaseModel from panther.configs import QueryObservable from panther.db.cursor import Cursor @@ -402,7 +403,12 @@ async def save(self) -> None: >>> user = User(name='Ali') >>> await user.save() """ - document = {field: getattr(self, field) for field in self.model_fields_set if field != 'request'} + document = { + field: getattr(self, field).model_dump() + if issubclass(type(getattr(self, field)), BaseModel) + else getattr(self, field) + for field in self.model_fields_set if field != 'request' + } if self.id: await self.update(document)