Skip to content

Commit

Permalink
Improvement 4.2.5 (#98)
Browse files Browse the repository at this point in the history
* Fix an issue on nested models in `save()`

* Fix an issue on nested models in `update()`
  • Loading branch information
AliRn76 authored Jul 27, 2024
1 parent cb7b9db commit 19f3377
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/code_quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
push:
branches:
- 'release-*'
- 'improvement-*'

jobs:
qodana:
Expand Down
2 changes: 1 addition & 1 deletion panther/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from panther.main import Panther # noqa: F401

__version__ = '4.2.4'
__version__ = '4.2.5'


def version():
Expand Down
2 changes: 2 additions & 0 deletions panther/db/queries/mongodb_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions panther/db/queries/pantherdb_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 7 additions & 1 deletion panther/db/queries/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 19f3377

Please sign in to comment.