-
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
* Initial work on #11890 * Consolidate get_scripts() and get_reports() functions * Introduce proxy models for script & report modules * Add add/delete views for reports & scripts * Add deletion links for modules * Enable resolving scripts/reports from module class * Remove get_modules() utility function * Show results in report/script lists * Misc cleanup * Fix file uploads * Support automatic migration for submodules * Fix module child ordering * Template cleanup * Remove ManagedFile views * Move is_script(), is_report() into extras.utils * Fix URLs for nested reports & scripts * Misc cleanup
- Loading branch information
1 parent
9c5f416
commit f7a2eb8
Showing
23 changed files
with
658 additions
and
315 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,88 @@ | ||
import logging | ||
import os | ||
|
||
from django.conf import settings | ||
from django.db import models | ||
from django.urls import reverse | ||
from django.utils.translation import gettext as _ | ||
|
||
from ..choices import ManagedFileRootPathChoices | ||
from netbox.models.features import SyncedDataMixin | ||
from utilities.querysets import RestrictedQuerySet | ||
|
||
__all__ = ( | ||
'ManagedFile', | ||
) | ||
|
||
logger = logging.getLogger('netbox.core.files') | ||
|
||
|
||
class ManagedFile(SyncedDataMixin, models.Model): | ||
""" | ||
Database representation for a file on disk. This class is typically wrapped by a proxy class (e.g. ScriptModule) | ||
to provide additional functionality. | ||
""" | ||
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=ManagedFileRootPathChoices | ||
) | ||
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 self.name | ||
|
||
def get_absolute_url(self): | ||
return reverse('core:managedfile', args=[self.pk]) | ||
|
||
@property | ||
def name(self): | ||
return self.file_path | ||
|
||
@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 sync_data(self): | ||
if self.data_file: | ||
self.file_path = os.path.basename(self.data_path) | ||
self.data_file.write_to_disk(self.full_path, overwrite=True) | ||
|
||
def delete(self, *args, **kwargs): | ||
# Delete file from disk | ||
try: | ||
os.remove(self.full_path) | ||
except FileNotFoundError: | ||
pass | ||
|
||
return super().delete(*args, **kwargs) |
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
Oops, something went wrong.