|
1 | | -"""Sphinx configuration.""" |
| 1 | +"""Sphinx configuration for django-tenant-options documentation.""" |
2 | 2 |
|
3 | | -import django |
4 | | -from django.conf import settings |
| 3 | +import inspect |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from datetime import datetime |
5 | 7 |
|
| 8 | +import django |
| 9 | +from django.utils.html import strip_tags |
6 | 10 |
|
7 | | -settings.configure(DEBUG=True) |
8 | 11 |
|
9 | | -# Initialize Django |
| 12 | +sys.path.insert(0, os.path.abspath("..")) |
| 13 | +os.environ["DJANGO_SETTINGS_MODULE"] = "example_project.settings" |
10 | 14 | django.setup() |
11 | 15 |
|
| 16 | + |
| 17 | +# Project information |
12 | 18 | project = "django-tenant-options" |
13 | 19 | author = "Jack Linke" |
14 | | -copyright = "2024, Jack Linke" |
| 20 | +copyright = f"{datetime.now().year}, {author}" |
| 21 | + |
| 22 | +# General configuration |
15 | 23 | extensions = [ |
16 | 24 | "sphinx.ext.autodoc", |
17 | 25 | "sphinx.ext.napoleon", |
| 26 | + "sphinx.ext.intersphinx", |
| 27 | + "sphinx.ext.viewcode", |
18 | 28 | "sphinx_click", |
19 | 29 | "myst_parser", |
20 | 30 | ] |
21 | | -autodoc_typehints = "description" |
| 31 | + |
| 32 | +# Any paths that contain templates here, relative to this directory. |
| 33 | +# templates_path = ["_templates"] |
| 34 | + |
| 35 | +# List of patterns, relative to source directory, that match files and |
| 36 | +# directories to ignore when looking for source files. |
| 37 | +exclude_patterns = ["_build"] |
| 38 | + |
| 39 | +# The name of the Pygments (syntax highlighting) style to use. |
| 40 | +pygments_style = "sphinx" |
| 41 | + |
| 42 | +# -- Options for HTML output ------------------------------------------------- |
| 43 | + |
| 44 | +# The theme to use for HTML and HTML Help pages. |
22 | 45 | html_theme = "furo" |
| 46 | + |
| 47 | +# Add any paths that contain custom static files (such as style sheets) here, |
| 48 | +# relative to this directory. They are copied after the builtin static files, |
| 49 | +# so a file named "default.css" will overwrite the builtin "default.css". |
| 50 | +# html_static_path = ["_static"] |
| 51 | + |
| 52 | +# -- Extension configuration ------------------------------------------------- |
| 53 | + |
| 54 | +# Napoleon settings |
| 55 | +napoleon_google_docstring = True |
| 56 | +napoleon_numpy_docstring = False |
| 57 | +napoleon_include_init_with_doc = False |
| 58 | +napoleon_include_private_with_doc = False |
| 59 | +napoleon_include_special_with_doc = True |
| 60 | +napoleon_use_admonition_for_examples = False |
| 61 | +napoleon_use_admonition_for_notes = False |
| 62 | +napoleon_use_admonition_for_references = False |
| 63 | +napoleon_use_ivar = False |
| 64 | +napoleon_use_param = True |
| 65 | +napoleon_use_rtype = True |
| 66 | +napoleon_preprocess_types = False |
| 67 | +napoleon_type_aliases = None |
| 68 | +napoleon_attr_annotations = True |
| 69 | + |
| 70 | +# Autodoc settings |
| 71 | +autodoc_typehints = "description" |
| 72 | +autodoc_default_options = { |
| 73 | + "members": True, |
| 74 | + "special-members": "__init__", |
| 75 | + "exclude-members": "__weakref__", |
| 76 | +} |
| 77 | +autodoc_mock_imports = [ |
| 78 | + "django", |
| 79 | +] # Add any modules that might cause import errors during doc building |
| 80 | + |
| 81 | +# Intersphinx settings |
| 82 | +intersphinx_mapping = { |
| 83 | + "python": ("https://docs.python.org/3", None), |
| 84 | + "django": ("https://docs.djangoproject.com/en/stable/", "https://docs.djangoproject.com/en/stable/_objects/"), |
| 85 | +} |
| 86 | + |
| 87 | +# MyST Parser settings |
| 88 | +myst_enable_extensions = [ |
| 89 | + "amsmath", |
| 90 | + "colon_fence", |
| 91 | + "deflist", |
| 92 | + "dollarmath", |
| 93 | + "html_admonition", |
| 94 | + "html_image", |
| 95 | + "linkify", |
| 96 | + "replacements", |
| 97 | + "smartquotes", |
| 98 | + "substitution", |
| 99 | + "tasklist", |
| 100 | +] |
| 101 | + |
| 102 | + |
| 103 | +def project_django_models(app, what, name, obj, options, lines): # pylint: disable=W0613 disable=R0913 |
| 104 | + """Process Django models for autodoc. |
| 105 | +
|
| 106 | + From: https://djangosnippets.org/snippets/2533/ |
| 107 | + """ |
| 108 | + from django.db import models # pylint: disable=C0415 |
| 109 | + |
| 110 | + # Only look at objects that inherit from Django's base model class |
| 111 | + if inspect.isclass(obj) and issubclass(obj, models.Model): |
| 112 | + # Grab the field list from the meta class |
| 113 | + fields = obj._meta.get_fields() # pylint: disable=W0212 |
| 114 | + |
| 115 | + for field in fields: |
| 116 | + # If it's a reverse relation, skip it |
| 117 | + if isinstance( |
| 118 | + field, |
| 119 | + ( |
| 120 | + models.fields.related.ManyToOneRel, |
| 121 | + models.fields.related.ManyToManyRel, |
| 122 | + models.fields.related.OneToOneRel, |
| 123 | + ), |
| 124 | + ): |
| 125 | + continue |
| 126 | + |
| 127 | + # Decode and strip any html out of the field's help text |
| 128 | + help_text = strip_tags(field.help_text) if hasattr(field, "help_text") else None |
| 129 | + |
| 130 | + # Decode and capitalize the verbose name, for use if there isn't |
| 131 | + # any help text |
| 132 | + verbose_name = field.verbose_name if hasattr(field, "verbose_name") else "" |
| 133 | + |
| 134 | + if help_text: |
| 135 | + # Add the model field to the end of the docstring as a param |
| 136 | + # using the help text as the description |
| 137 | + lines.append(f":param {field.attname}: {help_text}") |
| 138 | + else: |
| 139 | + # Add the model field to the end of the docstring as a param |
| 140 | + # using the verbose name as the description |
| 141 | + lines.append(f":param {field.attname}: {verbose_name}") |
| 142 | + |
| 143 | + # Add the field's type to the docstring |
| 144 | + lines.append(f":type {field.attname}: {field.__class__.__name__}") |
| 145 | + |
| 146 | + # Return the extended docstring |
| 147 | + return lines |
| 148 | + |
| 149 | + |
| 150 | +def setup(app): |
| 151 | + """Register the Django model processor with Sphinx.""" |
| 152 | + app.connect("autodoc-process-docstring", project_django_models) |
0 commit comments