From 75e9831d1cef8e1360d5bb23fb09282ac03ce38c Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 3 Aug 2022 23:26:22 +0100 Subject: [PATCH] Implement choosing source in search form --- core/lib/opensearch.py | 87 +++++++++++++++------- core/templates/partials/notify.html | 5 ++ core/templates/ui/drilldown/drilldown.html | 14 +++- core/templates/ui/drilldown/results.html | 2 +- core/views/ui/drilldown.py | 5 +- core/views/ui/insights.py | 2 + 6 files changed, 82 insertions(+), 33 deletions(-) create mode 100644 core/templates/partials/notify.html diff --git a/core/lib/opensearch.py b/core/lib/opensearch.py index d054bdc..3b4402d 100644 --- a/core/lib/opensearch.py +++ b/core/lib/opensearch.py @@ -127,7 +127,8 @@ def run_main_query(client, user, query, custom_query=False, index=None, size=Non search_query = construct_query(query, size) try: response = client.search(body=search_query, index=index) - except RequestError: + except RequestError as err: + print("OpenSearch error", err) return False filter_blacklisted(user, response) return response @@ -141,6 +142,9 @@ def query_results(request, size=None): results with the other data we have. """ # is_anonymous = isinstance(request.user, AnonymousUser) + message = None + message_class = None + add_bool = [] if request.user.is_anonymous: sizes = settings.OPENSEARCH_MAIN_SIZES_ANON else: @@ -149,14 +153,39 @@ def query_results(request, size=None): if "size" in request.POST: size = request.POST["size"] if size not in sizes: - return False + message = "Size is not permitted" + message_class = "danger" + return {"message": message, "class": message_class} + if "source" in request.POST: + source = request.POST["source"] + if source not in settings.OPENSEARCH_MAIN_SOURCES: + message = "Invalid source" + message_class = "danger" + return {"message": message, "class": message_class} + if source != "all": + add_bool.append({"src": source}) + + if "check-sentiment" in request.POST: + if "sentiment" in request.POST: + sentiment = request.POST["sentiment"] + try: + sentiment = float(sentiment) + except ValueError: + message = "Sentiment is not a float" + message_class = "danger" + return {"message": message, "class": message_class} + if "query" in request.POST: query = request.POST["query"] - + search_query = construct_query(query, size) + if add_bool: + for item in add_bool: + search_query["query"]["bool"]["must"].append({"match": item}) results = run_main_query( client, request.user, # passed through run_main_query to filter_blacklisted - query, + search_query, + custom_query=True, size=size, ) if not results: @@ -208,28 +237,34 @@ def construct_query(query, size): query = { "size": size, "query": { - "query_string": { - "query": query, - # "fields": fields, - # "default_field": "msg", - # "type": "best_fields", - "fuzziness": "AUTO", - "fuzzy_transpositions": True, - "fuzzy_max_expansions": 50, - "fuzzy_prefix_length": 0, - # "minimum_should_match": 1, - "default_operator": "or", - "analyzer": "standard", - "lenient": True, - "boost": 1, - "allow_leading_wildcard": True, - # "enable_position_increments": False, - "phrase_slop": 3, - # "max_determinized_states": 10000, - "quote_field_suffix": "", - "quote_analyzer": "standard", - "analyze_wildcard": False, - "auto_generate_synonyms_phrase_query": True, + "bool": { + "must": [ + { + "query_string": { + "query": query, + # "fields": fields, + # "default_field": "msg", + # "type": "best_fields", + "fuzziness": "AUTO", + "fuzzy_transpositions": True, + "fuzzy_max_expansions": 50, + "fuzzy_prefix_length": 0, + # "minimum_should_match": 1, + "default_operator": "or", + "analyzer": "standard", + "lenient": True, + "boost": 1, + "allow_leading_wildcard": True, + # "enable_position_increments": False, + "phrase_slop": 3, + # "max_determinized_states": 10000, + "quote_field_suffix": "", + "quote_analyzer": "standard", + "analyze_wildcard": False, + "auto_generate_synonyms_phrase_query": True, + } + } + ] } }, "sort": [ diff --git a/core/templates/partials/notify.html b/core/templates/partials/notify.html new file mode 100644 index 0000000..80a3852 --- /dev/null +++ b/core/templates/partials/notify.html @@ -0,0 +1,5 @@ +{% if message is not None %} +
+ {{ message }} +
+{% endif %} \ No newline at end of file diff --git a/core/templates/ui/drilldown/drilldown.html b/core/templates/ui/drilldown/drilldown.html index 07d9afd..9d172b0 100644 --- a/core/templates/ui/drilldown/drilldown.html +++ b/core/templates/ui/drilldown/drilldown.html @@ -65,6 +65,7 @@
@@ -81,9 +82,9 @@
-
+
- + 0
@@ -93,15 +94,20 @@

+
-
+
+
-
{% endblock %} diff --git a/core/templates/ui/drilldown/results.html b/core/templates/ui/drilldown/results.html index 9b1ca21..7a93861 100644 --- a/core/templates/ui/drilldown/results.html +++ b/core/templates/ui/drilldown/results.html @@ -1,6 +1,6 @@ {% load static %} {% load index %} - +{% include 'partials/notify.html' %} {% if results is not None %} diff --git a/core/views/ui/drilldown.py b/core/views/ui/drilldown.py index c13d68f..6c7f30b 100644 --- a/core/views/ui/drilldown.py +++ b/core/views/ui/drilldown.py @@ -43,8 +43,9 @@ class DrilldownSearch(View): # return HttpResponseForbidden() context = query_results(request) - if not context: - return HttpResponseForbidden() + if "message" in context: + return render(request, self.template_name, context) + context["data"] = json.dumps( [ { diff --git a/core/views/ui/insights.py b/core/views/ui/insights.py index 1957f95..473c2d4 100644 --- a/core/views/ui/insights.py +++ b/core/views/ui/insights.py @@ -79,6 +79,8 @@ class InsightsNicks(LoginRequiredMixin, APIView): return HttpResponse("No nick") net = request.data["net"] nick = request.data["nick"] + if not nick: + return HttpResponseForbidden() nicks = get_nicks(request, net, [nick]) # Filter Guest nicks = [x for x in nicks if not x.startswith("Guest")]