-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 add sphinx django model field directive
- Loading branch information
Showing
6 changed files
with
118 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from sphinx.application import Sphinx | ||
from sphinx.util.typing import ExtensionMetadata | ||
|
||
from .directives import ModelFieldsDirective | ||
from .roles import ModelFieldRole | ||
|
||
|
||
def setup(app: Sphinx) -> ExtensionMetadata: | ||
app.add_role("model_field", ModelFieldRole()) | ||
app.add_directive("model_fields", ModelFieldsDirective) | ||
|
||
return { | ||
"version": "0.1", | ||
"parallel_read_safe": True, | ||
"parallel_write_safe": True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import annotations | ||
|
||
from django.utils.module_loading import import_string | ||
|
||
from docutils import nodes | ||
from sphinx.util.docutils import SphinxDirective | ||
|
||
from .utils import get_field_representation | ||
|
||
|
||
class ModelFieldsDirective(SphinxDirective): | ||
"""Displays a model's fields with their name, helptext and default in a list""" | ||
|
||
required_arguments = 1 | ||
has_content = True | ||
|
||
def run(self) -> list[nodes.Node]: | ||
|
||
model_path = self.arguments[0] | ||
model = import_string(model_path) | ||
|
||
field_list = [] | ||
|
||
for line in self.content: | ||
field = model._meta.get_field(line) | ||
node = nodes.paragraph("", "", *get_field_representation(field)) | ||
field_list.append(nodes.list_item("", node)) | ||
|
||
bullet_list = nodes.bullet_list("", *field_list) | ||
|
||
return [bullet_list] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.utils.module_loading import import_string | ||
|
||
from docutils import nodes | ||
from sphinx.util.docutils import SphinxRole | ||
|
||
from .utils import get_field_representation | ||
|
||
|
||
class ModelFieldRole(SphinxRole): | ||
"""Displays a model field's name, helptext and default inline""" | ||
|
||
def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]: | ||
|
||
field_split = len(self.text) - self.text[::-1].index(".") - 1 | ||
model_path = self.text[:field_split] | ||
field_name = self.text[field_split + 1 :] | ||
|
||
model = import_string(model_path) | ||
field = model._meta.get_field(field_name) | ||
|
||
field_nodes = get_field_representation(field) | ||
return field_nodes, [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from django.db.models.fields import Field | ||
|
||
from docutils import nodes | ||
|
||
|
||
def get_field_representation(field: Field) -> list[nodes.literal | nodes.Text]: | ||
|
||
name = nodes.literal("", nodes.Text(field.name)) | ||
description = nodes.Text(f": {field.help_text}.") | ||
|
||
field_nodes = [name, description] | ||
|
||
# Should it include blank that default to empty string? | ||
if field.has_default(): | ||
field_nodes.append(nodes.Text(" Defaults to ")) | ||
field_nodes.append(nodes.literal("", nodes.Text(field.get_default()))) | ||
else: | ||
field_nodes.append(nodes.Text(" No default.")) | ||
|
||
return field_nodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters