Implement a better Drilldown form

This commit is contained in:
Mark Veidemanis 2022-07-21 13:49:11 +01:00
parent 67ea926c96
commit 424f8381d3
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 145 additions and 139 deletions

View File

@ -21,11 +21,13 @@ def initialise_opensearch():
return client return client
def construct_query(query, fields): def construct_query(query, fields, results):
if not fields: if not fields:
fields = settings.OPENSEARCH_MAIN_SEARCH_FIELDS fields = settings.OPENSEARCH_MAIN_SEARCH_FIELDS
if not results:
results = 5
query = { query = {
"size": 5, "size": results,
"query": { "query": {
"multi_match": { "multi_match": {
"query": query, "query": query,
@ -36,8 +38,8 @@ def construct_query(query, fields):
return query return query
def run_main_query(client, query, fields=None): def run_main_query(client, query, fields=None, results=None):
search_query = construct_query(query, fields) search_query = construct_query(query, fields, results)
# fmt: off # fmt: off
response = client.search(body=search_query, response = client.search(body=search_query,
index=settings.OPENSEARCH_INDEX_MAIN) index=settings.OPENSEARCH_INDEX_MAIN)

View File

@ -2,12 +2,8 @@
{% for plan in plans %} {% for plan in plans %}
{% if plan in user_plans %}
<div class="box has-background-info-light">
{% else %}
<div class="box">
{% endif %}
<div class="box">
<article class="media"> <article class="media">
<div class="media-left"> <div class="media-left">
<figure class="image is-64x64"> <figure class="image is-64x64">
@ -19,7 +15,7 @@
<p> <p>
<strong>{{ plan.name }}</strong> <small>£{{ plan.cost }}</small> <strong>{{ plan.name }}</strong> <small>£{{ plan.cost }}</small>
{% if plan in user_plans %} {% if plan in user_plans %}
<i class="fas fa-check" aria-hidden="true"></i> <i class="fas fa-check" aria-hidden="true"></i>
{% endif %} {% endif %}
<br> <br>
{{ plan.description }} {{ plan.description }}
@ -28,19 +24,19 @@
<nav class="level is-mobile"> <nav class="level is-mobile">
<div class="level-left"> <div class="level-left">
{% if plan not in user_plans %} {% if plan not in user_plans %}
<a class="level-item" href="/order/{{ plan.name }}"> <a class="level-item" href="/order/{{ plan.name }}">
<span class="icon is-small has-text-success"> <span class="icon is-small has-text-success">
<i class="fas fa-plus" aria-hidden="true"></i> <i class="fas fa-plus" aria-hidden="true"></i>
</span> </span>
</a> </a>
{% endif %} {% endif %}
{% if plan in user_plans %} {% if plan in user_plans %}
<a class="level-item" href="/cancel/{{ plan.name }}"> <a class="level-item" href="/cancel/{{ plan.name }}">
<span class="icon is-small has-text-info"> <span class="icon is-small has-text-info">
<i class="fas fa-cancel" aria-hidden="true"></i> <i class="fas fa-cancel" aria-hidden="true"></i>
</span> </span>
</a> </a>
{% endif %} {% endif %}
</div> </div>
</nav> </nav>

View File

@ -1,142 +1,146 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %} {% load static %}
{% block content %} {% block content %}
<div class="block"> <div class="block">
<form method="POST"> <form method="POST">
<div class="field-body"> {% csrf_token %}
<div class="field"> <div class="field is-horizontal">
<p class="control is-expanded has-icons-left"> <div class="field-label is-normal">
{% csrf_token %} <label class="label">Search</label>
<input name="query" class="input" type="text" placeholder="Query">
<span class="icon is-small is-left">
<i class="fas fa-user"></i>
</span>
</p>
</div>
<div class="field is-horizontal">
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<div class="select is-fullwidth">
<select name="fields">
{% for field in fields %}
<option>{{ field }}</option>
{% endfor %}
</select>
</div>
</div>
</div>
</div>
</div> </div>
<div class="field-body"> <div class="field-body">
<div class="field is-narrow"> <div class="field">
<div class="control"> <div class="control is-expanded has-icons-left">
<input name="query" class="input" type="text" placeholder="Query">
<span class="icon is-small is-left">
<i class="fas fa-magnifying-glass"></i>
</span>
</div>
</div>
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Timescale</label>
</div>
<div class="field-body">
<div class="field">
<div class="control is-expanded has-icons-left">
<div class="select is-fullwidth"> <div class="select is-fullwidth">
<select> <select name="timescale">
<option>day</option> <option value="minute">Minute</option>
<option>week</option> <option value="hour">Hour</option>
<option>month</option> <option value="day">Day</option>
<option>year</option> <option value="week">Week</option>
<option value="month">Month</option>
<option value="6months">6 months</option>
</select> </select>
<span class="icon is-small is-left">
<i class="fas fa-magnifying-glass"></i>
</span>
</div>
</div>
</div>
<div class="field-label is-normal">
<label class="label">Fields</label>
</div>
<div class="field">
<div class="control is-expanded has-icons-left">
<div class="select is-fullwidth is-multiple">
<select multiple name="fields">
{% for field in fields %}
<option value="{{ field }}">{{ field }}</option>
{% endfor %}
</select>
<span class="icon is-small is-left">
<i class="fas fa-magnifying-glass"></i>
</span>
</div> </div>
</div> </div>
</div> </div>
</div> <div class="field-label is-normal">
</div> <label class="label">Results</label>
<div class="field is-horizontal">
<div class="field-body">
<div class="field is-horizontal">
<div class="field-label">
<label class="label">Search all fields</label>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<label class="radio">
<input type="radio" name="member">
Yes
</label>
<label class="radio">
<input type="radio" name="member">
No
</label>
</div> </div>
<div class="field">
<div class="control is-expanded has-icons-left">
<div class="select is-fullwidth">
<select name="results">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
<span class="icon is-small is-left">
<i class="fas fa-magnifying-glass"></i>
</span>
</div>
</div>
</div>
</div> </div>
</div> </div>
</div>
<div class="field is-horizontal">
<div class="field-label is-normal"> <div class="field is-horizontal">
<label class="label">Results</label> <div class="field-label">
</div> <!-- Left empty for spacing -->
<div class="field-body"> </div>
<div class="field is-narrow"> <div class="field-body">
<div class="control"> <div class="field">
<div class="select is-fullwidth"> <div class="control">
<select name="results">
<option>5</option> <button class="button is-primary">
<option>10</option> Search
<option>25</option> </button>
</select>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div>
</div>
</div>
<div class="field-body"> </form>
<div class="field is-horizontal">
<div class="field-label">
<!-- Left empty for spacing -->
</div>
<div class="field-body">
<div class="field">
<div class="control">
<button class="button is-primary">
Send message
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div> </div>
<div class="block"> <div class="block">
<table class="table"> <table class="table">
{% if results is not None %} {% if results is not None %}
<thead> <thead>
<tr> <tr>
<th>TS</th> <th>TS</th>
<th>msg</th> <th>msg</th>
<th>host</th> <th>host</th>
<th>nick</th> <th>nick</th>
<th>channel</th> <th>channel</th>
<th>net</th> <th>net</th>
</tr> </tr>
</thead> </thead>
{% endif %} {% endif %}
<tbody> <tbody>
{% for item in results %} {% for item in results %}
<tr> <tr>
<td>{{ item.ts }}</td> <td>{{ item.ts }}</td>
<td>{{ item.msg }}</td> <td>{{ item.msg }}</td>
<td>{{ item.host }}</td> <td>{{ item.host }}</td>
<td>{{ item.nick }}</td> <td>{{ item.nick }}</td>
<td>{{ item.channel }}</td> <td>{{ item.channel }}</td>
<td>{{ item.net }}</td> <td>{{ item.net }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
</div> </div>
<p>{{ card }} hits</p> {% if results is not None %}
<p>{{ card }} hits</p>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -18,20 +18,24 @@ class Drilldown(LoginRequiredMixin, View):
def get(self, request): def get(self, request):
if not request.user.has_plan(self.plan_name): if not request.user.has_plan(self.plan_name):
return render(request, "denied.html") return render(request, "denied.html")
return render(request, self.template_name) context = {"fields": settings.OPENSEARCH_MAIN_SEARCH_FIELDS}
return render(request, self.template_name, context)
def post(self, request): def post(self, request):
if not request.user.has_plan(self.plan_name): if not request.user.has_plan(self.plan_name):
return render(request, "denied.html") return render(request, "denied.html")
fields = None fields = None
if "fields" in request.POST: if "fields" in request.POST:
fields = request.POST["fields"] fields = request.POST.getlist("fields")
print("FIELD", fields) print("FIELD", fields)
if "results" in request.POST:
results = request.POST["results"]
print("RESULTS", results)
if "query" in request.POST: if "query" in request.POST:
query = request.POST["query"] query = request.POST["query"]
# field = results.POST["field"] # field = results.POST["field"]
# print("FIELD ", field) # print("FIELD ", field)
results = run_main_query(client, query, fields) results = run_main_query(client, query, fields, results)
pp.pprint(results) pp.pprint(results)
results_parsed = [] results_parsed = []
if "hits" in results.keys(): if "hits" in results.keys():