From 648526a6bfdec853fa2c7102afc5c030fa6f5699 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 10 Aug 2022 09:27:01 +0100 Subject: [PATCH] Implement query strings for shareable search links --- core/lib/opensearch.py | 59 +++++----- core/templates/ui/drilldown/drilldown.html | 117 ++++++++++++++++--- core/views/ui/drilldown.py | 128 +++++++++++++++------ 3 files changed, 225 insertions(+), 79 deletions(-) diff --git a/core/lib/opensearch.py b/core/lib/opensearch.py index 926ebbe..8541d81 100644 --- a/core/lib/opensearch.py +++ b/core/lib/opensearch.py @@ -141,7 +141,7 @@ def run_main_query(client, user, query, custom_query=False, index=None, size=Non return response -def query_results(request, size=None): +def query_results(request, query_params, size=None): """ API helper to alter the OpenSearch return format into something a bit better to parse. @@ -160,14 +160,14 @@ def query_results(request, size=None): else: sizes = settings.OPENSEARCH_MAIN_SIZES if not size: - if "size" in request.POST: - size = request.POST["size"] + if "size" in query_params: + size = query_params["size"] if size not in sizes: 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" in query_params: + source = query_params["source"] if source not in settings.OPENSEARCH_MAIN_SOURCES: message = "Invalid source" message_class = "danger" @@ -175,27 +175,24 @@ def query_results(request, size=None): if source != "all": add_bool.append({"src": source}) - if "dates" in request.POST: - dates = request.POST["dates"] - spl = dates.split(" - ") - if all(spl): - spl = [f"{x.replace(' ', 'T')}Z" for x in spl] - if not len(spl) == 2: - message = "Invalid dates" - message_class = "danger" - return {"message": message, "class": message_class} - from_ts, to_ts = spl - range_query = { - "range": { - "ts": { - "gt": from_ts, - "lt": to_ts, - } + if set({"from_date", "to_date", "from_time", "to_time"}).issubset( + query_params.keys() + ): + from_ts = f"{query_params['from_date']}T{query_params['from_time']}Z" + to_ts = f"{query_params['to_date']}T{query_params['to_time']}Z" + print("from ts", from_ts) + print("to_ts", to_ts) + range_query = { + "range": { + "ts": { + "gt": from_ts, + "lt": to_ts, } } - add_top.append(range_query) - if "sorting" in request.POST: - sorting = request.POST["sorting"] + } + add_top.append(range_query) + if "sorting" in query_params: + sorting = query_params["sorting"] if sorting not in ("asc", "desc", "none"): message = "Invalid sort" message_class = "danger" @@ -209,20 +206,20 @@ def query_results(request, size=None): } ] - if "check-sentiment" in request.POST: - if "sentiment-method" not in request.POST: + if "check_sentiment" in query_params: + if "sentiment_method" not in query_params: message = "No sentiment method" message_class = "danger" return {"message": message, "class": message_class} - if "sentiment" in request.POST: - sentiment = request.POST["sentiment"] + if "sentiment" in query_params: + sentiment = query_params["sentiment"] try: sentiment = float(sentiment) except ValueError: message = "Sentiment is not a float" message_class = "danger" return {"message": message, "class": message_class} - sentiment_method = request.POST["sentiment-method"] + sentiment_method = query_params["sentiment_method"] range_query_compare = {"range": {"sentiment": {}}} range_query_precise = { "match": { @@ -242,8 +239,8 @@ def query_results(request, size=None): range_query_precise["match"]["sentiment"] = 0 add_top_negative.append(range_query_precise) - if "query" in request.POST: - query = request.POST["query"] + if "query" in query_params: + query = query_params["query"] search_query = construct_query(query, size) if add_bool: for item in add_bool: diff --git a/core/templates/ui/drilldown/drilldown.html b/core/templates/ui/drilldown/drilldown.html index b46ef35..aa007ef 100644 --- a/core/templates/ui/drilldown/drilldown.html +++ b/core/templates/ui/drilldown/drilldown.html @@ -81,7 +81,7 @@ hx-post="{% url 'home' %}" hx-trigger="keyup changed delay:200ms" hx-target="#results" - hx-swap="innerHTML" id="query" name="query" class="input" type="text" placeholder="msg: science AND nick: BillNye AND channel: #science"> + hx-swap="innerHTML" id="query" name="query" value="{{ params.query }}" class="input" type="text" placeholder="msg: science AND nick: BillNye AND channel: #science"> @@ -122,7 +122,11 @@ @@ -142,9 +146,26 @@
@@ -162,8 +183,24 @@
- - 0 + + + {% if params.sentiment == None %} + 0 + {% else %} + {{ params.sentiment }} + {% endif %} +

@@ -174,25 +211,47 @@

@@ -210,7 +273,7 @@
- +