-
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.
- Loading branch information
1 parent
9c5f416
commit ba7fab3
Showing
15 changed files
with
310 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Generated by Django 4.1.7 on 2023-03-23 17:35 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ManagedFile', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), | ||
('data_path', models.CharField(blank=True, editable=False, max_length=1000)), | ||
('data_synced', models.DateTimeField(blank=True, editable=False, null=True)), | ||
('created', models.DateTimeField(auto_now_add=True)), | ||
('last_updated', models.DateTimeField(blank=True, editable=False, null=True)), | ||
('file_root', models.CharField(max_length=1000)), | ||
('file_path', models.FilePathField(editable=False)), | ||
('data_file', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='core.datafile')), | ||
('data_source', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='core.datasource')), | ||
], | ||
options={ | ||
'ordering': ('file_root', 'file_path'), | ||
}, | ||
), | ||
migrations.AddIndex( | ||
model_name='managedfile', | ||
index=models.Index(fields=['file_root', 'file_path'], name='core_managedfile_root_path'), | ||
), | ||
migrations.AddConstraint( | ||
model_name='managedfile', | ||
constraint=models.UniqueConstraint(fields=('file_root', 'file_path'), name='core_managedfile_unique_root_path'), | ||
), | ||
] |
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 +1,2 @@ | ||
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
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,82 @@ | ||
import logging | ||
import os | ||
from importlib.machinery import FileFinder | ||
from pkgutil import ModuleInfo, get_importer | ||
|
||
from django.conf import settings | ||
from django.db import models | ||
from django.urls import reverse | ||
from django.utils.translation import gettext as _ | ||
|
||
from netbox.models.features import SyncedDataMixin | ||
from utilities.querysets import RestrictedQuerySet | ||
|
||
__all__ = ( | ||
'ManagedFile', | ||
) | ||
|
||
logger = logging.getLogger('netbox.core.files') | ||
|
||
ROOT_PATH_CHOICES = ( | ||
('scripts', 'Scripts Root'), | ||
('reports', 'Reports Root'), | ||
) | ||
|
||
|
||
class ManagedFile(SyncedDataMixin, models.Model): | ||
""" | ||
Database representation for a file on disk. | ||
""" | ||
created = models.DateTimeField( | ||
auto_now_add=True | ||
) | ||
last_updated = models.DateTimeField( | ||
editable=False, | ||
blank=True, | ||
null=True | ||
) | ||
file_root = models.CharField( | ||
max_length=1000, | ||
choices=ROOT_PATH_CHOICES | ||
) | ||
file_path = models.FilePathField( | ||
editable=False, | ||
help_text=_("File path relative to the designated root path") | ||
) | ||
|
||
objects = RestrictedQuerySet.as_manager() | ||
|
||
class Meta: | ||
ordering = ('file_root', 'file_path') | ||
constraints = ( | ||
models.UniqueConstraint( | ||
fields=('file_root', 'file_path'), | ||
name='%(app_label)s_%(class)s_unique_root_path' | ||
), | ||
) | ||
indexes = [ | ||
models.Index(fields=('file_root', 'file_path'), name='core_managedfile_root_path'), | ||
] | ||
|
||
def __str__(self): | ||
return f'{self.get_file_root_display()}: {self.file_path}' | ||
|
||
def get_absolute_url(self): | ||
return reverse('core:managedfile', args=[self.pk]) | ||
|
||
@property | ||
def full_path(self): | ||
return os.path.join(self._resolve_root_path(), self.file_path) | ||
|
||
def _resolve_root_path(self): | ||
return { | ||
'scripts': settings.SCRIPTS_ROOT, | ||
'reports': settings.REPORTS_ROOT, | ||
}[self.file_root] | ||
|
||
def get_module_info(self): | ||
return ModuleInfo( | ||
module_finder=get_importer(self.file_root), | ||
name=self.file_path.split('.py')[0], | ||
ispkg=False | ||
) |
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 +1,2 @@ | ||
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,25 @@ | ||
import django_tables2 as tables | ||
|
||
from core.models import * | ||
from netbox.tables import NetBoxTable, columns | ||
|
||
__all__ = ( | ||
'ManagedFileTable', | ||
) | ||
|
||
|
||
class ManagedFileTable(NetBoxTable): | ||
file_path = tables.Column( | ||
linkify=True | ||
) | ||
last_updated = columns.DateTimeColumn() | ||
actions = columns.ActionsColumn( | ||
actions=('delete',) | ||
) | ||
|
||
class Meta(NetBoxTable.Meta): | ||
model = ManagedFile | ||
fields = ( | ||
'pk', 'id', 'file_root', 'file_path', 'last_updated', 'size', 'hash', | ||
) | ||
default_columns = ('pk', 'file_root', 'file_path', 'last_updated') |
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,47 @@ | ||
import pkgutil | ||
|
||
from django.conf import settings | ||
from django.db import migrations | ||
|
||
|
||
def create_files(cls, root_name, path): | ||
|
||
modules = list(pkgutil.iter_modules([path])) | ||
filenames = [f'{m.name}.py' for m in modules] | ||
|
||
managed_files = [ | ||
cls( | ||
file_root=root_name, | ||
file_path=filename | ||
) for filename in filenames | ||
] | ||
cls.objects.bulk_create(managed_files) | ||
|
||
|
||
def replicate_scripts(apps, schema_editor): | ||
ManagedFile = apps.get_model('core', 'ManagedFile') | ||
create_files(ManagedFile, 'scripts', settings.SCRIPTS_ROOT) | ||
|
||
|
||
def replicate_reports(apps, schema_editor): | ||
ManagedFile = apps.get_model('core', 'ManagedFile') | ||
create_files(ManagedFile, 'reports', settings.REPORTS_ROOT) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0002_managedfile'), | ||
('extras', '0090_objectchange_index_request_id'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
code=replicate_scripts, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
migrations.RunPython( | ||
code=replicate_reports, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
] |
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,57 @@ | ||
{% extends 'generic/object.html' %} | ||
{% load buttons %} | ||
{% load custom_links %} | ||
{% load helpers %} | ||
{% load perms %} | ||
{% load plugins %} | ||
|
||
{% block controls %} | ||
<div class="controls"> | ||
<div class="control-group"> | ||
{% plugin_buttons object %} | ||
</div> | ||
{% if request.user|can_delete:object %} | ||
{% delete_button object %} | ||
{% endif %} | ||
<div class="control-group"> | ||
{% custom_links object %} | ||
</div> | ||
</div> | ||
{% endblock controls %} | ||
|
||
{% block content %} | ||
<div class="row mb-3"> | ||
<div class="col"> | ||
<div class="card"> | ||
<h5 class="card-header">Managed File</h5> | ||
<div class="card-body"> | ||
<table class="table table-hover attr-table"> | ||
<tr> | ||
<th scope="row">Root</th> | ||
<td>{{ object.get_file_root_display }}</td> | ||
</tr> | ||
<tr> | ||
<th scope="row">Path</th> | ||
<td> | ||
<span class="font-monospace" id="datafile_path">{{ object.file_path }}</span> | ||
<a class="btn btn-sm btn-primary copy-token" data-clipboard-target="#datafile_path" title="Copy to clipboard"> | ||
<i class="mdi mdi-content-copy"></i> | ||
</a> | ||
</td> | ||
</tr> | ||
<tr> | ||
<th scope="row">Last Updated</th> | ||
<td>{{ object.last_updated }}</td> | ||
</tr> | ||
</table> | ||
</div> | ||
</div> | ||
{% plugin_left_page object %} | ||
</div> | ||
</div> | ||
<div class="row mb-3"> | ||
<div class="col col-md-12"> | ||
{% plugin_full_width_page object %} | ||
</div> | ||
</div> | ||
{% endblock %} |