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 post(self, request, pk): cluster = get_object_or_404(self.queryset, 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): """ Reuse the model tables for config context, device, and virtual machine but inject the `ConfigContextSchemaValidationStateColumn` and an object edit action button. """ # Prep the validator with the schema so it can be reused for all records validator = Draft7Validator(instance.data_schema) # Config context table config_context_table = tables.ConfigContextTable( data=instance.configcontext_set.all(), orderable=False, extra_columns=[ ( "validation_state", tables.ConfigContextSchemaValidationStateColumn(validator, "data", empty_values=()), ), ("actions", ButtonsColumn(model=ConfigContext, buttons=["edit"])), ], ) paginate = { "paginator_class": EnhancedPaginator, "per_page": get_paginate_count(request), } RequestConfig(request, paginate).configure(config_context_table) # Device table device_table = DeviceTable( data=instance.device_set.prefetch_related( "tenant", "site", "rack", "device_type", "device_role", "primary_ip" ), orderable=False, extra_columns=[ ( "validation_state", tables.ConfigContextSchemaValidationStateColumn(validator, "local_context_data", empty_values=()), ), ("actions", ButtonsColumn(model=Device, buttons=["edit"])), ], ) paginate = { "paginator_class": EnhancedPaginator, "per_page": get_paginate_count(request), } RequestConfig(request, paginate).configure(device_table) # Virtual machine table virtual_machine_table = VirtualMachineTable( data=instance.virtualmachine_set.prefetch_related("cluster", "role", "tenant", "primary_ip"), orderable=False, extra_columns=[ ( "validation_state", tables.ConfigContextSchemaValidationStateColumn(validator, "local_context_data", empty_values=()), ), ("actions", ButtonsColumn(model=VirtualMachine, buttons=["edit"])), ], ) paginate = { "paginator_class": EnhancedPaginator, "per_page": get_paginate_count(request), } RequestConfig(request, paginate).configure(virtual_machine_table) return { "config_context_table": config_context_table, "device_table": device_table, "virtual_machine_table": virtual_machine_table, "active_tab": "validation", }