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
def construct_query(query, fields):
def construct_query(query, fields, results):
if not fields:
fields = settings.OPENSEARCH_MAIN_SEARCH_FIELDS
if not results:
results = 5
query = {
"size": 5,
"size": results,
"query": {
"multi_match": {
"query": query,
@ -36,8 +38,8 @@ def construct_query(query, fields):
return query
def run_main_query(client, query, fields=None):
search_query = construct_query(query, fields)
def run_main_query(client, query, fields=None, results=None):
search_query = construct_query(query, fields, results)
# fmt: off
response = client.search(body=search_query,
index=settings.OPENSEARCH_INDEX_MAIN)

View File

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

View File

@ -1,142 +1,146 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
<div class="block">
<form method="POST">
<div class="field-body">
<div class="field">
<p class="control is-expanded has-icons-left">
{% csrf_token %}
<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>
{% csrf_token %}
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Search</label>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<div class="field">
<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">
<select>
<option>day</option>
<option>week</option>
<option>month</option>
<option>year</option>
<select name="timescale">
<option value="minute">Minute</option>
<option value="hour">Hour</option>
<option value="day">Day</option>
<option value="week">Week</option>
<option value="month">Month</option>
<option value="6months">6 months</option>
</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 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 class="field-label is-normal">
<label class="label">Results</label>
</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 class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">Results</label>
</div>
<div class="field-body">
<div class="field is-narrow">
<div class="control">
<div class="select is-fullwidth">
<select name="results">
<option>5</option>
<option>10</option>
<option>25</option>
</select>
<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">
Search
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="field-body">
<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>
</form>
</div>
<div class="block">
<table class="table">
{% if results is not None %}
<thead>
<tr>
<th>TS</th>
<th>msg</th>
<th>host</th>
<th>nick</th>
<th>channel</th>
<th>net</th>
</tr>
</thead>
{% endif %}
<table class="table">
{% if results is not None %}
<thead>
<tr>
<th>TS</th>
<th>msg</th>
<th>host</th>
<th>nick</th>
<th>channel</th>
<th>net</th>
</tr>
</thead>
{% endif %}
<tbody>
{% for item in results %}
<tr>
<td>{{ item.ts }}</td>
<td>{{ item.msg }}</td>
<td>{{ item.host }}</td>
<td>{{ item.nick }}</td>
<td>{{ item.channel }}</td>
<td>{{ item.net }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<tbody>
{% for item in results %}
<tr>
<td>{{ item.ts }}</td>
<td>{{ item.msg }}</td>
<td>{{ item.host }}</td>
<td>{{ item.nick }}</td>
<td>{{ item.channel }}</td>
<td>{{ item.net }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<p>{{ card }} hits</p>
{% if results is not None %}
<p>{{ card }} hits</p>
{% endif %}
{% endblock %}

View File

@ -18,20 +18,24 @@ class Drilldown(LoginRequiredMixin, View):
def get(self, request):
if not request.user.has_plan(self.plan_name):
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):
if not request.user.has_plan(self.plan_name):
return render(request, "denied.html")
fields = None
if "fields" in request.POST:
fields = request.POST["fields"]
fields = request.POST.getlist("fields")
print("FIELD", fields)
if "results" in request.POST:
results = request.POST["results"]
print("RESULTS", results)
if "query" in request.POST:
query = request.POST["query"]
# field = results.POST["field"]
# print("FIELD ", field)
results = run_main_query(client, query, fields)
results = run_main_query(client, query, fields, results)
pp.pprint(results)
results_parsed = []
if "hits" in results.keys():