Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
baseplate-admin committed Feb 24, 2024
1 parent 24c1516 commit 3c5945c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion django_ltree/apps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.apps import AppConfig


class DjangoLtreeConfig(AppConfig):
class DjangoLtreeConfig(AppConfig): # type: ignore
default_auto_field = "django.db.models.BigAutoField"
name = "django_ltree"

Expand Down
4 changes: 2 additions & 2 deletions django_ltree/checks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.core.checks import Warning, register


@register
def check_database_backend_is_postgres(app_configs, **kwargs):
@register # type: ignore
def check_database_backend_is_postgres() -> list[Warning]:
from django.conf import settings

errors = []
Expand Down
12 changes: 7 additions & 5 deletions django_ltree/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ def __get__(

return PathValue(instance.__dict__[self.field_name])

def __set__(
def __set__( # type: ignore
self, instance: "PathValueProxy" | None, value: str
) -> NoReturn | "PathValueProxy":
) -> None | "PathValueProxy":
if instance is None:
return self

instance.__dict__[self.field_name] = value


class PathFormField(forms.CharField):
class PathFormField(forms.CharField): # type: ignore
default_validators = [path_label_validator]


class PathField(TextField):
class PathField(TextField): # type: ignore
default_validators = [path_label_validator]

def db_type(self, *args: TypeVarTuple) -> str:
Expand All @@ -85,7 +85,9 @@ def contribute_to_class(
super().contribute_to_class(cls, name)
setattr(cls, self.name, PathValueProxy(self.name))

def from_db_value(self, value, expression, connection, *args):
def from_db_value(
self, value: PathValue | "None", *args: TypeVarTuple
) -> PathValue | "None":
if value is None:
return value
return PathValue(value)
Expand Down
4 changes: 2 additions & 2 deletions django_ltree/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@


@PathField.register_lookup
class NLevel(Transform):
class NLevel(Transform): # type: ignore
lookup_name = "depth"
function = "nlevel"

@property
def output_field(self):
def output_field(self) -> fields.IntegerField:
return fields.IntegerField()
14 changes: 6 additions & 8 deletions django_ltree/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@
from django_ltree.models import TreeModel


class TreeManager(models.Manager):
def get_queryset(self) -> TreeQuerySet["TreeModel"]:
class TreeManager(models.Manager["TreeModel"]):
def get_queryset(self) -> TreeQuerySet:
"""Returns a queryset with the models ordered by `path`"""

return TreeQuerySet(model=self.model, using=self._db).order_by("path")

def roots(self) -> TreeQuerySet["TreeModel"]:
def roots(self) -> TreeQuerySet:
"""Returns the roots of a given model"""

return self.filter().roots()

def children(self, path: str) -> TreeQuerySet["TreeModel"]:
def children(self, path: str) -> TreeQuerySet:
"""Returns the childrens of a given object"""

return self.filter().children(path)

def create_child(
self, parent: "TreeModel" = None, **kwargs
) -> TreeQuerySet["TreeModel"]:
def create_child(self, parent: "TreeModel" | None = None, **kwargs) -> "TreeModel":
"""Creates a tree child with or without parent"""

paths_in_use = parent.children() if parent else self.roots()
Expand Down
12 changes: 8 additions & 4 deletions django_ltree/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from itertools import product

from .fields import PathValue
from typing import Sequence


class PathGenerator(object):
def __init__(self, prefix: str = None, skip: list[str] = None):
def __init__(self, prefix: str | None = None, skip: list[str] | None = None):
combinations = string.digits + string.ascii_letters

self.skip_paths = [] if skip is None else skip[:]
Expand All @@ -18,15 +19,18 @@ def __init__(self, prefix: str = None, skip: list[str] = None):
),
)

def __iter__(self):
def __iter__(self) -> "PathGenerator":
return self

def __next__(self):
def __next__(self) -> "PathValue" | None:
for val in self.product_iterator:
label = "".join(val)
path = PathValue(self.path_prefix + [label])
path = PathValue(
list(self.path_prefix) if self.path_prefix else [] + [label]
)
if path not in self.skip_paths:
return path
raise StopIteration

next = __next__

Expand Down
2 changes: 1 addition & 1 deletion django_ltree/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .models import TreeModel


class TreeQuerySet(models.QuerySet):
class TreeQuerySet(models.QuerySet): # type: ignore
def roots(self) -> models.QuerySet["TreeModel"]:
return self.filter(path__depth=1)

Expand Down

0 comments on commit 3c5945c

Please sign in to comment.