def put(self, request): agent = get_object_or_404(Agent, agent_id=request.data["agent_id"]) serializer = WinAgentSerializer(instance=agent, data=request.data, partial=True) if request.data["func"] == "disks": disks = request.data["disks"] new = [] for disk in disks: tmp = {} for _, _ in disk.items(): tmp["device"] = disk["device"] tmp["fstype"] = disk["fstype"] tmp["total"] = bytes2human(disk["total"]) tmp["used"] = bytes2human(disk["used"]) tmp["free"] = bytes2human(disk["free"]) tmp["percent"] = int(disk["percent"]) new.append(tmp) serializer.is_valid(raise_exception=True) serializer.save(disks=new) return Response("ok") if request.data["func"] == "loggedonuser": if request.data["logged_in_username"] != "None": serializer.is_valid(raise_exception=True) serializer.save( last_logged_in_user=request.data["logged_in_username"]) return Response("ok") if request.data["func"] == "software": raw: SoftwareList = request.data["software"] if not isinstance(raw, list): return notify_error("err") sw = filter_software(raw) if not InstalledSoftware.objects.filter(agent=agent).exists(): InstalledSoftware(agent=agent, software=sw).save() else: s = agent.installedsoftware_set.first() # type: ignore s.software = sw s.save(update_fields=["software"]) return Response("ok") serializer.is_valid(raise_exception=True) serializer.save() return Response("ok")
def post(self, request): agent = get_object_or_404(Agent, agent_id=request.data["agent_id"]) raw: SoftwareList = request.data["software"] if not isinstance(raw, list): return notify_error("err") sw = filter_software(raw) if not InstalledSoftware.objects.filter(agent=agent).exists(): InstalledSoftware(agent=agent, software=sw).save() else: s = agent.installedsoftware_set.first() s.software = sw s.save(update_fields=["software"]) return Response("ok")