Skip to content

Commit f7972e2

Browse files
Add docs (#9)
* add * add * add * add
1 parent 8c8eb32 commit f7972e2

12 files changed

+1024
-11
lines changed

.readthedocs.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# .readthedocs.yaml
2+
# Read the Docs configuration file
3+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4+
5+
# Required
6+
version: 2
7+
8+
# Set the OS, Python version and other tools you might need
9+
build:
10+
os: ubuntu-22.04
11+
tools:
12+
python: '3.12'
13+
# You can also specify other tool versions:
14+
jobs:
15+
post_create_environment:
16+
# Install poetry
17+
# https://python-poetry.org/docs/#installing-manually
18+
- pip install poetry
19+
# Tell poetry to not use a virtual environment
20+
- poetry config virtualenvs.create false
21+
post_install:
22+
# Install dependencies with 'docs' dependency group
23+
# https://python-poetry.org/docs/managing-dependencies/#dependency-groups
24+
- poetry install --with docs
25+
26+
# Build documentation in the "docs/" directory with Sphinx
27+
sphinx:
28+
configuration: docs/conf.py
29+
# Optionally build your docs in additional formats such as PDF and ePub
30+
# formats:
31+
# - pdf
32+
# - epub
33+

django_ltree/managers.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
from django.db import models
2-
2+
from typing import TYPE_CHECKING
33
from django_ltree.paths import PathGenerator
44

5+
from .querysets import TreeQuerySet
56

6-
class TreeQuerySet(models.QuerySet):
7-
def roots(self):
8-
return self.filter(path__depth=1)
9-
10-
def children(self, path):
11-
return self.filter(path__descendants=path, path__depth=len(path) + 1)
7+
if TYPE_CHECKING:
8+
from django_ltree.models import TreeModel
129

1310

1411
class TreeManager(models.Manager):
1512
def get_queryset(self):
13+
"""Returns a queryset with the models ordered by `path`"""
1614
return TreeQuerySet(model=self.model, using=self._db).order_by("path")
1715

18-
def roots(self):
16+
def roots(self) -> models.QuerySet["TreeModel"]:
17+
"""Returns the roots of a given model"""
1918
return self.filter().roots()
2019

21-
def children(self, path):
20+
def children(self, path: str) -> models.QuerySet["TreeModel"]:
21+
"""Returns the childrens of a given object"""
2222
return self.filter().children(path)
2323

24-
def create_child(self, parent=None, **kwargs):
24+
def create_child(
25+
self, parent: "TreeModel" = None, **kwargs
26+
) -> models.QuerySet["TreeModel"]:
27+
"""Creates a tree child with or without parent"""
2528
paths_in_use = parent.children() if parent else self.roots()
2629
prefix = parent.path if parent else None
2730
path_generator = PathGenerator(

django_ltree/querysets.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.db import models
2+
3+
from typing import TYPE_CHECKING
4+
5+
if TYPE_CHECKING:
6+
from .models import TreeModel
7+
8+
9+
class TreeQuerySet(models.QuerySet):
10+
def roots(self) -> models.QuerySet["TreeModel"]:
11+
return self.filter(path__depth=1)
12+
13+
def children(self, path: str) -> models.QuerySet["TreeModel"]:
14+
return self.filter(path__descendants=path, path__depth=len(path) + 1)

docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = .
9+
BUILDDIR = _build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/conf.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# For the full list of built-in configuration values, see the documentation:
4+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
6+
# -- Project information -----------------------------------------------------
7+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
8+
9+
project = "django_ltree"
10+
copyright = "2024, baseplate-admin"
11+
author = "baseplate-admin"
12+
release = "0.1.5"
13+
14+
# -- General configuration ---------------------------------------------------
15+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
16+
17+
extensions = [
18+
"sphinx.ext.autodoc",
19+
"sphinx.ext.intersphinx",
20+
"sphinx.ext.viewcode",
21+
]
22+
23+
templates_path = ["_templates"]
24+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
25+
26+
# List of patterns, relative to source directory, that match files and
27+
# directories to ignore when looking for source files.
28+
29+
30+
autodoc_typehints = "description"
31+
32+
# -- Options for HTML output -------------------------------------------------
33+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
34+
35+
html_theme = "furo"
36+
html_static_path = ["_static"]

docs/index.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. django_ltree documentation master file, created by
2+
sphinx-quickstart on Sat Feb 24 21:13:02 2024.
3+
You can adapt this file completely to your liking, but it should at least
4+
contain the root `toctree` directive.
5+
6+
Welcome to django_ltree's documentation!
7+
========================================
8+
9+
Augmenting `django` orm with postgres `ltree <https://www.postgresql.org/docs/current/ltree.html>`_ functionalities
10+
11+
.. toctree::
12+
:maxdepth: 2
13+
:caption: Contents:
14+
15+
installation
16+
usage
17+
manager
18+
19+
Indices and tables
20+
==================
21+
22+
* :ref:`genindex`
23+
* :ref:`modindex`
24+
* :ref:`search`

docs/installation.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Installation
2+
============
3+
4+
Requirements
5+
------------
6+
7+
Python 3.10 to 3.12 supported.
8+
9+
Django 4.2 to 5.0 supported.
10+
11+
12+
Installation
13+
------------
14+
15+
1. Install with **pip**:
16+
17+
.. code-block:: sh
18+
19+
python -m pip install django-ltree-2
20+
21+
2. Add django-ltree to your ``INSTALLED_APPS``:
22+
23+
.. code-block:: python
24+
25+
INSTALLED_APPS = [
26+
...,
27+
"django_ltree",
28+
...,
29+
]
30+
31+
3. Run migrations:
32+
33+
.. code-block:: sh
34+
35+
./manage.py migrate

docs/make.bat

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=.
11+
set BUILDDIR=_build
12+
13+
%SPHINXBUILD% >NUL 2>NUL
14+
if errorlevel 9009 (
15+
echo.
16+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
17+
echo.installed, then set the SPHINXBUILD environment variable to point
18+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
19+
echo.may add the Sphinx directory to PATH.
20+
echo.
21+
echo.If you don't have Sphinx installed, grab it from
22+
echo.https://www.sphinx-doc.org/
23+
exit /b 1
24+
)
25+
26+
if "%1" == "" goto help
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33+
34+
:end
35+
popd

docs/manager.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Manager
2+
=======
3+
4+
5+
.. currentmodule:: django_ltree.managers
6+
7+
.. class:: TreeManager
8+
9+
This manager augments the django model, allowing it to be queried with tree specific queries
10+
11+
.. automethod:: get_queryset
12+
13+
.. automethod:: roots
14+
15+
.. automethod:: children
16+
17+
.. automethod:: create_child

docs/usage.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Usage
2+
=====
3+
4+
Lets assume that our model looks like this.
5+
6+
.. code-block:: python
7+
8+
# models.py
9+
from django import models
10+
from django_ltree import TreeModel
11+
12+
class CustomTree(TreeModel):
13+
text = models.TextField()
14+
15+
16+
Create a child without parent
17+
-----------------------------
18+
19+
.. code-block:: python
20+
21+
from .models import CustomTree
22+
23+
CustomTree.objects.create_child(text='Hello world')
24+
25+
The following code with create a child with no parent (ie:roots of a tree)
26+
27+
Create a child with parent
28+
--------------------------
29+
Let's assume we want to add a child to the CustomTree object of `pk 1`
30+
31+
.. code-block:: python
32+
33+
from .models import CustomTree
34+
35+
# This must return a single object
36+
parent: CustomTree = CustomTree.objects.get(pk=1)
37+
CustomTree.objects.create_child(text='Hello world', parent=parent)
38+
39+
40+
Get all the roots of the model
41+
------------------------------
42+
A root means the the object that childrens anchor to.
43+
44+
.. code-block:: python
45+
46+
from .models import CustomTree
47+
48+
roots: list[CustomTree] = CustomTree.objects.roots()
49+
50+
51+
Get all the childrens of a object
52+
---------------------------------
53+
To get the childrens of a object, we can first get the object then call the QuerySet specific children function.
54+
55+
.. code-block:: python
56+
57+
from .models import CustomTree
58+
59+
instance = CustomTree.objects.get(pk=1)
60+
61+
childrens: list[CustomTree] = instance.children()
62+
63+
64+
65+
Get the length of childrens of a object
66+
---------------------------------------
67+
`django` specific database functions still work when we call the filter method.
68+
69+
Lets assume we want to get the childrens of `CustomTree` object whose pk is 1.
70+
71+
.. code-block:: python
72+
73+
from .models import CustomTree
74+
75+
instance = CustomTree.objects.get(pk=1)
76+
77+
childrens: int = instance.children().count()
78+

0 commit comments

Comments
 (0)