def get(self, request, *args, **kwargs): playbook = serializers.DetailedPlaybookSerializer(self.get_object()) hosts = serializers.ListHostSerializer(models.Host.objects.filter( playbook=playbook.data["id"]).order_by("name").all(), many=True) files = serializers.ListFileSerializer( models.File.objects.filter(playbook=playbook.data["id"]).all(), many=True) records = serializers.ListRecordSerializer( models.Record.objects.filter(playbook=playbook.data["id"]).all(), many=True) search_form = forms.ResultSearchForm(request.GET) order = "-started" if "order" in request.GET: order = request.GET["order"] result_queryset = models.Result.objects.filter( playbook=playbook.data["id"]).order_by(order).all() result_filter = filters.ResultFilter(request.GET, queryset=result_queryset) page = self.paginate_queryset(result_filter.qs) if page is not None: serializer = serializers.ListResultSerializer(page, many=True) else: serializer = serializers.ListResultSerializer(result_filter, many=True) for result in serializer.data: task_id = result["task"] result["task"] = serializers.SimpleTaskSerializer( models.Task.objects.get(pk=task_id)).data host_id = result["host"] result["host"] = serializers.SimpleHostSerializer( models.Host.objects.get(pk=host_id)).data paginated_results = self.get_paginated_response(serializer.data) if self.paginator.count > (self.paginator.offset + self.paginator.limit): max_current = self.paginator.offset + self.paginator.limit else: max_current = self.paginator.count current_page_results = "%s-%s" % (self.paginator.offset + 1, max_current) # fmt: off return Response({ "playbook": playbook.data, "hosts": hosts.data, "files": files.data, "records": records.data, "results": paginated_results.data, "current_page_results": current_page_results, "search_form": search_form })
def get(self, request, *args, **kwargs): host = self.get_object() host_serializer = serializers.DetailedHostSerializer(host) order = "-started" if "order" in request.GET: order = request.GET["order"] result_queryset = models.Result.objects.filter( host=host_serializer.data["id"]).order_by(order).all() result_filter = filters.ResultFilter(request.GET, queryset=result_queryset) page = self.paginate_queryset(result_filter.qs) if page is not None: result_serializer = serializers.ListResultSerializer(page, many=True) else: result_serializer = serializers.ListResultSerializer(result_filter, many=True) for result in result_serializer.data: task_id = result["task"] result["task"] = serializers.SimpleTaskSerializer( models.Task.objects.get(pk=task_id)).data paginated_results = self.get_paginated_response(result_serializer.data) if self.paginator.count > (self.paginator.offset + self.paginator.limit): max_current = self.paginator.offset + self.paginator.limit else: max_current = self.paginator.count current_page_results = "%s-%s" % (self.paginator.offset + 1, max_current) # We need to expand the search card if there is a search query, not considering pagination args search_args = [ arg for arg in request.GET.keys() if arg not in ["limit", "offset"] ] expand_search = True if search_args else False search_form = forms.ResultSearchForm(request.GET) # fmt: off return Response( dict( current_page_results=current_page_results, expand_search=expand_search, host=host_serializer.data, page="host", results=paginated_results.data, search_form=search_form, static_generation=False, ))
def get(self, request, *args, **kwargs): playbook = serializers.DetailedPlaybookSerializer(self.get_object()) hosts = serializers.ListHostSerializer(models.Host.objects.filter( playbook=playbook.data["id"]).order_by("name").all(), many=True) files = serializers.ListFileSerializer( models.File.objects.filter(playbook=playbook.data["id"]).all(), many=True) records = serializers.ListRecordSerializer( models.Record.objects.filter(playbook=playbook.data["id"]).all(), many=True) order = "-started" if "order" in request.GET: order = request.GET["order"] result_queryset = models.Result.objects.filter( playbook=playbook.data["id"]).order_by(order).all() result_filter = filters.ResultFilter(request.GET, queryset=result_queryset) page = self.paginate_queryset(result_filter.qs) if page is not None: serializer = serializers.ListResultSerializer(page, many=True) else: serializer = serializers.ListResultSerializer(result_filter, many=True) # TODO: We should have a serializer that takes care of this automatically instead of backfilling "manually" for result in serializer.data: task = models.Task.objects.get(pk=result["task"]) result["task"] = serializers.SimpleTaskSerializer(task).data host = models.Host.objects.get(pk=result["host"]) result["host"] = serializers.SimpleHostSerializer(host).data if result["delegated_to"]: delegated_to = [ models.Host.objects.get(pk=delegated) for delegated in result["delegated_to"] ] result["delegated_to"] = serializers.SimpleHostSerializer( delegated_to, many=True).data paginated_results = self.get_paginated_response(serializer.data) if self.paginator.count > (self.paginator.offset + self.paginator.limit): max_current = self.paginator.offset + self.paginator.limit else: max_current = self.paginator.count current_page_results = "%s-%s" % (self.paginator.offset + 1, max_current) # We need to expand the search card if there is a search query, not considering pagination args search_args = [ arg for arg in request.GET.keys() if arg not in ["limit", "offset"] ] expand_search = True if search_args else False search_form = forms.ResultSearchForm(request.GET) # fmt: off return Response( dict( current_page_results=current_page_results, expand_search=expand_search, files=files.data, hosts=hosts.data, page="playbook", playbook=playbook.data, records=records.data, results=paginated_results.data, search_form=search_form, static_generation=False, ))