-
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,17 @@ | ||
from sphinx.application import Sphinx | ||
from sphinx.util.typing import ExtensionMetadata | ||
|
||
|
||
from .roles import ModelFieldRole | ||
from .directives import ModelFieldsDirective | ||
|
||
|
||
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,32 @@ | ||
from __future__ import annotations | ||
|
||
from docutils import nodes | ||
|
||
from sphinx.util.docutils import SphinxDirective | ||
|
||
from django.utils.module_loading import import_string | ||
|
||
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,21 @@ | ||
from sphinx.util.docutils import SphinxRole | ||
|
||
from docutils import nodes | ||
from django.utils.module_loading import import_string | ||
|
||
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]]: | ||
|
||
args = self.text.split(" ") | ||
|
||
model_path = args[0] | ||
model = import_string(model_path) | ||
field_name = args[1] | ||
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,17 @@ | ||
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] | ||
|
||
if field.get_default(): | ||
field_nodes.append(nodes.Text(" Defaults to ")) | ||
field_nodes.append(nodes.literal("", nodes.Text(field.get_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