Skip to content

Commit 2544e2b

Browse files
committed
Fixes #13622: Fix exception when viewing current config and no revisions have been created
1 parent 06f2c6f commit 2544e2b

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

netbox/core/views.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.shortcuts import get_object_or_404, redirect
33

44
from extras.models import ConfigRevision
5+
from netbox.config import get_config
56
from netbox.views import generic
67
from netbox.views.generic.base import BaseObjectView
78
from utilities.utils import count_related
@@ -152,4 +153,9 @@ class ConfigView(generic.ObjectView):
152153
queryset = ConfigRevision.objects.all()
153154

154155
def get_object(self, **kwargs):
155-
return self.queryset.first()
156+
if config := self.queryset.first():
157+
return config
158+
# Instantiate a dummy default config if none has been created yet
159+
return ConfigRevision(
160+
data=get_config().defaults
161+
)

netbox/extras/models/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ class Meta:
723723
verbose_name_plural = _('config revisions')
724724

725725
def __str__(self):
726+
if not self.pk:
727+
return gettext('Default configuration')
726728
if self.is_active:
727729
return gettext('Current configuration')
728730
return gettext('Config revision #{id}').format(id=self.pk)
@@ -733,6 +735,8 @@ def __getattr__(self, item):
733735
return super().__getattribute__(item)
734736

735737
def get_absolute_url(self):
738+
if not self.pk:
739+
return reverse('core:config') # Default config view
736740
return reverse('extras:configrevision', args=[self.pk])
737741

738742
def activate(self):

netbox/templates/extras/configrevision.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
<div class="controls">
1515
<div class="control-group">
1616
{% plugin_buttons object %}
17-
{% if object.is_active and perms.extras.add_configrevision %}
17+
{% if not object.pk or object.is_active and perms.extras.add_configrevision %}
1818
{% url 'extras:configrevision_add' as edit_url %}
1919
{% include "buttons/edit.html" with url=edit_url %}
2020
{% endif %}
21-
{% if not object.is_active and perms.extras.delete_configrevision %}
21+
{% if object.pk and not object.is_active and perms.extras.delete_configrevision %}
2222
{% delete_button object %}
2323
{% endif %}
2424
</div>
@@ -28,6 +28,14 @@
2828
</div>
2929
{% endblock controls %}
3030

31+
{% block subtitle %}
32+
{% if object.created %}
33+
<div class="object-subtitle">
34+
<span>{% trans "Created" %} {{ object.created|annotated_date }}</span>
35+
</div>
36+
{% endif %}
37+
{% endblock subtitle %}
38+
3139
{% block content %}
3240
<div class="row">
3341
<div class="col col-md-12">

0 commit comments

Comments
 (0)