Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some issue #996

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ormar/fields/foreign_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ def ForeignKey( # type: ignore # noqa CFQ002
skip_field=skip_field,
)

Field = type("ForeignKey", (ForeignKeyField, BaseField), {})
return Field(**namespace)
field_class = type("ForeignKey", (ForeignKeyField, BaseField), {})
return field_class(**namespace)


class ForeignKeyField(BaseField):
Expand Down
6 changes: 3 additions & 3 deletions ormar/models/metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def get_constraint_copy(
constraint: ColumnCollectionConstraint,
) -> Union[UniqueColumns, IndexColumns, CheckColumns]:
"""
Copy the constraint and unpacking it's values
Copy the constraint and unpacking its values

:raises ValueError: if non subclass of ColumnCollectionConstraint
:param value: an instance of the ColumnCollectionConstraint class
Expand Down Expand Up @@ -433,7 +433,7 @@ def extract_from_parents_definition( # noqa: CCR001
If the class is parsed first time annotations and field definition is parsed
from the class.__dict__.

If the class is a ormar.Model it is skipped.
If the class is an ormar.Model it is skipped.

:param base_class: one of the parent classes
:type base_class: Model or model parent class
Expand Down Expand Up @@ -537,7 +537,7 @@ def add_field_descriptor(
:param field: model field to add descriptor for
:type field: BaseField
:param new_model: model with fields
:type new_model: Type["Model]
:type new_model: Type["Model"]
"""
if field.is_relation:
setattr(new_model, name, RelationDescriptor(name=name))
Expand Down
2 changes: 0 additions & 2 deletions ormar/queryset/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,6 @@ async def first(self, *args: Any, **kwargs: Any) -> "T":
"""
Gets the first row from the db ordered by primary key column ascending.

:raises NoMatch: if no rows are returned
:raises MultipleMatches: if more than 1 row is returned.
:param kwargs: fields names and proper value types
:type kwargs: Any
:return: returned model
Expand Down
49 changes: 27 additions & 22 deletions ormar/queryset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Type,
Union,
)
from functools import lru_cache

if TYPE_CHECKING: # pragma no cover
from ormar import Model, BaseField
Expand Down Expand Up @@ -59,28 +60,32 @@ def translate_list_to_dict( # noqa: CCR001
:return: converted to dictionary input list
:rtype: Dict
"""
new_dict: Dict = dict()
for path in list_to_trans:
current_level = new_dict
parts = path.split("__")
def_val: Any = ...
if is_order:
if parts[0][0] == "-":
def_val = "desc"
parts[0] = parts[0][1:]
else:
def_val = "asc"

for ind, part in enumerate(parts):
is_last = ind == len(parts) - 1
if check_node_not_dict_or_not_last_node(
part=part, is_last=is_last, current_level=current_level
):
current_level[part] = dict()
elif part not in current_level:
current_level[part] = def_val
current_level = current_level[part]
return new_dict
@lru_cache(maxsize=128)
def _translate(translate_set: str) -> Dict:
new_dict: Dict = dict()
for path in translate_set.split(":"):
current_level = new_dict
parts = path.split("__")
def_val: Any = ...
if is_order:
if parts[0][0] == "-":
def_val = "desc"
parts[0] = parts[0][1:]
else:
def_val = "asc"

for ind, part in enumerate(parts):
is_last = ind == len(parts) - 1
if check_node_not_dict_or_not_last_node(
part=part, is_last=is_last, current_level=current_level
):
current_level[part] = dict()
elif part not in current_level:
current_level[part] = def_val
current_level = current_level[part]
return new_dict

return dict() if not list_to_trans else _translate(":".join(sorted(list_to_trans)))


def convert_set_to_required_dict(set_to_convert: set) -> Dict:
Expand Down
2 changes: 1 addition & 1 deletion ormar/signals/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def send(self, sender: Type["Model"], **kwargs: Any) -> None:
class SignalEmitter(dict):
"""
Emitter that registers the signals in internal dictionary.
If signal with given name does not exist it's auto added on access.
If signal with given name does not exist its auto added on access.
"""

def __getattr__(self, item: str) -> Signal:
Expand Down