def post(self, request, pk): cluster = get_object_or_404(Cluster, pk=pk) if '_confirm' in request.POST: form = self.form(request.POST) if form.is_valid(): device_pks = form.cleaned_data['pk'] with transaction.atomic(): # Remove the selected Devices from the Cluster for device in Device.objects.filter(pk__in=device_pks): device.cluster = None device.save() messages.success( request, "Removed {} devices from cluster {}".format( len(device_pks), cluster)) return redirect(cluster.get_absolute_url()) else: form = self.form(initial={'pk': request.POST.getlist('pk')}) selected_objects = Device.objects.filter(pk__in=form.initial['pk']) device_table = DeviceTable(list(selected_objects), orderable=False) return render( request, self.template_name, { 'form': form, 'parent_obj': cluster, 'table': device_table, 'obj_type_plural': 'devices', 'return_url': cluster.get_absolute_url(), })
def get_extra_context(self, request, instance): devices = Device.objects.restrict(request.user, 'view').filter( cluster=instance).prefetch_related('site', 'rack', 'tenant', 'device_type__manufacturer') device_table = DeviceTable(list(devices), orderable=False) if request.user.has_perm('virtualization.change_cluster'): device_table.columns.show('pk') return { 'device_table': device_table, }
def get(self, request, pk): cluster = get_object_or_404(Cluster, pk=pk) devices = Device.objects.filter(cluster=cluster).select_related( 'site', 'rack', 'tenant', 'device_type__manufacturer') device_table = DeviceTable(list(devices), orderable=False) if request.user.has_perm('virtualization:change_cluster'): device_table.columns.show('pk') return render(request, 'virtualization/cluster.html', { 'cluster': cluster, 'device_table': device_table, })
def get(self, request, pk): self.queryset = self.queryset.prefetch_related( Prefetch('virtual_machines', queryset=VirtualMachine.objects.restrict(request.user))) cluster = get_object_or_404(self.queryset, pk=pk) devices = Device.objects.restrict(request.user, 'view').filter( cluster=cluster).prefetch_related('site', 'rack', 'tenant', 'device_type__manufacturer') device_table = DeviceTable(list(devices), orderable=False) if request.user.has_perm('virtualization.change_cluster'): device_table.columns.show('pk') return render(request, 'virtualization/cluster.html', { 'cluster': cluster, 'device_table': device_table, })