mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-26 23:27:46 -06:00
Closes #33: Add ability to clone objects (pre-populate form fields)
This commit is contained in:
3
netbox/utilities/templates/buttons/clone.html
Normal file
3
netbox/utilities/templates/buttons/clone.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<a href="{{ url }}" class="btn btn-success">
|
||||
<span class="fa fa-plus" aria-hidden="true"></span> Clone
|
||||
</a>
|
||||
@@ -1,4 +1,5 @@
|
||||
from django import template
|
||||
from django.urls import reverse
|
||||
|
||||
from extras.models import ExportTemplate
|
||||
|
||||
@@ -7,12 +8,47 @@ register = template.Library()
|
||||
|
||||
@register.inclusion_tag('buttons/add.html')
|
||||
def add_button(url):
|
||||
return {'add_url': url}
|
||||
return {
|
||||
'add_url': url,
|
||||
}
|
||||
|
||||
|
||||
@register.inclusion_tag('buttons/import.html')
|
||||
def import_button(url):
|
||||
return {'import_url': url}
|
||||
return {
|
||||
'import_url': url,
|
||||
}
|
||||
|
||||
|
||||
@register.inclusion_tag('buttons/clone.html')
|
||||
def clone_button(url, instance):
|
||||
|
||||
url = reverse(url)
|
||||
|
||||
# Populate form field values
|
||||
params = {}
|
||||
for field_name in getattr(instance, 'clone_fields', []):
|
||||
field = instance._meta.get_field(field_name)
|
||||
field_value = field.value_from_object(instance)
|
||||
|
||||
# Swap out False with URL-friendly value
|
||||
if field_value is False:
|
||||
field_value = ''
|
||||
|
||||
# Omit empty values
|
||||
if field_value not in (None, ''):
|
||||
params[field_name] = field_value
|
||||
|
||||
# TODO: Tag replication
|
||||
|
||||
# Append parameters to URL
|
||||
param_string = '&'.join(['{}={}'.format(k, v) for k, v in params.items()])
|
||||
if param_string:
|
||||
url = '{}?{}'.format(url, param_string)
|
||||
|
||||
return {
|
||||
'url': url,
|
||||
}
|
||||
|
||||
|
||||
@register.inclusion_tag('buttons/export.html', takes_context=True)
|
||||
|
||||
Reference in New Issue
Block a user