Begin implementing modern tables

This commit is contained in:
Mark Veidemanis 2022-08-09 09:04:31 +01:00
parent 7e010bd9b8
commit ba51922fe9
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
10 changed files with 183 additions and 2 deletions

View File

@ -35,11 +35,15 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"django_htmx",
"crispy_forms", "crispy_forms",
"crispy_bulma", "crispy_bulma",
"django_tables2",
"django_tables2_bulma_template",
] ]
CRISPY_TEMPLATE_PACK = "bulma" CRISPY_TEMPLATE_PACK = "bulma"
CRISPY_ALLOWED_TEMPLATE_PACKS = ("bulma",) CRISPY_ALLOWED_TEMPLATE_PACKS = ("bulma",)
DJANGO_TABLES2_TEMPLATE = "django-tables2/bulma.html"
MIDDLEWARE = [ MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware", "django.middleware.security.SecurityMiddleware",
@ -49,6 +53,7 @@ MIDDLEWARE = [
"django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware", "django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_htmx.middleware.HtmxMiddleware",
] ]
ROOT_URLCONF = "app.urls" ROOT_URLCONF = "app.urls"

View File

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

View File

@ -0,0 +1,32 @@
{% load static %}
{% load index %}
{% include 'partials/notify.html' %}
{% if table %}
<div style="display: none" id="jsonData" data-json="{{ data }}">
</div>
<div class="has-text-grey-light nowrap-parent">
<div class="nowrap-child block">
<i class="fa-solid fa-chart-mixed"></i>
</div>
<div class="nowrap-child">
<p>fetched {{ results|length }} of {{ card }} hits in {{ took }}ms</p>
</div>
{% if exemption is not None %}
<div class="nowrap-child">
<i class="fa-solid fa-book-bible"></i>
</div>
{% else %}
{% if redacted != 0 %}
<div class="nowrap-child">
<p>{{ redacted }} redacted</p>
</div>
{% endif %}
{% endif %}
</div>
<div class="box">
<div class="table-container">
{% include 'ui/drilldown/table_results_partial.html' %}
</div>
</div>
{% endif %}

View File

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

View File

View File

@ -3,7 +3,9 @@ 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 rest_framework.parsers import FormParser from rest_framework.parsers import FormParser
from rest_framework.views import APIView from rest_framework.views import APIView
@ -14,6 +16,33 @@ from core.lib.threshold import (
get_chans, get_chans,
get_users, get_users,
) )
from core.views.ui.tables import DrilldownTable
class DrilldownTableView(View, SingleTableMixin):
table_class = DrilldownTable
template_name = "ui/drilldown/table_results.html"
paginate_by = 5
def post(self, request):
context = query_results(request)
table = DrilldownTable(context["results"])
context["table"] = table
del context["results"]
if "message" in context:
return render(request, self.template_name, context)
if self.request.htmx:
print("IS HTMX")
template_name = "ui/drilldown/table_results.html"
else:
print("IS NOT HTMX")
template_name = "ui/drilldown/table_results_partial.html"
if context:
return render(request, self.template_name, context)
else:
return HttpResponse("No results")
class Drilldown(View): class Drilldown(View):

33
core/views/ui/tables.py Normal file
View File

@ -0,0 +1,33 @@
from django_tables2 import Table, Column
class DrilldownTable(Table):
id = Column()
host = Column()
ident = Column()
nick = Column()
nick_id = Column()
user_id = Column()
msg = Column()
msg_id = Column()
net = Column()
net_id = Column()
num = Column()
src = Column()
ts = Column()
type = Column()
bot = Column()
channel = Column()
channel_category = Column()
channel_category_id = Column()
channel_category_nsfw = Column()
channel_id = Column()
channel_nsfw = Column()
guild_member_count = Column()
guild = Column()
guild_id = Column()
mode = Column()
modearg = Column()
sentiment = Column()
status = Column()
user = Column()
version_sentiment = Column()

View File

@ -7,4 +7,5 @@ stripe
django-rest-framework django-rest-framework
numpy numpy
uwsgi uwsgi
pyroscope-io django-tables2
django-tables2-bulma-template

View File

@ -6,3 +6,6 @@ opensearch-py
stripe stripe
django-rest-framework django-rest-framework
numpy numpy
django-tables2
django-tables2-bulma-template
django-htmx

View File

@ -7,3 +7,5 @@ opensearch-py
stripe stripe
django-rest-framework django-rest-framework
numpy numpy
django-tables2
django-tables2-bulma-template