From c50714ec42aa0d0c01301956b2065a10eb864c62 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 6 Mar 2020 09:35:58 -0500 Subject: [PATCH] Introduce DOCS_ROOT configuration parameter --- docs/configuration/optional-settings.md | 8 ++++++++ netbox/netbox/settings.py | 1 + netbox/templates/utilities/obj_edit.html | 12 ++++++++---- netbox/utilities/templatetags/helpers.py | 16 +++++++++++++--- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/docs/configuration/optional-settings.md b/docs/configuration/optional-settings.md index cbe01728c..337b41b1b 100644 --- a/docs/configuration/optional-settings.md +++ b/docs/configuration/optional-settings.md @@ -98,6 +98,14 @@ This parameter serves as a safeguard to prevent some potentially dangerous behav --- +## DOCS_ROOT + +Default: `$INSTALL_DIR/docs/` + +The file path to NetBox's documentation. This is used when presenting context-sensitive documentation in the web UI. by default, this will be the `docs/` directory within the root NetBox installation path. (Set this to `None` to disable the embedded documentation.) + +--- + ## EMAIL In order to send email, NetBox needs an email server configured. The following items can be defined within the `EMAIL` setting: diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 7f4f44b1a..df1ab908a 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -75,6 +75,7 @@ DATE_FORMAT = getattr(configuration, 'DATE_FORMAT', 'N j, Y') DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a') DEBUG = getattr(configuration, 'DEBUG', False) DEVELOPER = getattr(configuration, 'DEVELOPER', False) +DOCS_ROOT = getattr(configuration, 'DOCS_ROOT', os.path.join(os.path.dirname(BASE_DIR), 'docs')) EMAIL = getattr(configuration, 'EMAIL', {}) ENFORCE_GLOBAL_UNIQUE = getattr(configuration, 'ENFORCE_GLOBAL_UNIQUE', False) EXEMPT_VIEW_PERMISSIONS = getattr(configuration, 'EXEMPT_VIEW_PERMISSIONS', []) diff --git a/netbox/templates/utilities/obj_edit.html b/netbox/templates/utilities/obj_edit.html index 286563a23..0d7760f30 100644 --- a/netbox/templates/utilities/obj_edit.html +++ b/netbox/templates/utilities/obj_edit.html @@ -11,9 +11,11 @@

-
- -
+ {% if settings.DOCS_ROOT %} +
+ +
+ {% endif %} {% block title %}{% if obj.pk %}Editing {{ obj_type }} {{ obj }}{% else %}Add a new {{ obj_type }}{% endif %}{% endblock %}

{% block tabs %}{% endblock %} @@ -49,5 +51,7 @@
- {% include 'inc/modal.html' with name='docs' content=obj|get_docs %} + {% if settings.DOCS_ROOT %} + {% include 'inc/modal.html' with name='docs' content=obj|get_docs %} + {% endif %} {% endblock %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index a57cfc66d..5cede8a7e 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -4,6 +4,7 @@ import re import yaml from django import template +from django.conf import settings from django.urls import NoReverseMatch, reverse from django.utils.html import strip_tags from django.utils.safestring import mark_safe @@ -222,9 +223,18 @@ def get_docs(model): """ Render and return documentation for the specified model. """ - path = '../docs/models/{}/{}.md'.format(model._meta.app_label, model._meta.model_name) - with open(path) as docfile: - content = docfile.read() + path = '{}/models/{}/{}.md'.format( + settings.DOCS_ROOT, + model._meta.app_label, + model._meta.model_name + ) + try: + with open(path) as docfile: + content = docfile.read() + except FileNotFoundError: + return "Unable to load documentation, file not found: {}".format(path) + except IOError: + return "Unable to load documentation, error reading file: {}".format(path) # Render Markdown with the admonition extension content = markdown(content, extensions=['admonition', 'fenced_code'])