-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Move ConfigRevision model & write migrations * Move ConfigRevision resources from extras to core * Extend migration to update original content type for ConfigRevision
- Loading branch information
1 parent
18422e1
commit 975a647
Showing
26 changed files
with
417 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0008_contenttype_proxy'), | ||
] | ||
|
||
operations = [ | ||
migrations.SeparateDatabaseAndState( | ||
state_operations=[ | ||
migrations.CreateModel( | ||
name='ConfigRevision', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), | ||
('created', models.DateTimeField(auto_now_add=True)), | ||
('comment', models.CharField(blank=True, max_length=200)), | ||
('data', models.JSONField(blank=True, null=True)), | ||
], | ||
options={ | ||
'verbose_name': 'config revision', | ||
'verbose_name_plural': 'config revisions', | ||
'ordering': ['-created'], | ||
}, | ||
), | ||
], | ||
# Table will be renamed from extras_configrevision in extras/0101_move_configrevision | ||
database_operations=[], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .config import * | ||
from .contenttypes import * | ||
from .data import * | ||
from .files import * | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from django.core.cache import cache | ||
from django.db import models | ||
from django.urls import reverse | ||
from django.utils.translation import gettext, gettext_lazy as _ | ||
|
||
from utilities.querysets import RestrictedQuerySet | ||
|
||
__all__ = ( | ||
'ConfigRevision', | ||
) | ||
|
||
|
||
class ConfigRevision(models.Model): | ||
""" | ||
An atomic revision of NetBox's configuration. | ||
""" | ||
created = models.DateTimeField( | ||
verbose_name=_('created'), | ||
auto_now_add=True | ||
) | ||
comment = models.CharField( | ||
verbose_name=_('comment'), | ||
max_length=200, | ||
blank=True | ||
) | ||
data = models.JSONField( | ||
blank=True, | ||
null=True, | ||
verbose_name=_('configuration data') | ||
) | ||
|
||
objects = RestrictedQuerySet.as_manager() | ||
|
||
class Meta: | ||
ordering = ['-created'] | ||
verbose_name = _('config revision') | ||
verbose_name_plural = _('config revisions') | ||
|
||
def __str__(self): | ||
if not self.pk: | ||
return gettext('Default configuration') | ||
if self.is_active: | ||
return gettext('Current configuration') | ||
return gettext('Config revision #{id}').format(id=self.pk) | ||
|
||
def __getattr__(self, item): | ||
if item in self.data: | ||
return self.data[item] | ||
return super().__getattribute__(item) | ||
|
||
def get_absolute_url(self): | ||
if not self.pk: | ||
return reverse('core:config') # Default config view | ||
return reverse('core:configrevision', args=[self.pk]) | ||
|
||
def activate(self): | ||
""" | ||
Cache the configuration data. | ||
""" | ||
cache.set('config', self.data, None) | ||
cache.set('config_version', self.pk, None) | ||
activate.alters_data = True | ||
|
||
@property | ||
def is_active(self): | ||
return cache.get('config_version') == self.pk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .config import * | ||
from .data import * | ||
from .jobs import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from core.models import ConfigRevision | ||
from netbox.tables import NetBoxTable, columns | ||
|
||
__all__ = ( | ||
'ConfigRevisionTable', | ||
) | ||
|
||
REVISION_BUTTONS = """ | ||
{% if not record.is_active %} | ||
<a href="{% url 'core:configrevision_restore' pk=record.pk %}" class="btn btn-sm btn-primary" title="Restore config"> | ||
<i class="mdi mdi-file-restore"></i> | ||
</a> | ||
{% endif %} | ||
""" | ||
|
||
|
||
class ConfigRevisionTable(NetBoxTable): | ||
is_active = columns.BooleanColumn( | ||
verbose_name=_('Is Active'), | ||
) | ||
actions = columns.ActionsColumn( | ||
actions=('delete',), | ||
extra_buttons=REVISION_BUTTONS | ||
) | ||
|
||
class Meta(NetBoxTable.Meta): | ||
model = ConfigRevision | ||
fields = ( | ||
'pk', 'id', 'is_active', 'created', 'comment', | ||
) | ||
default_columns = ('pk', 'id', 'is_active', 'created', 'comment') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.