Introduce DOCS_ROOT configuration parameter

This commit is contained in:
Jeremy Stretch 2020-03-06 09:35:58 -05:00
parent 7d236b607e
commit c50714ec42
4 changed files with 30 additions and 7 deletions

View File

@ -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 ## EMAIL
In order to send email, NetBox needs an email server configured. The following items can be defined within the `EMAIL` setting: In order to send email, NetBox needs an email server configured. The following items can be defined within the `EMAIL` setting:

View File

@ -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') DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a')
DEBUG = getattr(configuration, 'DEBUG', False) DEBUG = getattr(configuration, 'DEBUG', False)
DEVELOPER = getattr(configuration, 'DEVELOPER', 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', {}) EMAIL = getattr(configuration, 'EMAIL', {})
ENFORCE_GLOBAL_UNIQUE = getattr(configuration, 'ENFORCE_GLOBAL_UNIQUE', False) ENFORCE_GLOBAL_UNIQUE = getattr(configuration, 'ENFORCE_GLOBAL_UNIQUE', False)
EXEMPT_VIEW_PERMISSIONS = getattr(configuration, 'EXEMPT_VIEW_PERMISSIONS', []) EXEMPT_VIEW_PERMISSIONS = getattr(configuration, 'EXEMPT_VIEW_PERMISSIONS', [])

View File

@ -11,9 +11,11 @@
<div class="row"> <div class="row">
<div class="col-md-6 col-md-offset-3"> <div class="col-md-6 col-md-offset-3">
<h3> <h3>
<div class="pull-right"> {% if settings.DOCS_ROOT %}
<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#docs_modal"><i class="fa fa-question"></i></button> <div class="pull-right">
</div> <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#docs_modal"><i class="fa fa-question"></i></button>
</div>
{% endif %}
{% block title %}{% if obj.pk %}Editing {{ obj_type }} {{ obj }}{% else %}Add a new {{ obj_type }}{% endif %}{% endblock %} {% block title %}{% if obj.pk %}Editing {{ obj_type }} {{ obj }}{% else %}Add a new {{ obj_type }}{% endif %}{% endblock %}
</h3> </h3>
{% block tabs %}{% endblock %} {% block tabs %}{% endblock %}
@ -49,5 +51,7 @@
</div> </div>
</div> </div>
</form> </form>
{% 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 %} {% endblock %}

View File

@ -4,6 +4,7 @@ import re
import yaml import yaml
from django import template from django import template
from django.conf import settings
from django.urls import NoReverseMatch, reverse from django.urls import NoReverseMatch, reverse
from django.utils.html import strip_tags from django.utils.html import strip_tags
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
@ -222,9 +223,18 @@ def get_docs(model):
""" """
Render and return documentation for the specified model. Render and return documentation for the specified model.
""" """
path = '../docs/models/{}/{}.md'.format(model._meta.app_label, model._meta.model_name) path = '{}/models/{}/{}.md'.format(
with open(path) as docfile: settings.DOCS_ROOT,
content = docfile.read() 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 # Render Markdown with the admonition extension
content = markdown(content, extensions=['admonition', 'fenced_code']) content = markdown(content, extensions=['admonition', 'fenced_code'])