def get(self, request, *args, **kwargs): self.form = DeviceSearchForm(request.GET) self.form.is_valid() self.devices = list(self.form.fetch_devices()) if len(self.devices) == 1: return HttpResponseRedirect(reverse("mdm:device", args=(self.devices[0]["urlsafe_serial_number"],))) return super().get(request, *args, **kwargs)
class DevicesView(LoginRequiredMixin, TemplateView): template_name = "mdm/device_list.html" def get(self, request, *args, **kwargs): self.form = DeviceSearchForm(request.GET) self.form.is_valid() self.devices = list(self.form.fetch_devices()) if len(self.devices) == 1: return HttpResponseRedirect(reverse("mdm:device", args=(self.devices[0]["urlsafe_serial_number"],))) return super().get(request, *args, **kwargs) def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) ctx["mdm"] = True ctx["form"] = self.form ctx["devices"] = self.devices ctx["devices_count"] = len(self.devices) bc = [(None, "MDM")] if not self.form.is_initial(): bc.extend([(reverse("mdm:devices"), "Devices"), (None, "Search")]) else: bc.extend([(None, "Devices")]) ctx["breadcrumbs"] = bc return ctx