import pprint from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import render from django.views import View from core.lib.opensearch import initialise_opensearch, run_main_query client = initialise_opensearch() pp = pprint.PrettyPrinter(indent=4) class Drilldown(LoginRequiredMixin, View): template_name = "ui/drilldown.html" plan_name = "drilldown" def get(self, request): if not request.user.has_plan(self.plan_name): return render(request, "denied.html") context = { "fields": settings.OPENSEARCH_MAIN_SEARCH_FIELDS, "sizes": settings.OPENSEARCH_MAIN_SIZES, "timescales": settings.OPENSEARCH_MAIN_TIMESCALES, } return render(request, self.template_name, context) def post(self, request): if not request.user.has_plan(self.plan_name): return render(request, "denied.html") fields = None if "fields" in request.POST: fields = request.POST.getlist("fields") if "size" in request.POST: size = request.POST["size"] if "query" in request.POST: query = request.POST["query"] # field = results.POST["field"] # print("FIELD ", field) results = run_main_query(client, request.user, query, fields, size) if not results: return render(request, "denied.html") # pp.pprint(results) results_parsed = [] if "hits" in results.keys(): if "hits" in results["hits"]: for item in results["hits"]["hits"]: results_parsed.append(item["_source"]) context = { "query": query, "results": results_parsed, "card": results["hits"]["total"]["value"], "took": results["took"], "redacted": results["redacted"], "exemption": results["exemption"], "fields": settings.OPENSEARCH_MAIN_SEARCH_FIELDS, "sizes": settings.OPENSEARCH_MAIN_SIZES, "timescales": settings.OPENSEARCH_MAIN_TIMESCALES, } return render(request, self.template_name, context) return render(request, self.template_name)