From 8acd3d0a72d6de34d77806053785cc535a94e7e0 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 13 Dec 2019 15:29:55 -0500 Subject: [PATCH] Introduced clone, edit, and delete buttons --- netbox/templates/circuits/circuit.html | 12 +-- netbox/templates/circuits/provider.html | 15 +-- netbox/templates/dcim/cable.html | 9 +- netbox/templates/dcim/device.html | 12 +-- netbox/templates/dcim/devicetype.html | 12 +-- netbox/templates/dcim/interface.html | 4 +- netbox/templates/dcim/powerfeed.html | 12 +-- netbox/templates/dcim/powerpanel.html | 13 +-- netbox/templates/dcim/rack.html | 10 +- netbox/templates/dcim/site.html | 12 +-- netbox/templates/ipam/aggregate.html | 12 +-- netbox/templates/ipam/ipaddress.html | 12 +-- netbox/templates/ipam/prefix.html | 12 +-- netbox/templates/ipam/service.html | 17 ++-- netbox/templates/ipam/vlan.html | 12 +-- netbox/templates/ipam/vrf.html | 12 +-- netbox/templates/secrets/secret.html | 13 +-- netbox/templates/tenancy/tenant.html | 12 +-- netbox/templates/virtualization/cluster.html | 12 +-- .../virtualization/virtualmachine.html | 12 +-- netbox/utilities/templates/buttons/add.html | 2 +- .../utilities/templates/buttons/delete.html | 3 + netbox/utilities/templates/buttons/edit.html | 3 + netbox/utilities/templatetags/buttons.py | 92 ++++++++++++++++--- 24 files changed, 153 insertions(+), 184 deletions(-) create mode 100644 netbox/utilities/templates/buttons/delete.html create mode 100644 netbox/utilities/templates/buttons/edit.html diff --git a/netbox/templates/circuits/circuit.html b/netbox/templates/circuits/circuit.html index 41a195bed..712ee5861 100644 --- a/netbox/templates/circuits/circuit.html +++ b/netbox/templates/circuits/circuit.html @@ -29,19 +29,13 @@
{% if perms.circuits.add_circuit %} - {% clone_button 'circuits:circuit_add' circuit %} + {% clone_button circuit %} {% endif %} {% if perms.circuits.change_circuit %} - - - Edit this circuit - + {% edit_button circuit %} {% endif %} {% if perms.circuits.delete_circuit %} - - - Delete this circuit - + {% delete_button circuit %} {% endif %}

{{ circuit }}

diff --git a/netbox/templates/circuits/provider.html b/netbox/templates/circuits/provider.html index 8a72f6ba5..b4cedd6d3 100644 --- a/netbox/templates/circuits/provider.html +++ b/netbox/templates/circuits/provider.html @@ -30,24 +30,17 @@
{% if show_graphs %} {% endif %} {% if perms.circuits.add_provider %} - {% clone_button 'circuits:provider_add' provider %} + {% clone_button provider %} {% endif %} {% if perms.circuits.change_provider %} - - - Edit this provider - + {% edit_button provider %} {% endif %} {% if perms.circuits.delete_provider %} - - - Delete this provider - + {% delete_button provider %} {% endif %}

{{ provider }}

diff --git a/netbox/templates/dcim/cable.html b/netbox/templates/dcim/cable.html index 54e575009..a190a7001 100644 --- a/netbox/templates/dcim/cable.html +++ b/netbox/templates/dcim/cable.html @@ -1,4 +1,5 @@ {% extends '_base.html' %} +{% load buttons %} {% load custom_links %} {% load helpers %} @@ -13,14 +14,10 @@
{% if perms.dcim.change_cable %} - - Edit this cable - + {% edit_button cable %} {% endif %} {% if perms.dcim.delete_cable %} - - Delete this cable - + {% delete_button cable %} {% endif %}

{% block title %}Cable {{ cable }}{% endblock %}

diff --git a/netbox/templates/dcim/device.html b/netbox/templates/dcim/device.html index 93be6ebca..1afd2b41f 100644 --- a/netbox/templates/dcim/device.html +++ b/netbox/templates/dcim/device.html @@ -60,19 +60,13 @@ {% endif %} {% if perms.dcim.add_device %} - {% clone_button 'dcim:device_add' device %} + {% clone_button device %} {% endif %} {% if perms.dcim.change_device %} - - - Edit this device - + {% edit_button device %} {% endif %} {% if perms.dcim.delete_device %} - - - Delete this device - + {% delete_button device %} {% endif %}

{{ device }}

diff --git a/netbox/templates/dcim/devicetype.html b/netbox/templates/dcim/devicetype.html index b7d5a013c..14a2d5690 100644 --- a/netbox/templates/dcim/devicetype.html +++ b/netbox/templates/dcim/devicetype.html @@ -34,19 +34,13 @@ {% endif %} {% if perms.dcim.add_devicetype %} - {% clone_button 'dcim:devicetype_add' devicetype %} + {% clone_button devicetype %} {% endif %} {% if perms.dcim.change_devicetype %} - - - Edit this device type - + {% edit_button devicetype use_pk=True %} {% endif %} {% if perms.dcim.delete_devicetype %} - - - Delete this device type - + {% delete_button devicetype use_pk=True %} {% endif %}

{{ devicetype.manufacturer }} {{ devicetype.model }}

diff --git a/netbox/templates/dcim/interface.html b/netbox/templates/dcim/interface.html index 4e7cc6306..6027afb58 100644 --- a/netbox/templates/dcim/interface.html +++ b/netbox/templates/dcim/interface.html @@ -18,12 +18,12 @@
{% if perms.dcim.change_interface %} - Edit this interface + Edit {% endif %} {% if perms.dcim.delete_interface %} - Delete this interface + Delete {% endif %}
diff --git a/netbox/templates/dcim/powerfeed.html b/netbox/templates/dcim/powerfeed.html index e192b837e..217de4a19 100644 --- a/netbox/templates/dcim/powerfeed.html +++ b/netbox/templates/dcim/powerfeed.html @@ -32,19 +32,13 @@
{% if perms.dcim.add_powerfeed %} - {% clone_button 'dcim:powerfeed_add' powerfeed %} + {% clone_button powerfeed %} {% endif %} {% if perms.dcim.change_powerfeed %} - - - Edit this power feed - + {% edit_button powerfeed %} {% endif %} {% if perms.dcim.delete_powerfeed %} - - - Delete this power feed - + {% delete_button powerfeed %} {% endif %}

{% block title %}{{ powerfeed }}{% endblock %}

diff --git a/netbox/templates/dcim/powerpanel.html b/netbox/templates/dcim/powerpanel.html index b92eddfc7..b0a6961f6 100644 --- a/netbox/templates/dcim/powerpanel.html +++ b/netbox/templates/dcim/powerpanel.html @@ -1,7 +1,8 @@ {% extends '_base.html' %} -{% load static %} +{% load buttons %} {% load custom_links %} {% load helpers %} +{% load static %} {% block header %}
@@ -30,16 +31,10 @@
{% if perms.dcim.change_powerpanel %} - - - Edit this power panel - + {% edit_button powerpanel %} {% endif %} {% if perms.dcim.delete_powerpanel %} - - - Delete this power panel - + {% delete_button powerpanel %} {% endif %}

{% block title %}{{ powerpanel }}{% endblock %}

diff --git a/netbox/templates/dcim/rack.html b/netbox/templates/dcim/rack.html index 0cc261a27..51faeef50 100644 --- a/netbox/templates/dcim/rack.html +++ b/netbox/templates/dcim/rack.html @@ -33,17 +33,13 @@ Next Rack {% if perms.dcim.add_rack %} - {% clone_button 'dcim:rack_add' rack %} + {% clone_button rack %} {% endif %} {% if perms.dcim.change_rack %} - - Edit this rack - + {% edit_button rack %} {% endif %} {% if perms.dcim.delete_rack %} - - Delete this rack - + {% delete_button rack %} {% endif %}

{% block title %}Rack {{ rack }}{% endblock %}

diff --git a/netbox/templates/dcim/site.html b/netbox/templates/dcim/site.html index 8c7cc1915..7773bc881 100644 --- a/netbox/templates/dcim/site.html +++ b/netbox/templates/dcim/site.html @@ -40,19 +40,13 @@ {% endif %} {% if perms.dcim.add_site %} - {% clone_button 'dcim:site_add' site %} + {% clone_button site %} {% endif %} {% if perms.dcim.change_site %} - - - Edit this site - + {% edit_button site %} {% endif %} {% if perms.dcim.delete_site %} - - - Delete this site - + {% delete_button site %} {% endif %}

{% block title %}{{ site }}{% endblock %}

diff --git a/netbox/templates/ipam/aggregate.html b/netbox/templates/ipam/aggregate.html index 2661fb61c..2af8e8cc8 100644 --- a/netbox/templates/ipam/aggregate.html +++ b/netbox/templates/ipam/aggregate.html @@ -27,19 +27,13 @@
{% if perms.ipam.add_aggregate %} - {% clone_button 'ipam:aggregate_add' aggregate %} + {% clone_button aggregate %} {% endif %} {% if perms.ipam.change_aggregate %} - - - Edit this aggregate - + {% edit_button aggregate %} {% endif %} {% if perms.ipam.delete_aggregate %} - - - Delete this aggregate - + {% delete_button aggregate %} {% endif %}

{% block title %}{{ aggregate }}{% endblock %}

diff --git a/netbox/templates/ipam/ipaddress.html b/netbox/templates/ipam/ipaddress.html index ad334fecc..1bb3265ff 100644 --- a/netbox/templates/ipam/ipaddress.html +++ b/netbox/templates/ipam/ipaddress.html @@ -29,19 +29,13 @@
{% if perms.ipam.add_ipaddress %} - {% clone_button 'ipam:ipaddress_add' ipaddress %} + {% clone_button ipaddress %} {% endif %} {% if perms.ipam.change_ipaddress %} - - - Edit this IP - + {%edit_button ipaddress %} {% endif %} {% if perms.ipam.delete_ipaddress %} - - - Delete this IP - + {% delete_button ipaddress %} {% endif %}

{% block title %}{{ ipaddress }}{% endblock %}

diff --git a/netbox/templates/ipam/prefix.html b/netbox/templates/ipam/prefix.html index a74bc595e..023090ef5 100644 --- a/netbox/templates/ipam/prefix.html +++ b/netbox/templates/ipam/prefix.html @@ -40,19 +40,13 @@ {% endif %} {% if perms.ipam.add_prefix %} - {% clone_button 'ipam:prefix_add' prefix %} + {% clone_button prefix %} {% endif %} {% if perms.ipam.change_prefix %} - - - Edit this prefix - + {% edit_button prefix %} {% endif %} {% if perms.ipam.delete_prefix %} - - - Delete this prefix - + {% delete_button prefix %} {% endif %}

{% block title %}{{ prefix }}{% endblock %}

diff --git a/netbox/templates/ipam/service.html b/netbox/templates/ipam/service.html index f39bace84..b845aca17 100644 --- a/netbox/templates/ipam/service.html +++ b/netbox/templates/ipam/service.html @@ -1,4 +1,5 @@ {% extends '_base.html' %} +{% load buttons %} {% load custom_links %} {% load helpers %} @@ -24,14 +25,14 @@ -{% if perms.dcim.change_service %} -
- - - Edit this service - -
-{% endif %} +
+ {% if perms.dcim.change_service %} + {% edit_button service %} + {% endif %} + {% if perms.dcim.delete_service %} + {% delete_button service %} + {% endif %} +

{% block title %}{{ service }}{% endblock %}

{% include 'inc/created_updated.html' with obj=service %}
diff --git a/netbox/templates/ipam/vlan.html b/netbox/templates/ipam/vlan.html index 2f751bd5f..50b964992 100644 --- a/netbox/templates/ipam/vlan.html +++ b/netbox/templates/ipam/vlan.html @@ -32,19 +32,13 @@
{% if perms.ipam.add_vlan %} - {% clone_button 'ipam:vlan_add' vlan %} + {% clone_button vlan %} {% endif %} {% if perms.ipam.change_vlan %} - - - Edit this VLAN - + {% edit_button vlan %} {% endif %} {% if perms.ipam.delete_vlan %} - - - Delete this VLAN - + {% delete_button vlan %} {% endif %}

{% block title %}VLAN {{ vlan.display_name }}{% endblock %}

diff --git a/netbox/templates/ipam/vrf.html b/netbox/templates/ipam/vrf.html index dd52671fb..242cfde92 100644 --- a/netbox/templates/ipam/vrf.html +++ b/netbox/templates/ipam/vrf.html @@ -26,19 +26,13 @@
{% if perms.ipam.add_vrf %} - {% clone_button 'ipam:vrf_add' vrf %} + {% clone_button vrf %} {% endif %} {% if perms.ipam.change_vrf %} - - - Edit this VRF - + {% edit_button vrf %} {% endif %} {% if perms.ipam.delete_vrf %} - - - Delete this VRF - + {% delete_button vrf %} {% endif %}

{% block title %}VRF {{ vrf }}{% endblock %}

diff --git a/netbox/templates/secrets/secret.html b/netbox/templates/secrets/secret.html index a52f626d3..438a5a943 100644 --- a/netbox/templates/secrets/secret.html +++ b/netbox/templates/secrets/secret.html @@ -1,8 +1,9 @@ {% extends '_base.html' %} -{% load static %} +{% load buttons %} {% load custom_links %} {% load helpers %} {% load secret_helpers %} +{% load static %} {% block header %}
@@ -16,16 +17,10 @@
{% if perms.secrets.change_secret %} - - - Edit this secret - + {% edit_button secret %} {% endif %} {% if perms.secrets.delete_secret %} - - - Delete this secret - + {% delete_button secret %} {% endif %}

{% block title %}{{ secret }}{% endblock %}

diff --git a/netbox/templates/tenancy/tenant.html b/netbox/templates/tenancy/tenant.html index cbb09b7a3..71c063502 100644 --- a/netbox/templates/tenancy/tenant.html +++ b/netbox/templates/tenancy/tenant.html @@ -29,19 +29,13 @@
{% if perms.tenancy.add_tenant %} - {% clone_button 'tenancy:tenant_add' tenant %} + {% clone_button tenant %} {% endif %} {% if perms.tenancy.change_tenant %} - - - Edit this tenant - + {% edit_button tenant %} {% endif %} {% if perms.tenancy.delete_tenant %} - - - Delete this tenant - + {% delete_button tenant %} {% endif %}

{% block title %}{{ tenant }}{% endblock %}

diff --git a/netbox/templates/virtualization/cluster.html b/netbox/templates/virtualization/cluster.html index 0385c8923..6b90a660d 100644 --- a/netbox/templates/virtualization/cluster.html +++ b/netbox/templates/virtualization/cluster.html @@ -29,19 +29,13 @@
{% if perms.virtualization.add_cluster %} - {% clone_button 'virtualization:cluster_add' cluster %} + {% clone_button cluster %} {% endif %} {% if perms.virtualization.change_cluster %} - - - Edit this cluster - + {% edit_button cluster %} {% endif %} {% if perms.virtualization.delete_cluster %} - - - Delete this cluster - + {% delete_button cluster %} {% endif %}

{% block title %}{{ cluster }}{% endblock %}

diff --git a/netbox/templates/virtualization/virtualmachine.html b/netbox/templates/virtualization/virtualmachine.html index 8f51a0caf..c6f7e4994 100644 --- a/netbox/templates/virtualization/virtualmachine.html +++ b/netbox/templates/virtualization/virtualmachine.html @@ -28,19 +28,13 @@
{% if perms.virtualization.add_virtualmachine %} - {% clone_button 'virtualization:virtualmachine_add' virtualmachine %} + {% clone_button virtualmachine %} {% endif %} {% if perms.virtualization.change_virtualmachine %} - - - Edit this VM - + {% edit_button virtualmachine %} {% endif %} {% if perms.virtualization.delete_virtualmachine %} - - - Delete this VM - + {% delete_button virtualmachine %} {% endif %}

{% block title %}{{ virtualmachine }}{% endblock %}

diff --git a/netbox/utilities/templates/buttons/add.html b/netbox/utilities/templates/buttons/add.html index a5278ae12..9ba2626a9 100644 --- a/netbox/utilities/templates/buttons/add.html +++ b/netbox/utilities/templates/buttons/add.html @@ -1,3 +1,3 @@ - + Add diff --git a/netbox/utilities/templates/buttons/delete.html b/netbox/utilities/templates/buttons/delete.html new file mode 100644 index 000000000..6d621701f --- /dev/null +++ b/netbox/utilities/templates/buttons/delete.html @@ -0,0 +1,3 @@ + + Delete + diff --git a/netbox/utilities/templates/buttons/edit.html b/netbox/utilities/templates/buttons/edit.html new file mode 100644 index 000000000..14de5c86d --- /dev/null +++ b/netbox/utilities/templates/buttons/edit.html @@ -0,0 +1,3 @@ + + Edit + diff --git a/netbox/utilities/templatetags/buttons.py b/netbox/utilities/templatetags/buttons.py index 8e836f685..85f75f79e 100644 --- a/netbox/utilities/templatetags/buttons.py +++ b/netbox/utilities/templatetags/buttons.py @@ -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,