Implement choosing source in search form

This commit is contained in:
Mark Veidemanis 2022-08-03 23:26:22 +01:00
parent fc86aae119
commit 75e9831d1c
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
6 changed files with 82 additions and 33 deletions

View File

@ -127,7 +127,8 @@ def run_main_query(client, user, query, custom_query=False, index=None, size=Non
search_query = construct_query(query, size) search_query = construct_query(query, size)
try: try:
response = client.search(body=search_query, index=index) response = client.search(body=search_query, index=index)
except RequestError: except RequestError as err:
print("OpenSearch error", err)
return False return False
filter_blacklisted(user, response) filter_blacklisted(user, response)
return response return response
@ -141,6 +142,9 @@ def query_results(request, size=None):
results with the other data we have. results with the other data we have.
""" """
# is_anonymous = isinstance(request.user, AnonymousUser) # is_anonymous = isinstance(request.user, AnonymousUser)
message = None
message_class = None
add_bool = []
if request.user.is_anonymous: if request.user.is_anonymous:
sizes = settings.OPENSEARCH_MAIN_SIZES_ANON sizes = settings.OPENSEARCH_MAIN_SIZES_ANON
else: else:
@ -149,14 +153,39 @@ def query_results(request, size=None):
if "size" in request.POST: if "size" in request.POST:
size = request.POST["size"] size = request.POST["size"]
if size not in sizes: 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: if "query" in request.POST:
query = request.POST["query"] 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( results = run_main_query(
client, client,
request.user, # passed through run_main_query to filter_blacklisted request.user, # passed through run_main_query to filter_blacklisted
query, search_query,
custom_query=True,
size=size, size=size,
) )
if not results: if not results:
@ -208,6 +237,9 @@ def construct_query(query, size):
query = { query = {
"size": size, "size": size,
"query": { "query": {
"bool": {
"must": [
{
"query_string": { "query_string": {
"query": query, "query": query,
# "fields": fields, # "fields": fields,
@ -231,6 +263,9 @@ def construct_query(query, size):
"analyze_wildcard": False, "analyze_wildcard": False,
"auto_generate_synonyms_phrase_query": True, "auto_generate_synonyms_phrase_query": True,
} }
}
]
}
}, },
"sort": [ "sort": [
{ {

View File

@ -0,0 +1,5 @@
{% if message is not None %}
<div class="notification is-{{ class }}" hx-ext="remove-me" remove-me="3s">
{{ message }}
</div>
{% endif %}

View File

@ -65,6 +65,7 @@
<div class="control has-icons-left"> <div class="control has-icons-left">
<span class="select"> <span class="select">
<select name="source"> <select name="source">
<option selected value="all">All</option>
<option value="irc">IRC</option> <option value="irc">IRC</option>
<option value="dis">Discord</option> <option value="dis">Discord</option>
</select> </select>
@ -81,9 +82,9 @@
</div> </div>
</div> </div>
<div class="column"> <div class="column">
<div class="field has-addons"> <div class="field has-addons" id="sentiment">
<div class="control"> <div class="control">
<input id="sliderWithValue" class="slider has-output-tooltip is-fullwidth" min="-1" max="1" value="0" step="0.05" type="range"> <input disabled="undefined" name="sentiment" id="sliderWithValue" class="slider has-output-tooltip is-fullwidth" min="-1" max="1" value="0" step="0.05" type="range">
<output for="sliderWithValue" class="slider-output">0</output> <output for="sliderWithValue" class="slider-output">0</output>
<script>bulmaSlider.attach();</script> <script>bulmaSlider.attach();</script>
</div> </div>
@ -93,6 +94,11 @@
</a> </a>
</p> </p>
</div> </div>
<label class="checkbox">
<input type="checkbox" name="check-sentiment"
_="on click toggle @disabled on #sliderWithValue">
Check sentiment
</label>
</div> </div>
</div> </div>
<div class="is-hidden"></div> <div class="is-hidden"></div>

View File

@ -1,6 +1,6 @@
{% load static %} {% load static %}
{% load index %} {% load index %}
{% include 'partials/notify.html' %}
{% if results is not None %} {% if results is not None %}
<div style="display: none" id="jsonData" data-json="{{ data }}"> <div style="display: none" id="jsonData" data-json="{{ data }}">
</div> </div>

View File

@ -43,8 +43,9 @@ class DrilldownSearch(View):
# return HttpResponseForbidden() # return HttpResponseForbidden()
context = query_results(request) context = query_results(request)
if not context: if "message" in context:
return HttpResponseForbidden() return render(request, self.template_name, context)
context["data"] = json.dumps( context["data"] = json.dumps(
[ [
{ {

View File

@ -79,6 +79,8 @@ class InsightsNicks(LoginRequiredMixin, APIView):
return HttpResponse("No nick") return HttpResponse("No nick")
net = request.data["net"] net = request.data["net"]
nick = request.data["nick"] nick = request.data["nick"]
if not nick:
return HttpResponseForbidden()
nicks = get_nicks(request, net, [nick]) nicks = get_nicks(request, net, [nick])
# Filter Guest # Filter Guest
nicks = [x for x in nicks if not x.startswith("Guest")] nicks = [x for x in nicks if not x.startswith("Guest")]