Skip to content

Commit

Permalink
Merge pull request #21 from collerek/fix_json_schema
Browse files Browse the repository at this point in the history
Fix json schema generation
  • Loading branch information
collerek authored Oct 27, 2020
2 parents f192ae9 + e4221d6 commit a8f0ec0
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 79 deletions.
Binary file removed .coverage
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ p38venv
.idea
.pytest_cache
.mypy_cache
.coverage
*.pyc
*.log
test.db
Expand Down
25 changes: 23 additions & 2 deletions docs/fastapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Those models will be used insted of pydantic ones.
Define your desired endpoints, note how `ormar` models are used both
as `response_model` and as a requests parameters.

```python hl_lines="50-77"
```python hl_lines="50-79"
--8<-- "../docs_src/fastapi/docs001.py"
```

Expand All @@ -57,6 +57,23 @@ as `response_model` and as a requests parameters.

## Test the application

### Run fastapi

If you want to run this script and play with fastapi swagger install uvicorn first

`pip install uvicorn`

And launch the fastapi.

`uvicorn <filename_without_extension>:app --reload`

Now you can navigate to your browser (by default fastapi address is `127.0.0.1:8000/docs`) and play with the api.

!!!info
You can read more about running fastapi in [fastapi][fastapi] docs.

### Test with pytest

Here you have a sample test that will prove that everything works as intended.

Be sure to create the tables first. If you are using pytest you can use a fixture.
Expand Down Expand Up @@ -109,9 +126,13 @@ def test_all_endpoints():
assert len(items) == 0
```

!!!tip
If you want to see more test cases and how to test ormar/fastapi see [tests][tests] directory in the github repo

!!!info
You can read more on testing fastapi in [fastapi][fastapi] docs.

[fastapi]: https://fastapi.tiangolo.com/
[models]: ./models.md
[database initialization]: ../models/#database-initialization-migrations
[database initialization]: ../models/#database-initialization-migrations
[tests]: https://github.com/collerek/ormar/tree/master/tests
12 changes: 11 additions & 1 deletion docs/releases.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.3.9

* Fix json schema generation as of [#19][#19]
* Fix for not initialized ManyToMany relations in fastapi copies of ormar.Models
* Update docs in regard of fastapi use
* Add tests to verify fastapi/docs proper generation

# 0.3.8

* Added possibility to provide alternative database column names with name parameter to all fields.
Expand Down Expand Up @@ -42,4 +49,7 @@ Add queryset level methods

# 0.3.0

* Added ManyToMany field and support for many to many relations
* Added ManyToMany field and support for many to many relations


[#19]: https://github.com/collerek/ormar/issues/19
4 changes: 3 additions & 1 deletion docs_src/fastapi/docs001.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ async def get_item(item_id: int, item: Item):


@app.delete("/items/{item_id}")
async def delete_item(item_id: int, item: Item):
async def delete_item(item_id: int, item: Item = None):
if item:
return {"deleted_rows": await item.delete()}
item_db = await Item.objects.get(pk=item_id)
return {"deleted_rows": await item_db.delete()}
6 changes: 3 additions & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ nav:
- Release Notes: releases.md
repo_name: collerek/ormar
repo_url: https://github.com/collerek/ormar
google_analytics:
- UA-72514911-3
- auto
#google_analytics:
# - UA-72514911-3
# - auto
theme:
name: material
highlightjs: true
Expand Down
2 changes: 1 addition & 1 deletion ormar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __repr__(self) -> str:

Undefined = UndefinedType()

__version__ = "0.3.8"
__version__ = "0.3.9"
__all__ = [
"Integer",
"BigInteger",
Expand Down
1 change: 1 addition & 0 deletions ormar/fields/foreign_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def ForeignKey( # noqa CFQ002
pydantic_only=False,
default=None,
server_default=None,
__pydantic_model__=to,
)

return type("ForeignKey", (ForeignKeyField, BaseField), namespace)
Expand Down
14 changes: 13 additions & 1 deletion ormar/fields/many_to_many.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import TYPE_CHECKING, Type
from typing import Dict, TYPE_CHECKING, Type

from ormar.fields import BaseField
from ormar.fields.foreign_key import ForeignKeyField

if TYPE_CHECKING: # pragma no cover
from ormar.models import Model

REF_PREFIX = "#/components/schemas/"


def ManyToMany(
to: Type["Model"],
Expand All @@ -31,10 +33,20 @@ def ManyToMany(
pydantic_only=False,
default=None,
server_default=None,
__pydantic_model__=to,
# __origin__=List,
# __args__=[Optional[to]]
)

return type("ManyToMany", (ManyToManyField, BaseField), namespace)


class ManyToManyField(ForeignKeyField):
through: Type["Model"]

@classmethod
def __modify_schema__(cls, field_schema: Dict) -> None:
field_schema["type"] = "array"
field_schema["title"] = cls.name.title()
field_schema["definitions"] = {f"{cls.to.__name__}": cls.to.schema()}
field_schema["items"] = {"$ref": f"{REF_PREFIX}{cls.to.__name__}"}
4 changes: 2 additions & 2 deletions ormar/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async def update(self, **kwargs: Any) -> "Model":

self_fields = self._extract_model_db_fields()
self_fields.pop(self.get_column_name_from_alias(self.Meta.pkname))
self_fields = self.objects._translate_columns_to_aliases(self_fields)
self_fields = self.translate_columns_to_aliases(self_fields)
expr = self.Meta.table.update().values(**self_fields)
expr = expr.where(self.pk_column == getattr(self, self.Meta.pkname))

Expand All @@ -166,6 +166,6 @@ async def load(self) -> "Model":
"Instance was deleted from database and cannot be refreshed"
)
kwargs = dict(row)
kwargs = self.objects._translate_aliases_to_columns(kwargs)
kwargs = self.translate_aliases_to_columns(kwargs)
self.from_dict(kwargs)
return self
30 changes: 28 additions & 2 deletions ormar/models/modelproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ormar.exceptions import RelationshipInstanceError
from ormar.fields import BaseField, ManyToManyField
from ormar.fields.foreign_key import ForeignKeyField
from ormar.models.metaclass import ModelMeta
from ormar.models.metaclass import ModelMeta, expand_reverse_relationships

if TYPE_CHECKING: # pragma no cover
from ormar import Model
Expand Down Expand Up @@ -112,9 +112,10 @@ def _extract_model_db_fields(self) -> Dict:
return self_fields

@staticmethod
def resolve_relation_name(
def resolve_relation_name( # noqa CCR001
item: Union["NewBaseModel", Type["NewBaseModel"]],
related: Union["NewBaseModel", Type["NewBaseModel"]],
register_missing: bool = True,
) -> str:
for name, field in item.Meta.model_fields.items():
if issubclass(field, ForeignKeyField):
Expand All @@ -123,6 +124,13 @@ def resolve_relation_name(
# so we need to compare Meta too as this one is copied as is
if field.to == related.__class__ or field.to.Meta == related.Meta:
return name
# fallback for not registered relation
if register_missing:
expand_reverse_relationships(related.__class__) # type: ignore
return ModelTableProxy.resolve_relation_name(
item, related, register_missing=False
)

raise ValueError(
f"No relation between {item.get_name()} and {related.get_name()}"
) # pragma nocover
Expand All @@ -140,6 +148,24 @@ def resolve_relation_field(
)
return to_field

@classmethod
def translate_columns_to_aliases(cls, new_kwargs: Dict) -> Dict:
for field_name, field in cls.Meta.model_fields.items():
if (
field_name in new_kwargs
and field.name is not None
and field.name != field_name
):
new_kwargs[field.name] = new_kwargs.pop(field_name)
return new_kwargs

@classmethod
def translate_aliases_to_columns(cls, new_kwargs: Dict) -> Dict:
for field_name, field in cls.Meta.model_fields.items():
if field.name in new_kwargs and field.name != field_name:
new_kwargs[field_name] = new_kwargs.pop(field.name)
return new_kwargs

@classmethod
def merge_instances_list(cls, result_rows: List["Model"]) -> List["Model"]:
merged_rows: List["Model"] = []
Expand Down
4 changes: 2 additions & 2 deletions ormar/models/newbasemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
)

def __setattr__(self, name: str, value: Any) -> None: # noqa CCR001
if name in self.__slots__:
if name in ("_orm_id", "_orm_saved", "_orm"):
object.__setattr__(self, name, value)
elif name == "pk":
object.__setattr__(self, self.Meta.pkname, value)
Expand Down Expand Up @@ -137,7 +137,7 @@ def _extract_related_model_instead_of_field(
alias = self.get_column_alias(item)
if alias in self._orm:
return self._orm.get(alias)
return None
return None # pragma no cover

def __eq__(self, other: object) -> bool:
if isinstance(other, NewBaseModel):
Expand Down
22 changes: 3 additions & 19 deletions ormar/queryset/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _prepare_model_to_save(self, new_kwargs: dict) -> dict:
new_kwargs = self._remove_pk_from_kwargs(new_kwargs)
new_kwargs = self.model.substitute_models_with_pks(new_kwargs)
new_kwargs = self._populate_default_values(new_kwargs)
new_kwargs = self._translate_columns_to_aliases(new_kwargs)
new_kwargs = self.model.translate_columns_to_aliases(new_kwargs)
return new_kwargs

def _populate_default_values(self, new_kwargs: dict) -> dict:
Expand All @@ -83,22 +83,6 @@ def _populate_default_values(self, new_kwargs: dict) -> dict:
new_kwargs[field_name] = field.get_default()
return new_kwargs

def _translate_columns_to_aliases(self, new_kwargs: dict) -> dict:
for field_name, field in self.model_meta.model_fields.items():
if (
field_name in new_kwargs
and field.name is not None
and field.name != field_name
):
new_kwargs[field.name] = new_kwargs.pop(field_name)
return new_kwargs

def _translate_aliases_to_columns(self, new_kwargs: dict) -> dict:
for field_name, field in self.model_meta.model_fields.items():
if field.name in new_kwargs and field.name != field_name:
new_kwargs[field_name] = new_kwargs.pop(field.name)
return new_kwargs

def _remove_pk_from_kwargs(self, new_kwargs: dict) -> dict:
pkname = self.model_meta.pkname
pk = self.model_meta.model_fields[pkname]
Expand Down Expand Up @@ -207,7 +191,7 @@ async def count(self) -> int:
async def update(self, each: bool = False, **kwargs: Any) -> int:
self_fields = self.model.extract_db_own_fields()
updates = {k: v for k, v in kwargs.items() if k in self_fields}
updates = self._translate_columns_to_aliases(updates)
updates = self.model.translate_columns_to_aliases(updates)
if not each and not self.filter_clauses:
raise QueryDefinitionError(
"You cannot update without filtering the queryset first. "
Expand Down Expand Up @@ -353,7 +337,7 @@ async def bulk_update(
f"{self.model.__name__} has to have {pk_name} filled."
)
new_kwargs = self.model.substitute_models_with_pks(new_kwargs)
new_kwargs = self._translate_columns_to_aliases(new_kwargs)
new_kwargs = self.model.translate_columns_to_aliases(new_kwargs)
new_kwargs = {"new_" + k: v for k, v in new_kwargs.items() if k in columns}
ready_objects.append(new_kwargs)

Expand Down
2 changes: 2 additions & 0 deletions ormar/relations/relation_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def _add_relation(self, field: Type[BaseField]) -> None:
to=field.to,
through=getattr(field, "through", None),
)
if field.name not in self._related_names:
self._related_names.append(field.name)

def __contains__(self, item: str) -> bool:
return item in self._related_names
Expand Down
2 changes: 2 additions & 0 deletions ormar/relations/relation_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ async def add(self, item: "Model") -> None:
if self.relation._type == ormar.RelationType.MULTIPLE:
await self.queryset_proxy.create_through_instance(item)
rel_name = item.resolve_relation_name(item, self._owner)
if rel_name not in item._orm:
item._orm._add_relation(item.Meta.model_fields[rel_name])
setattr(item, rel_name, self._owner)
Loading

0 comments on commit a8f0ec0

Please sign in to comment.