mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Adds config template to vm model (#13450)
* adds config template to vm model #12461 * Add translation tags; collapse config data * i18n cleanup * Establish parity with DeviceRenderConfigView * Move config_template field to RenderConfigMixin --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
committed by
GitHub
parent
8593715149
commit
752e26c7de
@@ -1,11 +1,14 @@
|
||||
import traceback
|
||||
from collections import defaultdict
|
||||
|
||||
from django.contrib import messages
|
||||
from django.db import transaction
|
||||
from django.db.models import Prefetch, Sum
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext as _
|
||||
from jinja2.exceptions import TemplateError
|
||||
|
||||
from dcim.filtersets import DeviceFilterSet
|
||||
from dcim.models import Device
|
||||
@@ -389,6 +392,55 @@ class VirtualMachineConfigContextView(ObjectConfigContextView):
|
||||
)
|
||||
|
||||
|
||||
@register_model_view(VirtualMachine, 'render-config')
|
||||
class VirtualMachineRenderConfigView(generic.ObjectView):
|
||||
queryset = VirtualMachine.objects.all()
|
||||
template_name = 'virtualization/virtualmachine/render_config.html'
|
||||
tab = ViewTab(
|
||||
label=_('Render Config'),
|
||||
permission='extras.view_configtemplate',
|
||||
weight=2100
|
||||
)
|
||||
|
||||
def get(self, request, **kwargs):
|
||||
instance = self.get_object(**kwargs)
|
||||
context = self.get_extra_context(request, instance)
|
||||
|
||||
# If a direct export has been requested, return the rendered template content as a
|
||||
# downloadable file.
|
||||
if request.GET.get('export'):
|
||||
response = HttpResponse(context['rendered_config'], content_type='text')
|
||||
filename = f"{instance.name or 'config'}.txt"
|
||||
response['Content-Disposition'] = f'attachment; filename="{filename}"'
|
||||
return response
|
||||
|
||||
return render(request, self.get_template_name(), {
|
||||
'object': instance,
|
||||
'tab': self.tab,
|
||||
**context,
|
||||
})
|
||||
|
||||
def get_extra_context(self, request, instance):
|
||||
# Compile context data
|
||||
context_data = instance.get_config_context()
|
||||
context_data.update({'virtualmachine': instance})
|
||||
|
||||
# Render the config template
|
||||
rendered_config = None
|
||||
if config_template := instance.get_config_template():
|
||||
try:
|
||||
rendered_config = config_template.render(context=context_data)
|
||||
except TemplateError as e:
|
||||
messages.error(request, f"An error occurred while rendering the template: {e}")
|
||||
rendered_config = traceback.format_exc()
|
||||
|
||||
return {
|
||||
'config_template': config_template,
|
||||
'context_data': context_data,
|
||||
'rendered_config': rendered_config,
|
||||
}
|
||||
|
||||
|
||||
@register_model_view(VirtualMachine, 'edit')
|
||||
class VirtualMachineEditView(generic.ObjectEditView):
|
||||
queryset = VirtualMachine.objects.all()
|
||||
|
||||
Reference in New Issue
Block a user