Fix file uploads

This commit is contained in:
jeremystretch 2023-03-24 14:21:57 -04:00
parent 090a28131e
commit c20eeb8114
5 changed files with 18 additions and 6 deletions

View File

@ -98,3 +98,12 @@ class ManagedFileForm(SyncedDataMixin, NetBoxModelForm):
raise forms.ValidationError("Cannot upload a file and sync from an existing file")
return self.cleaned_data
def save(self, *args, **kwargs):
# If a file was uploaded, save it to disk
if self.cleaned_data['upload_file']:
self.instance.file_path = self.cleaned_data['upload_file'].name
with open(self.instance.full_path, 'wb+') as new_file:
new_file.write(self.cleaned_data['upload_file'].read())
return super().save(*args, **kwargs)

View File

@ -74,11 +74,14 @@ class ManagedFile(SyncedDataMixin, models.Model):
def sync_data(self):
if self.data_file:
self.file_path = self.data_path
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
os.remove(self.full_path)
try:
os.remove(self.full_path)
except FileNotFoundError:
pass
return super().delete(*args, **kwargs)

View File

@ -850,7 +850,7 @@ class ScriptModuleManager(models.Manager.from_queryset(RestrictedQuerySet)):
return super().get_queryset().filter(file_root='scripts')
class ScriptModule(JobResultsMixin, WebhooksMixin, PythonModuleMixin, ManagedFile):
class ScriptModule(PythonModuleMixin, ManagedFile):
"""
Proxy model for script module files.
"""
@ -901,7 +901,7 @@ class ReportModuleManager(models.Manager.from_queryset(RestrictedQuerySet)):
return super().get_queryset().filter(file_root='reports')
class ReportModule(JobResultsMixin, WebhooksMixin, PythonModuleMixin, ManagedFile):
class ReportModule(PythonModuleMixin, ManagedFile):
"""
Proxy model for report module files.
"""

View File

@ -94,7 +94,7 @@ urlpatterns = [
# Reports
path('reports/', views.ReportListView.as_view(), name='report_list'),
path('reports/add/', views.ScriptModuleCreateView.as_view(), name='reportmodule_add'),
path('reports/add/', views.ReportModuleCreateView.as_view(), name='reportmodule_add'),
path('reports/results/<int:job_result_pk>/', views.ReportResultView.as_view(), name='report_result'),
path('reports/<int:pk>/', include(get_model_urls('extras', 'reportmodule'))),
path('reports/<str:module>.<str:name>/', views.ReportView.as_view(), name='report'),

View File

@ -827,7 +827,7 @@ class ReportListView(ContentTypePermissionRequiredMixin, View):
}
return render(request, 'extras/report_list.html', {
'model': ScriptModule,
'model': ReportModule,
'report_modules': report_modules,
'job_results': job_results,
})