Properly format templates

This commit is contained in:
Mark Veidemanis 2022-08-09 11:54:44 +01:00
parent ba51922fe9
commit 220a4e2451
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
9 changed files with 122 additions and 104 deletions

View File

@ -51,10 +51,9 @@ from core.views.manage.threshold.threshold import (
) )
# Main tool pages # Main tool pages
from core.views.ui.drilldown import ( from core.views.ui.drilldown import ( # DrilldownTableView,
Drilldown, Drilldown,
DrilldownSearch, DrilldownSearch,
DrilldownTableView,
ThresholdInfoModal, ThresholdInfoModal,
) )
from core.views.ui.insights import ( from core.views.ui.insights import (

View File

@ -136,7 +136,7 @@ def run_main_query(client, user, query, custom_query=False, index=None, size=Non
response = client.search(body=search_query, index=index) response = client.search(body=search_query, index=index)
except RequestError as err: except RequestError as err:
print("OpenSearch error", err) print("OpenSearch error", err)
return False return err
filter_blacklisted(user, response) filter_blacklisted(user, response)
return response return response
@ -268,6 +268,15 @@ def query_results(request, size=None):
) )
if not results: if not results:
return False return False
if isinstance(results, RequestError):
message = results.info["error"]["root_cause"][0]["reason"]
message_class = "danger"
return {"message": message, "class": message_class}
if len(results["hits"]["hits"]) == 0:
message = "No results."
message_class = "danger"
return {"message": message, "class": message_class}
results_parsed = [] results_parsed = []
if "hits" in results.keys(): if "hits" in results.keys():
if "hits" in results["hits"]: if "hits" in results["hits"]:
@ -289,7 +298,6 @@ def query_results(request, size=None):
element["date"] = date element["date"] = date
element["time"] = time element["time"] = time
results_parsed.append(element) results_parsed.append(element)
annotate_results(results_parsed) annotate_results(results_parsed)
context = { context = {

View File

@ -1,5 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %} {% load static %}
{% load hy %}
{% block content %} {% block content %}
<script src="{% static 'js/chart.js' %}"></script> <script src="{% static 'js/chart.js' %}"></script>
<script> <script>
@ -104,7 +105,7 @@
<div class="column is-3"> <div class="column is-3">
<div class="nowrap-parent"> <div class="nowrap-parent">
<div <div
_="on click toggle .is-hidden on #options" {{ hy }} = "on click toggle .is-hidden on #options"
class="button is-light has-text-link is-right nowrap-child"> class="button is-light has-text-link is-right nowrap-child">
Options Options
</div> </div>

View File

@ -4,25 +4,24 @@
{% if results %} {% if results %}
<div style="display: none" id="jsonData" data-json="{{ data }}"> <div style="display: none" id="jsonData" data-json="{{ data }}">
</div> </div>
<div class="has-text-grey-light nowrap-parent"> <div class="has-text-grey-light nowrap-parent">
<div class="nowrap-child block"> <div class="nowrap-child block">
<i class="fa-solid fa-chart-mixed"></i> <i class="fa-solid fa-chart-mixed"></i>
</div> </div>
<div class="nowrap-child"> <div class="nowrap-child">
<p>fetched {{ results|length }} of {{ card }} hits in {{ took }}ms</p> <p>fetched {{ results|length }} of {{ card }} hits in {{ took }}ms</p>
</div> </div>
{% if exemption is not None %} {% if exemption is not None %}
<div class="nowrap-child"> <div class="nowrap-child">
<i class="fa-solid fa-book-bible"></i> <i class="fa-solid fa-book-bible"></i>
</div> </div>
{% else %} {% else %}
{% if redacted != 0 %} {% if redacted != 0 %}
<div class="nowrap-child"> <div class="nowrap-child">
<p>{{ redacted }} redacted</p> <p>{{ redacted }} redacted</p>
</div> </div>
{% endif %}
{% endif %} {% endif %}
{% endif %}
</div> </div>
<div class="box"> <div class="box">
<div style="height: 30rem"> <div style="height: 30rem">
@ -48,24 +47,23 @@
</thead> </thead>
<tbody> <tbody>
{% for item in results %} {% for item in results %}
{% if item.exemption == True %} {# Workaround for curlylint #}
<tr class="has-background-grey-lighter"> <tr class="{% if item.exemption == True %}
{% elif item.type == 'join' %} has-background-grey-lighter
<tr class="has-background-success-light"> {% elif item.type == 'join' %}
{% elif item.type == 'quit' %} has-background-success-light
<tr class="has-background-danger-light"> {% elif item.type == 'quit' %}
{% elif item.type == 'kick' %} has-background-danger-light
<tr class="has-background-danger-light"> {% elif item.type == 'kick' %}
{% elif item.type == 'part' %} has-background-danger-light
<tr class="has-background-warning-light"> {% elif item.type == 'part' %}
{% elif item.type == 'mode' %} has-background-warning-light
<tr class="has-background-info-light"> {% elif item.type == 'mode' %}
{% else %} has-background-info-light
<tr> {% endif %}">
{% endif %}
<td> <td>
<a class="has-text-link is-underlined" <a class="has-text-link is-underlined"
onclick="toggleTag('src', '{{ item.src|escapejs }}')"> onclick="toggleTag('src', '{{ item.src|escapejs }}')">
{% if item.src == 'irc' %} {% if item.src == 'irc' %}
<span class="icon" data-tooltip="IRC"> <span class="icon" data-tooltip="IRC">
<i class="fa-solid fa-hashtag" aria-hidden="true"></i> <i class="fa-solid fa-hashtag" aria-hidden="true"></i>
@ -79,7 +77,7 @@
</td> </td>
<td> <td>
<a class="has-text-link is-underlined" <a class="has-text-link is-underlined"
onclick="toggleTag('type', '{{ item.type|escapejs }}')"> onclick="toggleTag('type', '{{ item.type|escapejs }}')">
{% if item.type == 'msg' %} {% if item.type == 'msg' %}
<span class="icon" data-tooltip="Message"> <span class="icon" data-tooltip="Message">
<i class="fa-solid fa-message"></i> <i class="fa-solid fa-message"></i>
@ -124,7 +122,7 @@
<td style="max-width: 10em" class="wrap">{{ item.msg }}</td> <td style="max-width: 10em" class="wrap">{{ item.msg }}</td>
<td> <td>
<a class="has-text-link is-underlined" <a class="has-text-link is-underlined"
onclick="toggleTag('host', '{{ item.host|escapejs }}')"> onclick="toggleTag('host', '{{ item.host|escapejs }}')">
{{ item.host }} {{ item.host }}
</a> </a>
</td> </td>
@ -148,13 +146,13 @@
<a class="nowrap-child has-text-link is-underlined" onclick="toggleTag('nick', '{{ item.nick|escapejs }}')"> <a class="nowrap-child has-text-link is-underlined" onclick="toggleTag('nick', '{{ item.nick|escapejs }}')">
{{ item.nick }} {{ item.nick }}
</a> </a>
{% if item.num_chans is not None %} {% if item.num_chans is not None %}
<div class="nowrap-child"> <div class="nowrap-child">
<span class="tag"> <span class="tag">
{{ item.num_chans }} {{ item.num_chans }}
</span> </span>
</div> </div>
{% endif %} {% endif %}
</div> </div>
</td> </td>
<td> <td>
@ -173,7 +171,7 @@
<td> <td>
<div class="nowrap-parent"> <div class="nowrap-parent">
<a class="nowrap-child has-text-link is-underlined" <a class="nowrap-child has-text-link is-underlined"
onclick="toggleTag('channel', '{{ item.channel|escapejs }}')"> onclick="toggleTag('channel', '{{ item.channel|escapejs }}')">
{{ item.channel }} {{ item.channel }}
</a> </a>
{% if item.num_users is not None %} {% if item.num_users is not None %}
@ -187,7 +185,7 @@
</td> </td>
<td> <td>
<a class="has-text-link is-underlined" <a class="has-text-link is-underlined"
onclick="toggleTag('net', '{{ item.net|escapejs }}')"> onclick="toggleTag('net', '{{ item.net|escapejs }}')">
{{ item.net }} {{ item.net }}
</a> </a>
</td> </td>

View File

@ -6,23 +6,23 @@
</div> </div>
<div class="has-text-grey-light nowrap-parent"> <div class="has-text-grey-light nowrap-parent">
<div class="nowrap-child block"> <div class="nowrap-child block">
<i class="fa-solid fa-chart-mixed"></i> <i class="fa-solid fa-chart-mixed"></i>
</div> </div>
<div class="nowrap-child"> <div class="nowrap-child">
<p>fetched {{ results|length }} of {{ card }} hits in {{ took }}ms</p> <p>fetched {{ results|length }} of {{ card }} hits in {{ took }}ms</p>
</div> </div>
{% if exemption is not None %} {% if exemption is not None %}
<div class="nowrap-child"> <div class="nowrap-child">
<i class="fa-solid fa-book-bible"></i> <i class="fa-solid fa-book-bible"></i>
</div> </div>
{% else %} {% else %}
{% if redacted != 0 %} {% if redacted != 0 %}
<div class="nowrap-child"> <div class="nowrap-child">
<p>{{ redacted }} redacted</p> <p>{{ redacted }} redacted</p>
</div> </div>
{% endif %}
{% endif %} {% endif %}
{% endif %}
</div> </div>
<div class="box"> <div class="box">
<div class="table-container"> <div class="table-container">

View File

@ -6,66 +6,70 @@
{% block table.thead %} {% block table.thead %}
{% if table.show_header %} {% if table.show_header %}
<thead {{ table.attrs.thead.as_html }}> <thead {{ table.attrs.thead.as_html }}>
<tr> <tr>
{% for column in table.columns %} {% for column in table.columns %}
<th {{ column.attrs.th.as_html }} <th
hx-post="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}" {{ column.attrs.th.as_html }}
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-post="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}"
hx-trigger="click" hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-target="div.table-container" hx-trigger="click"
hx-swap="outerHTML" hx-target="div.table-container"
hx-indicator=".progress" hx-swap="outerHTML"
style="cursor: pointer;"> hx-indicator=".progress"
{{ column.header }} style="cursor: pointer;">
</th> {{ column.header }}
{% endfor %} </th>
{% endfor %}
</tr> </tr>
</thead> </thead>
{% endif %} {% endif %}
{% endblock table.thead %} {% endblock table.thead %}
{# Pagination block overrides #} {# Pagination block overrides #}
{% block pagination.previous %} {% block pagination.previous %}
<li class="previous page-item"> <li class="previous page-item">
<div hx-post="{% querystring table.prefixed_page_field=table.page.previous_page_number %}" <div
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-post="{% querystring table.prefixed_page_field=table.page.previous_page_number %}"
hx-trigger="click" hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-target="div.table-container" hx-trigger="click"
hx-swap="outerHTML" hx-target="div.table-container"
hx-indicator=".progress" hx-swap="outerHTML"
class="page-link"> hx-indicator=".progress"
<span aria-hidden="true">&laquo;</span> class="page-link">
{% trans 'previous' %} <span aria-hidden="true">&laquo;</span>
</div> {% trans 'previous' %}
</li> </div>
</li>
{% endblock pagination.previous %} {% endblock pagination.previous %}
{% block pagination.range %} {% block pagination.range %}
{% for p in table.page|table_page_range:table.paginator %} {% for p in table.page|table_page_range:table.paginator %}
<li class="page-item{% if table.page.number == p %} active{% endif %}"> <li class="page-item{% if table.page.number == p %} active{% endif %}">
<div class="page-link" <div
{% if p != '...' %}hx-post="{% querystring table.prefixed_page_field=p %}"{% endif %} class="page-link"
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' {% if p != '...' %}hx-post="{% querystring table.prefixed_page_field=p %}"{% endif %}
hx-trigger="click" hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-target="div.table-container" hx-trigger="click"
hx-swap="outerHTML" hx-target="div.table-container"
hx-indicator=".progress"> hx-swap="outerHTML"
{{ p }} hx-indicator=".progress">
</div> {{ p }}
</li> </div>
{% endfor %} </li>
{% endfor %}
{% endblock pagination.range %} {% endblock pagination.range %}
{% block pagination.next %} {% block pagination.next %}
<li class="next page-item"> <li class="next page-item">
<div hx-post="{% querystring table.prefixed_page_field=table.page.next_page_number %}" <div
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-post="{% querystring table.prefixed_page_field=table.page.next_page_number %}"
hx-trigger="click" hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-target="div.table-container" hx-trigger="click"
hx-swap="outerHTML" hx-target="div.table-container"
hx-indicator=".progress" hx-swap="outerHTML"
class="page-link"> hx-indicator=".progress"
{% trans 'next' %} class="page-link">
<span aria-hidden="true">&raquo;</span> {% trans 'next' %}
</div> <span aria-hidden="true">&raquo;</span>
</li> </div>
</li>
{% endblock pagination.next %} {% endblock pagination.next %}

8
core/templatetags/hy.py Normal file
View File

@ -0,0 +1,8 @@
from django import template
register = template.Library()
# Dirty hack to workaround curlylint not allowing _
@register.filter
def hy(user, plan_name):
return "_"

View File

@ -3,7 +3,6 @@ import json
from django.conf import settings from django.conf import settings
from django.http import HttpResponse, JsonResponse from django.http import HttpResponse, JsonResponse
from django.shortcuts import render from django.shortcuts import render
from django.utils.html import format_html
from django.views import View from django.views import View
from django_tables2 import SingleTableMixin from django_tables2 import SingleTableMixin
from rest_framework.parsers import FormParser from rest_framework.parsers import FormParser
@ -40,7 +39,7 @@ class DrilldownTableView(View, SingleTableMixin):
template_name = "ui/drilldown/table_results_partial.html" template_name = "ui/drilldown/table_results_partial.html"
if context: if context:
return render(request, self.template_name, context) return render(request, template_name, context)
else: else:
return HttpResponse("No results") return HttpResponse("No results")

View File

@ -1,4 +1,5 @@
from django_tables2 import Table, Column from django_tables2 import Column, Table
class DrilldownTable(Table): class DrilldownTable(Table):
id = Column() id = Column()