Merge pull request #3074 from digitalocean/969-custom-links

969 custom links
This commit is contained in:
Jeremy Stretch 2019-04-15 21:43:17 -04:00 committed by GitHub
commit 2b2de8f8a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 301 additions and 3 deletions

View File

@ -3,7 +3,7 @@ from django.contrib import admin
from netbox.admin import admin_site
from utilities.forms import LaxURLField
from .models import CustomField, CustomFieldChoice, Graph, ExportTemplate, TopologyMap, Webhook
from .models import CustomField, CustomFieldChoice, CustomLink, Graph, ExportTemplate, TopologyMap, Webhook
def order_content_types(field):
@ -77,6 +77,30 @@ class CustomFieldAdmin(admin.ModelAdmin):
return ', '.join([ct.name for ct in obj.obj_type.all()])
#
# Custom links
#
class CustomLinkForm(forms.ModelForm):
class Meta:
model = CustomLink
exclude = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Format ContentType choices
order_content_types(self.fields['content_type'])
self.fields['content_type'].choices.insert(0, ('', '---------'))
@admin.register(CustomLink, site=admin_site)
class CustomLinkAdmin(admin.ModelAdmin):
list_display = ['name', 'content_type', 'group_name', 'weight']
form = CustomLinkForm
#
# Graphs
#

View File

@ -35,6 +35,46 @@ CF_FILTER_CHOICES = (
(CF_FILTER_EXACT, 'Exact'),
)
# Custom links
CUSTOM_LINK_MODELS = [
'circuits.circuit',
'circuits.provider',
'dcim.cable',
'dcim.device',
'dcim.devicetype',
'dcim.powerpanel',
'dcim.powerfeed',
'dcim.rack',
'dcim.site',
'ipam.aggregate',
'ipam.ipaddress',
'ipam.prefix',
'ipam.service',
'ipam.vlan',
'ipam.vrf',
'secrets.secret',
'tenancy.tenant',
'virtualization.cluster',
'virtualization.virtualmachine',
]
BUTTON_CLASS_DEFAULT = 'default'
BUTTON_CLASS_PRIMARY = 'primary'
BUTTON_CLASS_SUCCESS = 'success'
BUTTON_CLASS_INFO = 'info'
BUTTON_CLASS_WARNING = 'warning'
BUTTON_CLASS_DANGER = 'danger'
BUTTON_CLASS_LINK = 'link'
BUTTON_CLASS_CHOICES = (
(BUTTON_CLASS_DEFAULT, 'Default'),
(BUTTON_CLASS_PRIMARY, 'Primary (blue)'),
(BUTTON_CLASS_SUCCESS, 'Success (green)'),
(BUTTON_CLASS_INFO, 'Info (aqua)'),
(BUTTON_CLASS_WARNING, 'Warning (orange)'),
(BUTTON_CLASS_DANGER, 'Danger (red)'),
(BUTTON_CLASS_LINK, 'None (link)'),
)
# Graph types
GRAPH_TYPE_INTERFACE = 100
GRAPH_TYPE_PROVIDER = 200

View File

@ -0,0 +1,32 @@
# Generated by Django 2.2 on 2019-04-15 19:28
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('extras', '0021_add_color_comments_changelog_to_tag'),
]
operations = [
migrations.CreateModel(
name='CustomLink',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)),
('name', models.CharField(max_length=100, unique=True)),
('text', models.CharField(max_length=200)),
('url', models.CharField(max_length=200)),
('weight', models.PositiveSmallIntegerField(default=100)),
('group_name', models.CharField(blank=True, max_length=50)),
('button_class', models.CharField(default='default', max_length=30)),
('new_window', models.BooleanField()),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
],
options={
'ordering': ['group_name', 'weight', 'name'],
},
),
]

View File

@ -300,6 +300,65 @@ class CustomFieldChoice(models.Model):
CustomFieldValue.objects.filter(field__type=CF_TYPE_SELECT, serialized_value=str(pk)).delete()
#
# Custom links
#
def get_custom_link_models():
# TODO: This should match on the app_label as well as the model name to avoid potential duplicate names
return {
'model__in': [model.split('.')[1] for model in CUSTOM_LINK_MODELS],
}
class CustomLink(models.Model):
"""
A custom link to an external representation of a NetBox object. The link text and URL fields accept Jinja2 template
code to be rendered with an object as context.
"""
content_type = models.ForeignKey(
to=ContentType,
on_delete=models.CASCADE,
limit_choices_to=get_custom_link_models
)
name = models.CharField(
max_length=100,
unique=True
)
text = models.CharField(
max_length=200,
help_text="Jinja2 template code for link text"
)
url = models.CharField(
max_length=200,
verbose_name='URL',
help_text="Jinja2 template code for link URL"
)
weight = models.PositiveSmallIntegerField(
default=100
)
group_name = models.CharField(
max_length=50,
blank=True,
help_text="Links with the same group will appear as a dropdown menu"
)
button_class = models.CharField(
max_length=30,
choices=BUTTON_CLASS_CHOICES,
default=BUTTON_CLASS_DEFAULT,
help_text="The class of the first link in a group will be used for the dropdown button"
)
new_window = models.BooleanField(
help_text="Force link to open in a new window"
)
class Meta:
ordering = ['group_name', 'weight', 'name']
def __str__(self):
return self.name
#
# Graphs
#

View File

View File

@ -0,0 +1,68 @@
from collections import OrderedDict
from django import template
from django.contrib.contenttypes.models import ContentType
from django.utils.safestring import mark_safe
from jinja2 import Environment
from extras.models import CustomLink
register = template.Library()
LINK_BUTTON = '<a href="{}"{} class="btn btn-sm btn-{}">{}</a>\n'
GROUP_BUTTON = '<div class="btn-group">\n' \
'<button type="button" class="btn btn-sm btn-{} dropdown-toggle" data-toggle="dropdown">\n' \
'{} <span class="caret"></span>\n' \
'</button>\n' \
'<ul class="dropdown-menu pull-right">\n'
GROUP_LINK = '<li><a href="{}"{}>{}</a></li>\n'
@register.simple_tag()
def custom_links(obj):
"""
Render all applicable links for the given object.
"""
content_type = ContentType.objects.get_for_model(obj)
custom_links = CustomLink.objects.filter(content_type=content_type)
if not custom_links:
return ''
context = {
'obj': obj,
}
template_code = ''
group_names = OrderedDict()
# Organize custom links by group
for cl in custom_links:
if cl.group_name and cl.group_name in group_names:
group_names[cl.group_name].append(cl)
elif cl.group_name:
group_names[cl.group_name] = [cl]
# Add non-grouped links
for cl in custom_links:
if not cl.group_name:
link_target = ' target="_blank"' if cl.new_window else ''
template_code += LINK_BUTTON.format(
cl.url, link_target, cl.button_class, cl.text
)
# Add grouped links to template
for group, links in group_names.items():
template_code += GROUP_BUTTON.format(
links[0].button_class, group
)
for cl in links:
link_target = ' target="_blank"' if cl.new_window else ''
template_code += GROUP_LINK.format(
cl.url, link_target, cl.text
)
template_code += '</ul>\n</div>\n'
# Render template
rendered = Environment().from_string(source=template_code).render(**context)
return mark_safe(rendered)

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block title %}{{ circuit }}{% endblock %}
@ -41,6 +42,9 @@
</div>
<h1>{{ circuit }}</h1>
{% include 'inc/created_updated.html' with obj=circuit %}
<div class="pull-right noprint">
{% custom_links circuit %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ circuit.get_absolute_url }}">Circuit</a>

View File

@ -1,5 +1,6 @@
{% extends '_base.html' %}
{% load static %}
{% load custom_links %}
{% load helpers %}
{% block title %}{{ provider }}{% endblock %}
@ -47,6 +48,9 @@
</div>
<h1>{{ provider }}</h1>
{% include 'inc/created_updated.html' with obj=provider %}
<div class="pull-right noprint">
{% custom_links provider %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ provider.get_absolute_url }}">Provider</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -23,6 +24,10 @@
{% endif %}
</div>
<h1>{% block title %}Cable {{ cable }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=cable %}
<div class="pull-right noprint">
{% custom_links cable %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ cable.get_absolute_url }}">Cable</a>

View File

@ -1,6 +1,7 @@
{% extends '_base.html' %}
{% load static %}
{% load helpers %}
{% load custom_links %}
{% block title %}{{ device }}{% endblock %}
@ -64,6 +65,9 @@
</div>
<h1>{{ device }}</h1>
{% include 'inc/created_updated.html' with obj=device %}
<div class="pull-right noprint">
{% custom_links device %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{% url 'dcim:device' pk=device.pk %}">Device</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block title %}{{ devicetype.manufacturer }} {{ devicetype.model }}{% endblock %}
@ -46,6 +47,9 @@
{% endif %}
<h1>{{ devicetype.manufacturer }} {{ devicetype.model }}</h1>
{% include 'inc/created_updated.html' with obj=devicetype %}
<div class="pull-right noprint">
{% custom_links devicetype %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ devicetype.get_absolute_url }}">Device Type</a>

View File

@ -1,6 +1,6 @@
{% extends '_base.html' %}
{% load static %}
{% load tz %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -45,6 +45,9 @@
</div>
<h1>{% block title %}{{ powerfeed }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=powerfeed %}
<div class="pull-right noprint">
{% custom_links powerfeed %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ powerfeed.get_absolute_url }}">Cable</a>

View File

@ -1,6 +1,6 @@
{% extends '_base.html' %}
{% load static %}
{% load tz %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -44,6 +44,9 @@
</div>
<h1>{% block title %}{{ powerpanel }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=powerpanel %}
<div class="pull-right noprint">
{% custom_links powerpanel %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ powerpanel.get_absolute_url }}">Cable</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -43,6 +44,9 @@
</div>
<h1>{% block title %}Rack {{ rack }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=rack %}
<div class="pull-right noprint">
{% custom_links rack %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ rack.get_absolute_url }}">Rack</a>

View File

@ -1,6 +1,7 @@
{% extends '_base.html' %}
{% load static %}
{% load tz %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -52,6 +53,9 @@
</div>
<h1>{% block title %}{{ site }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=site %}
<div class="pull-right noprint">
{% custom_links site %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ site.get_absolute_url }}">Site</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -39,6 +40,9 @@
</div>
<h1>{% block title %}{{ aggregate }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=aggregate %}
<div class="pull-right noprint">
{% custom_links aggregate %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ aggregate.get_absolute_url }}">Aggregate</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -41,6 +42,9 @@
</div>
<h1>{% block title %}{{ ipaddress }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=ipaddress %}
<div class="pull-right noprint">
{% custom_links ipaddress %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ ipaddress.get_absolute_url }}">IP Address</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -52,6 +53,9 @@
</div>
<h1>{% block title %}{{ prefix }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=prefix %}
<div class="pull-right noprint">
{% custom_links prefix %}
</div>
<ul class="nav nav-tabs" style="margin-bottom: 20px">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{% url 'ipam:prefix' pk=prefix.pk %}">Prefix</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block content %}
@ -33,6 +34,9 @@
{% endif %}
<h1>{% block title %}{{ service }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=service %}
<div class="pull-right noprint">
{% custom_links service %}
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -44,6 +45,9 @@
</div>
<h1>{% block title %}VLAN {{ vlan.display_name }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=vlan %}
<div class="pull-right noprint">
{% custom_links vlan %}
</div>
<ul class="nav nav-tabs" style="margin-bottom: 20px">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{% url 'ipam:vlan' pk=vlan.pk %}">VLAN</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -38,6 +39,9 @@
</div>
<h1>{% block title %}VRF {{ vrf }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=vrf %}
<div class="pull-right noprint">
{% custom_links vrf %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ vrf.get_absolute_url }}">VRF</a>

View File

@ -1,5 +1,6 @@
{% extends '_base.html' %}
{% load static %}
{% load custom_links %}
{% load helpers %}
{% load secret_helpers %}
@ -29,6 +30,9 @@
</div>
<h1>{% block title %}{{ secret }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=secret %}
<div class="pull-right noprint">
{% custom_links secret %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ secret.get_absolute_url }}">Secret</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -41,6 +42,9 @@
</div>
<h1>{% block title %}{{ tenant }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=tenant %}
<div class="pull-right noprint">
{% custom_links tenant %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ tenant.get_absolute_url }}">Tenant</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -41,6 +42,9 @@
</div>
<h1>{% block title %}{{ cluster }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=cluster %}
<div class="pull-right noprint">
{% custom_links cluster %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ cluster.get_absolute_url }}">Cluster</a>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load custom_links %}
{% load helpers %}
{% block header %}
@ -40,6 +41,9 @@
</div>
<h1>{% block title %}{{ virtualmachine }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=virtualmachine %}
<div class="pull-right noprint">
{% custom_links virtualmachine %}
</div>
<ul class="nav nav-tabs">
<li role="presentation"{% if not active_tab %} class="active"{% endif %}>
<a href="{{ virtualmachine.get_absolute_url }}">Virtual Machine</a>