mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-19 03:42:25 -06:00
Introduced clone, edit, and delete buttons
This commit is contained in:
@@ -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>
|
||||
|
||||
3
netbox/utilities/templates/buttons/delete.html
Normal file
3
netbox/utilities/templates/buttons/delete.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<a href="{{ url }}" class="btn btn-danger">
|
||||
<span class="fa fa-trash" aria-hidden="true"></span> Delete
|
||||
</a>
|
||||
3
netbox/utilities/templates/buttons/edit.html
Normal file
3
netbox/utilities/templates/buttons/edit.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<a href="{{ url }}" class="btn btn-warning">
|
||||
<span class="fa fa-pencil" aria-hidden="true"></span> Edit
|
||||
</a>
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user