mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 17:26:10 -06:00
Redirect user to filtered objects list after bulk import
This commit is contained in:
parent
6e4c4c4342
commit
4fb5ea0ea4
@ -1994,7 +1994,7 @@ class ModuleTestCase(
|
|||||||
}
|
}
|
||||||
|
|
||||||
initial_count = Module.objects.count()
|
initial_count = Module.objects.count()
|
||||||
self.assertHttpStatus(self.client.post(**request), 200)
|
self.assertHttpStatus(self.client.post(**request), 302)
|
||||||
self.assertEqual(Module.objects.count(), initial_count + len(csv_data) - 1)
|
self.assertEqual(Module.objects.count(), initial_count + len(csv_data) - 1)
|
||||||
self.assertEqual(Interface.objects.filter(device=device).count(), 0)
|
self.assertEqual(Interface.objects.filter(device=device).count(), 0)
|
||||||
|
|
||||||
@ -2010,7 +2010,7 @@ class ModuleTestCase(
|
|||||||
}
|
}
|
||||||
|
|
||||||
initial_count = Module.objects.count()
|
initial_count = Module.objects.count()
|
||||||
self.assertHttpStatus(self.client.post(**request), 200)
|
self.assertHttpStatus(self.client.post(**request), 302)
|
||||||
self.assertEqual(Module.objects.count(), initial_count + len(csv_data) - 1)
|
self.assertEqual(Module.objects.count(), initial_count + len(csv_data) - 1)
|
||||||
self.assertEqual(Interface.objects.filter(device=device).count(), 5)
|
self.assertEqual(Interface.objects.filter(device=device).count(), 5)
|
||||||
|
|
||||||
@ -2086,7 +2086,7 @@ class ModuleTestCase(
|
|||||||
}
|
}
|
||||||
|
|
||||||
initial_count = self._get_queryset().count()
|
initial_count = self._get_queryset().count()
|
||||||
self.assertHttpStatus(self.client.post(**request), 200)
|
self.assertHttpStatus(self.client.post(**request), 302)
|
||||||
self.assertEqual(self._get_queryset().count(), initial_count + len(csv_data) - 1)
|
self.assertEqual(self._get_queryset().count(), initial_count + len(csv_data) - 1)
|
||||||
|
|
||||||
# Re-retrieve interface to get new module id
|
# Re-retrieve interface to get new module id
|
||||||
|
@ -1047,7 +1047,7 @@ class CustomFieldImportTest(TestCase):
|
|||||||
csv_data = '\n'.join(','.join(row) for row in data)
|
csv_data = '\n'.join(','.join(row) for row in data)
|
||||||
|
|
||||||
response = self.client.post(reverse('dcim:site_import'), {'data': csv_data, 'format': 'csv'})
|
response = self.client.post(reverse('dcim:site_import'), {'data': csv_data, 'format': 'csv'})
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 302)
|
||||||
self.assertEqual(Site.objects.count(), 3)
|
self.assertEqual(Site.objects.count(), 3)
|
||||||
|
|
||||||
# Validate data for site 1
|
# Validate data for site 1
|
||||||
|
@ -42,7 +42,7 @@ class CSVImportTestCase(ModelViewTestCase):
|
|||||||
self.assertHttpStatus(self.client.get(self._get_url('import')), 200)
|
self.assertHttpStatus(self.client.get(self._get_url('import')), 200)
|
||||||
|
|
||||||
# Test POST with permission
|
# Test POST with permission
|
||||||
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 200)
|
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 302)
|
||||||
regions = Region.objects.all()
|
regions = Region.objects.all()
|
||||||
self.assertEqual(regions.count(), 4)
|
self.assertEqual(regions.count(), 4)
|
||||||
region = Region.objects.get(slug="region-4")
|
region = Region.objects.get(slug="region-4")
|
||||||
|
@ -11,6 +11,7 @@ from django.db.models.fields.reverse_related import ManyToManyRel
|
|||||||
from django.forms import ModelMultipleChoiceField, MultipleHiddenInput
|
from django.forms import ModelMultipleChoiceField, MultipleHiddenInput
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.shortcuts import get_object_or_404, redirect, render
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
|
from django.urls import reverse
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django_tables2.export import TableExport
|
from django_tables2.export import TableExport
|
||||||
|
|
||||||
@ -22,6 +23,7 @@ from utilities.forms import BulkRenameForm, ConfirmationForm, restrict_form_fiel
|
|||||||
from utilities.forms.bulk_import import ImportForm
|
from utilities.forms.bulk_import import ImportForm
|
||||||
from utilities.htmx import is_embedded, is_htmx
|
from utilities.htmx import is_embedded, is_htmx
|
||||||
from utilities.permissions import get_permission_for_model
|
from utilities.permissions import get_permission_for_model
|
||||||
|
from utilities.utils import get_viewname
|
||||||
from utilities.views import GetReturnURLMixin
|
from utilities.views import GetReturnURLMixin
|
||||||
from .base import BaseMultiObjectView
|
from .base import BaseMultiObjectView
|
||||||
from .mixins import ActionsMixin, TableMixin
|
from .mixins import ActionsMixin, TableMixin
|
||||||
@ -435,7 +437,7 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
logger = logging.getLogger('netbox.views.BulkImportView')
|
logger = logging.getLogger('netbox.views.BulkImportView')
|
||||||
|
model = self.model_form._meta.model
|
||||||
form = ImportForm(request.POST, request.FILES)
|
form = ImportForm(request.POST, request.FILES)
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
@ -450,18 +452,14 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
if self.queryset.filter(pk__in=[obj.pk for obj in new_objs]).count() != len(new_objs):
|
if self.queryset.filter(pk__in=[obj.pk for obj in new_objs]).count() != len(new_objs):
|
||||||
raise PermissionsViolation
|
raise PermissionsViolation
|
||||||
|
|
||||||
# Compile a table containing the imported objects
|
|
||||||
obj_table = self.table(new_objs)
|
|
||||||
|
|
||||||
if new_objs:
|
if new_objs:
|
||||||
msg = 'Imported {} {}'.format(len(new_objs), new_objs[0]._meta.verbose_name_plural)
|
msg = f"Imported {len(new_objs)} {model._meta.verbose_name_plural}"
|
||||||
logger.info(msg)
|
logger.info(msg)
|
||||||
messages.success(request, msg)
|
messages.success(request, msg)
|
||||||
|
|
||||||
return render(request, "import_success.html", {
|
view_name = get_viewname(model, action='list')
|
||||||
'table': obj_table,
|
results_url = f"{reverse(view_name)}?created_by_request={request.id}"
|
||||||
'return_url': self.get_return_url(request),
|
return redirect(results_url)
|
||||||
})
|
|
||||||
|
|
||||||
except (AbortTransaction, ValidationError):
|
except (AbortTransaction, ValidationError):
|
||||||
clear_webhooks.send(sender=self)
|
clear_webhooks.send(sender=self)
|
||||||
@ -475,7 +473,7 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
logger.debug("Form validation failed")
|
logger.debug("Form validation failed")
|
||||||
|
|
||||||
return render(request, self.template_name, {
|
return render(request, self.template_name, {
|
||||||
'model': self.model_form._meta.model,
|
'model': model,
|
||||||
'form': form,
|
'form': form,
|
||||||
'fields': self.model_form().fields,
|
'fields': self.model_form().fields,
|
||||||
'return_url': self.get_return_url(request),
|
'return_url': self.get_return_url(request),
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
{% extends 'base/layout.html' %}
|
|
||||||
{% load render_table from django_tables2 %}
|
|
||||||
|
|
||||||
{% block title %}Import Completed{% endblock %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
<div class="table-responsive">
|
|
||||||
{% render_table table 'inc/table.html' %}
|
|
||||||
</div>
|
|
||||||
{% if return_url %}
|
|
||||||
<a href="{{ return_url }}" class="btn btn-outline-dark">View All</a>
|
|
||||||
{% endif %}
|
|
||||||
<a href="{{ request.path }}" class="btn btn-primary">
|
|
||||||
<span class="mdi mdi-database-import-outline" aria-hidden="true"></span>
|
|
||||||
Import More
|
|
||||||
</a>
|
|
||||||
{% endblock content %}
|
|
@ -592,7 +592,7 @@ class ViewTestCases:
|
|||||||
self.assertHttpStatus(self.client.get(self._get_url('import')), 200)
|
self.assertHttpStatus(self.client.get(self._get_url('import')), 200)
|
||||||
|
|
||||||
# Test POST with permission
|
# Test POST with permission
|
||||||
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 200)
|
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 302)
|
||||||
self.assertEqual(self._get_queryset().count(), initial_count + len(self.csv_data) - 1)
|
self.assertEqual(self._get_queryset().count(), initial_count + len(self.csv_data) - 1)
|
||||||
|
|
||||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||||
@ -617,7 +617,7 @@ class ViewTestCases:
|
|||||||
obj_perm.object_types.add(ContentType.objects.get_for_model(self.model))
|
obj_perm.object_types.add(ContentType.objects.get_for_model(self.model))
|
||||||
|
|
||||||
# Test POST with permission
|
# Test POST with permission
|
||||||
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 200)
|
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 302)
|
||||||
self.assertEqual(initial_count, self._get_queryset().count())
|
self.assertEqual(initial_count, self._get_queryset().count())
|
||||||
|
|
||||||
reader = csv.DictReader(array, delimiter=',')
|
reader = csv.DictReader(array, delimiter=',')
|
||||||
@ -659,7 +659,7 @@ class ViewTestCases:
|
|||||||
obj_perm.save()
|
obj_perm.save()
|
||||||
|
|
||||||
# Import permitted objects
|
# Import permitted objects
|
||||||
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 200)
|
self.assertHttpStatus(self.client.post(self._get_url('import'), data), 302)
|
||||||
self.assertEqual(self._get_queryset().count(), initial_count + len(self.csv_data) - 1)
|
self.assertEqual(self._get_queryset().count(), initial_count + len(self.csv_data) - 1)
|
||||||
|
|
||||||
class BulkEditObjectsViewTestCase(ModelViewTestCase):
|
class BulkEditObjectsViewTestCase(ModelViewTestCase):
|
||||||
|
Loading…
Reference in New Issue
Block a user