Fixes #13622: Fix exception when viewing current config and no revisions have been created

This commit is contained in:
Jeremy Stretch 2023-08-31 08:52:46 -04:00
parent 06f2c6f867
commit 2544e2bf18
3 changed files with 21 additions and 3 deletions

View File

@ -2,6 +2,7 @@ from django.contrib import messages
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from extras.models import ConfigRevision from extras.models import ConfigRevision
from netbox.config import get_config
from netbox.views import generic from netbox.views import generic
from netbox.views.generic.base import BaseObjectView from netbox.views.generic.base import BaseObjectView
from utilities.utils import count_related from utilities.utils import count_related
@ -152,4 +153,9 @@ class ConfigView(generic.ObjectView):
queryset = ConfigRevision.objects.all() queryset = ConfigRevision.objects.all()
def get_object(self, **kwargs): def get_object(self, **kwargs):
return self.queryset.first() if config := self.queryset.first():
return config
# Instantiate a dummy default config if none has been created yet
return ConfigRevision(
data=get_config().defaults
)

View File

@ -723,6 +723,8 @@ class ConfigRevision(models.Model):
verbose_name_plural = _('config revisions') verbose_name_plural = _('config revisions')
def __str__(self): def __str__(self):
if not self.pk:
return gettext('Default configuration')
if self.is_active: if self.is_active:
return gettext('Current configuration') return gettext('Current configuration')
return gettext('Config revision #{id}').format(id=self.pk) return gettext('Config revision #{id}').format(id=self.pk)
@ -733,6 +735,8 @@ class ConfigRevision(models.Model):
return super().__getattribute__(item) return super().__getattribute__(item)
def get_absolute_url(self): def get_absolute_url(self):
if not self.pk:
return reverse('core:config') # Default config view
return reverse('extras:configrevision', args=[self.pk]) return reverse('extras:configrevision', args=[self.pk])
def activate(self): def activate(self):

View File

@ -14,11 +14,11 @@
<div class="controls"> <div class="controls">
<div class="control-group"> <div class="control-group">
{% plugin_buttons object %} {% plugin_buttons object %}
{% if object.is_active and perms.extras.add_configrevision %} {% if not object.pk or object.is_active and perms.extras.add_configrevision %}
{% url 'extras:configrevision_add' as edit_url %} {% url 'extras:configrevision_add' as edit_url %}
{% include "buttons/edit.html" with url=edit_url %} {% include "buttons/edit.html" with url=edit_url %}
{% endif %} {% endif %}
{% if not object.is_active and perms.extras.delete_configrevision %} {% if object.pk and not object.is_active and perms.extras.delete_configrevision %}
{% delete_button object %} {% delete_button object %}
{% endif %} {% endif %}
</div> </div>
@ -28,6 +28,14 @@
</div> </div>
{% endblock controls %} {% endblock controls %}
{% block subtitle %}
{% if object.created %}
<div class="object-subtitle">
<span>{% trans "Created" %} {{ object.created|annotated_date }}</span>
</div>
{% endif %}
{% endblock subtitle %}
{% block content %} {% block content %}
<div class="row"> <div class="row">
<div class="col col-md-12"> <div class="col col-md-12">