1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Merge pull request #3765 from netbox-community/3760-template-buttons

Introduce clone, edit, and delete button templatetags
This commit is contained in:
Jeremy Stretch
2019-12-13 15:59:33 -05:00
committed by GitHub
24 changed files with 153 additions and 184 deletions

View File

@ -29,19 +29,13 @@
</div>
<div class="pull-right noprint">
{% if perms.circuits.add_circuit %}
{% clone_button 'circuits:circuit_add' circuit %}
{% clone_button circuit %}
{% endif %}
{% if perms.circuits.change_circuit %}
<a href="{% url 'circuits:circuit_edit' pk=circuit.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this circuit
</a>
{% edit_button circuit %}
{% endif %}
{% if perms.circuits.delete_circuit %}
<a href="{% url 'circuits:circuit_delete' pk=circuit.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this circuit
</a>
{% delete_button circuit %}
{% endif %}
</div>
<h1>{{ circuit }}</h1>

View File

@ -30,24 +30,17 @@
<div class="pull-right noprint">
{% if show_graphs %}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#graphs_modal" data-obj="{{ provider.name }}" data-url="{% url 'circuits-api:provider-graphs' pk=provider.pk %}" title="Show graphs">
<i class="fa fa-signal" aria-hidden="true"></i>
Graphs
<i class="fa fa-signal" aria-hidden="true"></i> Graphs
</button>
{% endif %}
{% if perms.circuits.add_provider %}
{% clone_button 'circuits:provider_add' provider %}
{% clone_button provider %}
{% endif %}
{% if perms.circuits.change_provider %}
<a href="{% url 'circuits:provider_edit' slug=provider.slug %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this provider
</a>
{% edit_button provider %}
{% endif %}
{% if perms.circuits.delete_provider %}
<a href="{% url 'circuits:provider_delete' slug=provider.slug %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this provider
</a>
{% delete_button provider %}
{% endif %}
</div>
<h1>{{ provider }}</h1>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load buttons %}
{% load custom_links %}
{% load helpers %}
@ -13,14 +14,10 @@
</div>
<div class="pull-right noprint">
{% if perms.dcim.change_cable %}
<a href="{% url 'dcim:cable_edit' pk=cable.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span> Edit this cable
</a>
{% edit_button cable %}
{% endif %}
{% if perms.dcim.delete_cable %}
<a href="{% url 'dcim:cable_delete' pk=cable.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span> Delete this cable
</a>
{% delete_button cable %}
{% endif %}
</div>
<h1>{% block title %}Cable {{ cable }}{% endblock %}</h1>

View File

@ -60,19 +60,13 @@
</div>
{% endif %}
{% if perms.dcim.add_device %}
{% clone_button 'dcim:device_add' device %}
{% clone_button device %}
{% endif %}
{% if perms.dcim.change_device %}
<a href="{% url 'dcim:device_edit' pk=device.pk %}" class="btn btn-warning">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
Edit this device
</a>
{% edit_button device %}
{% endif %}
{% if perms.dcim.delete_device %}
<a href="{% url 'dcim:device_delete' pk=device.pk %}" class="btn btn-danger">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
Delete this device
</a>
{% delete_button device %}
{% endif %}
</div>
<h1>{{ device }}</h1>

View File

@ -34,19 +34,13 @@
</div>
{% endif %}
{% if perms.dcim.add_devicetype %}
{% clone_button 'dcim:devicetype_add' devicetype %}
{% clone_button devicetype %}
{% endif %}
{% if perms.dcim.change_devicetype %}
<a href="{% url 'dcim:devicetype_edit' pk=devicetype.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this device type
</a>
{% edit_button devicetype use_pk=True %}
{% endif %}
{% if perms.dcim.delete_devicetype %}
<a href="{% url 'dcim:devicetype_delete' pk=devicetype.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this device type
</a>
{% delete_button devicetype use_pk=True %}
{% endif %}
</div>
<h1>{{ devicetype.manufacturer }} {{ devicetype.model }}</h1>

View File

@ -18,12 +18,12 @@
<div class="pull-right noprint">
{% if perms.dcim.change_interface %}
<a href="{% if interface.device %}{% url 'dcim:interface_edit' pk=interface.pk %}{% else %}{% url 'virtualization:interface_edit' pk=interface.pk %}{% endif %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span> Edit this interface
<span class="fa fa-pencil" aria-hidden="true"></span> Edit
</a>
{% endif %}
{% if perms.dcim.delete_interface %}
<a href="{% if interface.device %}{% url 'dcim:interface_delete' pk=interface.pk %}{% else %}{% url 'virtualization:interface_delete' pk=interface.pk %}{% endif %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span> Delete this interface
<span class="fa fa-trash" aria-hidden="true"></span> Delete
</a>
{% endif %}
</div>

View File

@ -32,19 +32,13 @@
</div>
<div class="pull-right noprint">
{% if perms.dcim.add_powerfeed %}
{% clone_button 'dcim:powerfeed_add' powerfeed %}
{% clone_button powerfeed %}
{% endif %}
{% if perms.dcim.change_powerfeed %}
<a href="{% url 'dcim:powerfeed_edit' pk=powerfeed.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this power feed
</a>
{% edit_button powerfeed %}
{% endif %}
{% if perms.dcim.delete_powerfeed %}
<a href="{% url 'dcim:powerfeed_delete' pk=powerfeed.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this power feed
</a>
{% delete_button powerfeed %}
{% endif %}
</div>
<h1>{% block title %}{{ powerfeed }}{% endblock %}</h1>

View File

@ -1,7 +1,8 @@
{% extends '_base.html' %}
{% load static %}
{% load buttons %}
{% load custom_links %}
{% load helpers %}
{% load static %}
{% block header %}
<div class="row noprint">
@ -30,16 +31,10 @@
</div>
<div class="pull-right noprint">
{% if perms.dcim.change_powerpanel %}
<a href="{% url 'dcim:powerpanel_edit' pk=powerpanel.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this power panel
</a>
{% edit_button powerpanel %}
{% endif %}
{% if perms.dcim.delete_powerpanel %}
<a href="{% url 'dcim:powerpanel_delete' pk=powerpanel.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this power panel
</a>
{% delete_button powerpanel %}
{% endif %}
</div>
<h1>{% block title %}{{ powerpanel }}{% endblock %}</h1>

View File

@ -33,17 +33,13 @@
<span class="fa fa-chevron-right" aria-hidden="true"></span> Next Rack
</a>
{% if perms.dcim.add_rack %}
{% clone_button 'dcim:rack_add' rack %}
{% clone_button rack %}
{% endif %}
{% if perms.dcim.change_rack %}
<a href="{% url 'dcim:rack_edit' pk=rack.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span> Edit this rack
</a>
{% edit_button rack %}
{% endif %}
{% if perms.dcim.delete_rack %}
<a href="{% url 'dcim:rack_delete' pk=rack.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span> Delete this rack
</a>
{% delete_button rack %}
{% endif %}
</div>
<h1>{% block title %}Rack {{ rack }}{% endblock %}</h1>

View File

@ -40,19 +40,13 @@
</button>
{% endif %}
{% if perms.dcim.add_site %}
{% clone_button 'dcim:site_add' site %}
{% clone_button site %}
{% endif %}
{% if perms.dcim.change_site %}
<a href="{% url 'dcim:site_edit' slug=site.slug %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this site
</a>
{% edit_button site %}
{% endif %}
{% if perms.dcim.delete_site %}
<a href="{% url 'dcim:site_delete' slug=site.slug %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this site
</a>
{% delete_button site %}
{% endif %}
</div>
<h1>{% block title %}{{ site }}{% endblock %}</h1>

View File

@ -27,19 +27,13 @@
</div>
<div class="pull-right noprint">
{% if perms.ipam.add_aggregate %}
{% clone_button 'ipam:aggregate_add' aggregate %}
{% clone_button aggregate %}
{% endif %}
{% if perms.ipam.change_aggregate %}
<a href="{% url 'ipam:aggregate_edit' pk=aggregate.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this aggregate
</a>
{% edit_button aggregate %}
{% endif %}
{% if perms.ipam.delete_aggregate %}
<a href="{% url 'ipam:aggregate_delete' pk=aggregate.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this aggregate
</a>
{% delete_button aggregate %}
{% endif %}
</div>
<h1>{% block title %}{{ aggregate }}{% endblock %}</h1>

View File

@ -29,19 +29,13 @@
</div>
<div class="pull-right noprint">
{% if perms.ipam.add_ipaddress %}
{% clone_button 'ipam:ipaddress_add' ipaddress %}
{% clone_button ipaddress %}
{% endif %}
{% if perms.ipam.change_ipaddress %}
<a href="{% url 'ipam:ipaddress_edit' pk=ipaddress.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this IP
</a>
{%edit_button ipaddress %}
{% endif %}
{% if perms.ipam.delete_ipaddress %}
<a href="{% url 'ipam:ipaddress_delete' pk=ipaddress.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this IP
</a>
{% delete_button ipaddress %}
{% endif %}
</div>
<h1>{% block title %}{{ ipaddress }}{% endblock %}</h1>

View File

@ -40,19 +40,13 @@
</a>
{% endif %}
{% if perms.ipam.add_prefix %}
{% clone_button 'ipam:prefix_add' prefix %}
{% clone_button prefix %}
{% endif %}
{% if perms.ipam.change_prefix %}
<a href="{% url 'ipam:prefix_edit' pk=prefix.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this prefix
</a>
{% edit_button prefix %}
{% endif %}
{% if perms.ipam.delete_prefix %}
<a href="{% url 'ipam:prefix_delete' pk=prefix.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this prefix
</a>
{% delete_button prefix %}
{% endif %}
</div>
<h1>{% block title %}{{ prefix }}{% endblock %}</h1>

View File

@ -1,4 +1,5 @@
{% extends '_base.html' %}
{% load buttons %}
{% load custom_links %}
{% load helpers %}
@ -24,14 +25,14 @@
</form>
</div>
</div>
{% if perms.dcim.change_service %}
<div class="pull-right">
<a href="{% url 'ipam:service_edit' pk=service.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this service
</a>
</div>
{% endif %}
<div class="pull-right">
{% if perms.dcim.change_service %}
{% edit_button service %}
{% endif %}
{% if perms.dcim.delete_service %}
{% delete_button service %}
{% endif %}
</div>
<h1>{% block title %}{{ service }}{% endblock %}</h1>
{% include 'inc/created_updated.html' with obj=service %}
<div class="pull-right noprint">

View File

@ -32,19 +32,13 @@
</div>
<div class="pull-right noprint">
{% if perms.ipam.add_vlan %}
{% clone_button 'ipam:vlan_add' vlan %}
{% clone_button vlan %}
{% endif %}
{% if perms.ipam.change_vlan %}
<a href="{% url 'ipam:vlan_edit' pk=vlan.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this VLAN
</a>
{% edit_button vlan %}
{% endif %}
{% if perms.ipam.delete_vlan %}
<a href="{% url 'ipam:vlan_delete' pk=vlan.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this VLAN
</a>
{% delete_button vlan %}
{% endif %}
</div>
<h1>{% block title %}VLAN {{ vlan.display_name }}{% endblock %}</h1>

View File

@ -26,19 +26,13 @@
</div>
<div class="pull-right noprint">
{% if perms.ipam.add_vrf %}
{% clone_button 'ipam:vrf_add' vrf %}
{% clone_button vrf %}
{% endif %}
{% if perms.ipam.change_vrf %}
<a href="{% url 'ipam:vrf_edit' pk=vrf.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this VRF
</a>
{% edit_button vrf %}
{% endif %}
{% if perms.ipam.delete_vrf %}
<a href="{% url 'ipam:vrf_delete' pk=vrf.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this VRF
</a>
{% delete_button vrf %}
{% endif %}
</div>
<h1>{% block title %}VRF {{ vrf }}{% endblock %}</h1>

View File

@ -1,8 +1,9 @@
{% extends '_base.html' %}
{% load static %}
{% load buttons %}
{% load custom_links %}
{% load helpers %}
{% load secret_helpers %}
{% load static %}
{% block header %}
<div class="row noprint">
@ -16,16 +17,10 @@
</div>
<div class="pull-right noprint">
{% if perms.secrets.change_secret %}
<a href="{% url 'secrets:secret_edit' pk=secret.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this secret
</a>
{% edit_button secret %}
{% endif %}
{% if perms.secrets.delete_secret %}
<a href="{% url 'secrets:secret_delete' pk=secret.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this secret
</a>
{% delete_button secret %}
{% endif %}
</div>
<h1>{% block title %}{{ secret }}{% endblock %}</h1>

View File

@ -29,19 +29,13 @@
</div>
<div class="pull-right noprint">
{% if perms.tenancy.add_tenant %}
{% clone_button 'tenancy:tenant_add' tenant %}
{% clone_button tenant %}
{% endif %}
{% if perms.tenancy.change_tenant %}
<a href="{% url 'tenancy:tenant_edit' slug=tenant.slug %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this tenant
</a>
{% edit_button tenant %}
{% endif %}
{% if perms.tenancy.delete_tenant %}
<a href="{% url 'tenancy:tenant_delete' slug=tenant.slug %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this tenant
</a>
{% delete_button tenant %}
{% endif %}
</div>
<h1>{% block title %}{{ tenant }}{% endblock %}</h1>

View File

@ -29,19 +29,13 @@
</div>
<div class="pull-right noprint">
{% if perms.virtualization.add_cluster %}
{% clone_button 'virtualization:cluster_add' cluster %}
{% clone_button cluster %}
{% endif %}
{% if perms.virtualization.change_cluster %}
<a href="{% url 'virtualization:cluster_edit' pk=cluster.pk %}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span>
Edit this cluster
</a>
{% edit_button cluster %}
{% endif %}
{% if perms.virtualization.delete_cluster %}
<a href="{% url 'virtualization:cluster_delete' pk=cluster.pk %}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span>
Delete this cluster
</a>
{% delete_button cluster %}
{% endif %}
</div>
<h1>{% block title %}{{ cluster }}{% endblock %}</h1>

View File

@ -28,19 +28,13 @@
</div>
<div class="pull-right noprint">
{% if perms.virtualization.add_virtualmachine %}
{% clone_button 'virtualization:virtualmachine_add' virtualmachine %}
{% clone_button virtualmachine %}
{% endif %}
{% if perms.virtualization.change_virtualmachine %}
<a href="{% url 'virtualization:virtualmachine_edit' pk=virtualmachine.pk %}" class="btn btn-warning">
<span class="fa fa-pencil"></span>
Edit this VM
</a>
{% edit_button virtualmachine %}
{% endif %}
{% if perms.virtualization.delete_virtualmachine %}
<a href="{% url 'virtualization:virtualmachine_delete' pk=virtualmachine.pk %}" class="btn btn-danger">
<span class="fa fa-trash"></span>
Delete this VM
</a>
{% delete_button virtualmachine %}
{% endif %}
</div>
<h1>{% block title %}{{ virtualmachine }}{% endblock %}</h1>

View File

@ -1,3 +1,3 @@
<a href="{% url add_url %}" class="btn btn-primary">
<a href="{{ add_url }}" class="btn btn-primary">
<span class="fa fa-plus" aria-hidden="true"></span> Add
</a>

View File

@ -0,0 +1,3 @@
<a href="{{ url }}" class="btn btn-danger">
<span class="fa fa-trash" aria-hidden="true"></span> Delete
</a>

View File

@ -0,0 +1,3 @@
<a href="{{ url }}" class="btn btn-warning">
<span class="fa fa-pencil" aria-hidden="true"></span> Edit
</a>

View File

@ -7,8 +7,80 @@ from utilities.utils import prepare_cloned_fields
register = template.Library()
def _get_viewname(instance, action):
"""
Return the appropriate viewname for adding, editing, or deleting an instance.
"""
# Validate action
assert action in ('add', 'edit', 'delete')
viewname = "{}:{}_{}".format(
instance._meta.app_label, instance._meta.model_name, action
)
return viewname
#
# Instance buttons
#
@register.inclusion_tag('buttons/clone.html')
def clone_button(instance):
viewname = _get_viewname(instance, 'add')
# Populate cloned field values
param_string = prepare_cloned_fields(instance)
if param_string:
url = '{}?{}'.format(reverse(viewname), param_string)
return {
'url': url,
}
@register.inclusion_tag('buttons/edit.html')
def edit_button(instance, use_pk=False):
viewname = _get_viewname(instance, 'edit')
# Assign kwargs
if hasattr(instance, 'slug') and not use_pk:
kwargs = {'slug': instance.slug}
else:
kwargs = {'pk': instance.pk}
url = reverse(viewname, kwargs=kwargs)
return {
'url': url,
}
@register.inclusion_tag('buttons/delete.html')
def delete_button(instance, use_pk=False):
viewname = _get_viewname(instance, 'delete')
# Assign kwargs
if hasattr(instance, 'slug') and not use_pk:
kwargs = {'slug': instance.slug}
else:
kwargs = {'pk': instance.pk}
url = reverse(viewname, kwargs=kwargs)
return {
'url': url,
}
#
# List buttons
#
@register.inclusion_tag('buttons/add.html')
def add_button(url):
url = reverse(url)
return {
'add_url': url,
}
@ -16,27 +88,19 @@ def add_button(url):
@register.inclusion_tag('buttons/import.html')
def import_button(url):
return {
'import_url': url,
}
@register.inclusion_tag('buttons/clone.html')
def clone_button(url, instance):
url = reverse(url)
param_string = prepare_cloned_fields(instance)
if param_string:
url = '{}?{}'.format(url, param_string)
return {
'url': url,
}
@register.inclusion_tag('buttons/export.html', takes_context=True)
def export_button(context, content_type=None):
export_templates = ExportTemplate.objects.filter(content_type=content_type)
if content_type is not None:
export_templates = ExportTemplate.objects.filter(content_type=content_type)
else:
export_templates = []
return {
'url_params': context['request'].GET,
'export_templates': export_templates,