Skip to content

Commit

Permalink
Merge pull request #576 from collerek/check_null_json
Browse files Browse the repository at this point in the history
Add plugable queryset_class, enhance IndexColumns and bug fixes
  • Loading branch information
collerek authored Feb 25, 2022
2 parents 5718999 + b599cc2 commit 989e11e
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 271 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.1.0
hooks:
- id: black
exclude: ^(docs_src/|examples/)
Expand All @@ -11,7 +11,7 @@ repos:
exclude: ^(docs_src/|examples/|tests/)
args: [ '--max-line-length=88' ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910
rev: v0.931
hooks:
- id: mypy
exclude: ^(docs_src/|examples/)
Expand Down
12 changes: 12 additions & 0 deletions docs/releases.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# 0.10.25

## ✨ Features

* Add `queryset_class` option to `Model.Meta` that allows you to easily swap `QuerySet` for your Model (by @ponytailer - thanks!) [#538](https://github.com/collerek/ormar/pull/538)
* Allow passing extra `kwargs` to `IndexColumns` that will be passed to sqlalchemy `Index` (by @zevisert - thanks) [#575](https://github.com/collerek/ormar/pull/538)

## 🐛 Fixes

* Fix nullable setting on `JSON` fields [#529](https://github.com/collerek/ormar/issues/529)
* Fix bytes/str mismatch in bulk operations when using orjson instead of json (by @ponytailer - thanks!) [#538](https://github.com/collerek/ormar/pull/538)

# 0.10.24

## ✨ Features
Expand Down
4 changes: 2 additions & 2 deletions ormar/fields/model_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def __new__(cls, *args: Any, **kwargs: Any) -> BaseField: # type: ignore
unique=kwargs.pop("unique", False),
pydantic_only=pydantic_only,
autoincrement=autoincrement,
column_type=cls.get_column_type(**kwargs),
column_type=cls.get_column_type(**kwargs, sql_nullable=sql_nullable),
choices=choices,
encrypt_secret=encrypt_secret,
encrypt_backend=encrypt_backend,
Expand Down Expand Up @@ -516,7 +516,7 @@ def get_column_type(cls, **kwargs: Any) -> Any:
:return: initialized column with proper options
:rtype: sqlalchemy Column
"""
return sqlalchemy.JSON()
return sqlalchemy.JSON(none_as_null=kwargs.get("sql_nullable", False))


if TYPE_CHECKING: # pragma: nocover # noqa: C901
Expand Down
6 changes: 4 additions & 2 deletions ormar/fields/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime
import decimal
import uuid
from typing import Any, Callable, Dict, Union
from typing import Any, Callable, Dict, Optional, Union

import pydantic
from pydantic.datetime_parse import parse_date, parse_datetime, parse_time
Expand Down Expand Up @@ -37,7 +37,9 @@ def encode_bytes(value: Union[str, bytes], represent_as_string: bool = False) ->
return value if isinstance(value, bytes) else value.encode("utf-8")


def encode_json(value: Any) -> str:
def encode_json(value: Any) -> Optional[str]:
if isinstance(value, (datetime.date, datetime.datetime, datetime.time)):
value = value.isoformat()
value = json.dumps(value) if not isinstance(value, str) else re_dump_value(value)
value = value.decode("utf-8") if isinstance(value, bytes) else value
return value
Expand Down
1 change: 1 addition & 0 deletions ormar/models/mixins/merge_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def merge_two_instances(
setattr(other, field_name, value_to_set)
elif (
isinstance(current_field, ormar.Model)
and isinstance(other_value, ormar.Model)
and current_field.pk == other_value.pk
):
setattr(
Expand Down
16 changes: 4 additions & 12 deletions ormar/models/newbasemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
import databases
import pydantic
import sqlalchemy

from ormar.fields.parsers import encode_json
from ormar.models.utils import Extra
from pydantic import BaseModel

try:
import orjson as json
except ImportError: # pragma: no cover
import json # type: ignore


import ormar # noqa I100
from ormar.exceptions import ModelError, ModelPersistenceError
Expand Down Expand Up @@ -912,7 +909,7 @@ def _convert_bytes_to_str(self, column_name: str, value: Any) -> Union[str, Dict
return base64.b64encode(value).decode()
return value

def _convert_json(self, column_name: str, value: Any) -> Union[str, Dict]:
def _convert_json(self, column_name: str, value: Any) -> Union[str, Dict, None]:
"""
Converts value to/from json if needed (for Json columns).
Expand All @@ -925,12 +922,7 @@ def _convert_json(self, column_name: str, value: Any) -> Union[str, Dict]:
"""
if column_name not in self._json_fields:
return value
if not isinstance(value, str):
try:
value = json.dumps(value)
except TypeError: # pragma no cover
pass
return value.decode("utf-8") if isinstance(value, bytes) else value
return encode_json(value)

def _extract_own_model_fields(self) -> Dict:
"""
Expand Down
Loading

0 comments on commit 989e11e

Please sign in to comment.