Begin implementing smarter WM system for multi-type objects
This commit is contained in:
@@ -6,7 +6,6 @@ from django.conf import settings
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse
|
||||
from django.views import View
|
||||
from django_tables2 import SingleTableView
|
||||
from rest_framework.parsers import FormParser
|
||||
from rest_framework.views import APIView
|
||||
@@ -215,21 +214,132 @@ def drilldown_search(request, return_context=False, template=None):
|
||||
|
||||
class DrilldownTableView(SingleTableView):
|
||||
table_class = DrilldownTable
|
||||
template_name = "widgets/table_results.html"
|
||||
template_name = "wm/widget.html"
|
||||
window_content = "window-content/results.html"
|
||||
# htmx_partial = "partials/"
|
||||
paginate_by = settings.DRILLDOWN_RESULTS_PER_PAGE
|
||||
|
||||
def get_queryset(self, request, **kwargs):
|
||||
context = drilldown_search(request, return_context=True)
|
||||
# Save the context as we will need to merge other attributes later
|
||||
self.context = context
|
||||
def common_request(self, request, **kwargs):
|
||||
extra_params = {}
|
||||
|
||||
if "object_list" in context:
|
||||
return context["object_list"]
|
||||
if request.user.is_anonymous:
|
||||
sizes = settings.MAIN_SIZES_ANON
|
||||
else:
|
||||
return []
|
||||
sizes = settings.MAIN_SIZES
|
||||
|
||||
if request.GET:
|
||||
self.template_name = "index.html"
|
||||
# GET arguments in URL like ?query=xyz
|
||||
query_params = request.GET.dict()
|
||||
elif request.POST:
|
||||
query_params = request.POST.dict()
|
||||
else:
|
||||
self.template_name = "index.html"
|
||||
# No query, this is a fresh page load
|
||||
# Don't try to search, since there's clearly nothing to do
|
||||
params_with_defaults = {}
|
||||
helpers.add_defaults(params_with_defaults)
|
||||
context = {
|
||||
"sizes": sizes,
|
||||
"params": params_with_defaults,
|
||||
"unique": "results",
|
||||
"window_content": self.window_content,
|
||||
"title": "Results",
|
||||
}
|
||||
return render(request, self.template_name, context)
|
||||
|
||||
# Merge everything together just in case
|
||||
tmp_post = request.POST.dict()
|
||||
tmp_get = request.GET.dict()
|
||||
tmp_post = {k: v for k, v in tmp_post.items() if v and not v == "None"}
|
||||
tmp_get = {k: v for k, v in tmp_get.items() if v and not v == "None"}
|
||||
query_params.update(tmp_post)
|
||||
query_params.update(tmp_get)
|
||||
|
||||
# URI we're passing to the template for linking
|
||||
if "csrfmiddlewaretoken" in query_params:
|
||||
del query_params["csrfmiddlewaretoken"]
|
||||
|
||||
# Parse the dates
|
||||
if "dates" in query_params:
|
||||
dates = parse_dates(query_params["dates"])
|
||||
del query_params["dates"]
|
||||
if dates:
|
||||
if "message" in dates:
|
||||
return render(request, self.template_name, dates)
|
||||
query_params["from_date"] = dates["from_date"]
|
||||
query_params["to_date"] = dates["to_date"]
|
||||
query_params["from_time"] = dates["from_time"]
|
||||
query_params["to_time"] = dates["to_time"]
|
||||
|
||||
# Remove null values
|
||||
if "query" in query_params:
|
||||
if query_params["query"] == "":
|
||||
del query_params["query"]
|
||||
|
||||
# Remove null tags values
|
||||
if "tags" in query_params:
|
||||
if query_params["tags"] == "":
|
||||
del query_params["tags"]
|
||||
else:
|
||||
# Parse the tags and populate cast to pass to search function
|
||||
tags = parse_tags(query_params["tags"])
|
||||
extra_params["tags"] = tags
|
||||
|
||||
context = db.query_results(request, query_params, **extra_params)
|
||||
# Unique is for identifying the widgets.
|
||||
# We don't want a random one since we only want one results pane.
|
||||
context["unique"] = "results"
|
||||
context["window_content"] = self.window_content
|
||||
context["title"] = "Results"
|
||||
|
||||
# Valid sizes
|
||||
context["sizes"] = sizes
|
||||
|
||||
# Add any default parameters to the context
|
||||
params_with_defaults = dict(query_params)
|
||||
helpers.add_defaults(params_with_defaults)
|
||||
context["params"] = params_with_defaults
|
||||
|
||||
# Remove anything that we or the user set to a default for
|
||||
# pretty URLs
|
||||
helpers.remove_defaults(query_params)
|
||||
url_params = urllib.parse.urlencode(query_params)
|
||||
context["client_uri"] = url_params
|
||||
|
||||
# There's an error
|
||||
if "message" in context:
|
||||
response = render(request, self.template_name, context)
|
||||
# Still push the URL so they can share it to get assistance
|
||||
if request.GET:
|
||||
if request.htmx:
|
||||
response["HX-Push"] = reverse("home") + "?" + url_params
|
||||
elif request.POST:
|
||||
response["HX-Push"] = reverse("home") + "?" + url_params
|
||||
return response
|
||||
|
||||
# Create data for chart.js sentiment graph
|
||||
graph = make_graph(context["object_list"])
|
||||
context["data"] = graph
|
||||
|
||||
# Create the table
|
||||
context = make_table(context)
|
||||
|
||||
# URI we're passing to the template for linking, table fields removed
|
||||
table_fields = ["page", "sort"]
|
||||
clean_params = {k: v for k, v in query_params.items() if k not in table_fields}
|
||||
clean_url_params = urllib.parse.urlencode(clean_params)
|
||||
context["uri"] = clean_url_params
|
||||
|
||||
# unique = str(uuid.uuid4())[:8]
|
||||
# self.context = context
|
||||
return context
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
self.object_list = self.get_queryset(request)
|
||||
self.context = self.common_request(request)
|
||||
if isinstance(self.context, HttpResponse):
|
||||
return self.context
|
||||
self.object_list = self.context["object_list"]
|
||||
show = []
|
||||
show = set().union(*(d.keys() for d in self.object_list))
|
||||
allow_empty = self.get_allow_empty()
|
||||
@@ -245,17 +355,17 @@ class DrilldownTableView(SingleTableView):
|
||||
else:
|
||||
is_empty = not self.object_list # noqa
|
||||
context = self.get_context_data()
|
||||
if isinstance(self.context, HttpResponse):
|
||||
return self.context
|
||||
|
||||
for k, v in self.context.items():
|
||||
if k not in context:
|
||||
context[k] = v
|
||||
context["show"] = show
|
||||
|
||||
if request.method == "GET":
|
||||
if not request.htmx:
|
||||
self.template_name = "ui/drilldown/drilldown.html"
|
||||
# if request.htmx:
|
||||
# self.template_name = self.window_content
|
||||
# if request.method == "GET":
|
||||
# if not request.htmx:
|
||||
# self.template_name = "ui/drilldown/drilldown.html"
|
||||
response = self.render_to_response(context)
|
||||
# if not request.method == "GET":
|
||||
if "client_uri" in context:
|
||||
@@ -266,15 +376,15 @@ class DrilldownTableView(SingleTableView):
|
||||
return self.get(request, *args, **kwargs)
|
||||
|
||||
|
||||
class Drilldown(View):
|
||||
template_name = "ui/drilldown/drilldown.html"
|
||||
plan_name = "drilldown"
|
||||
# class Drilldown(View):
|
||||
# template_name = "ui/drilldown/drilldown.html"
|
||||
# plan_name = "drilldown"
|
||||
|
||||
def get(self, request):
|
||||
return drilldown_search(request)
|
||||
# def get(self, request):
|
||||
# return drilldown_search(request)
|
||||
|
||||
def post(self, request):
|
||||
return drilldown_search(request)
|
||||
# def post(self, request):
|
||||
# return drilldown_search(request)
|
||||
|
||||
|
||||
class DrilldownContextModal(APIView):
|
||||
@@ -377,7 +487,6 @@ class DrilldownContextModal(APIView):
|
||||
type=type,
|
||||
nicks=nicks_sensitive,
|
||||
)
|
||||
print("QUERY", search_query)
|
||||
results = db.query_results(
|
||||
request,
|
||||
query_params,
|
||||
@@ -437,45 +546,18 @@ class ThresholdInfoModal(APIView):
|
||||
nick = request.data["nick"]
|
||||
channel = request.data["channel"]
|
||||
|
||||
# SAFE BLOCK #
|
||||
# Lookup the hash values but don't disclose them to the user
|
||||
# if settings.HASHING:
|
||||
# SAFE_PARAMS = request.data.dict()
|
||||
# hash_lookup(request.user, SAFE_PARAMS)
|
||||
|
||||
channels = get_chans(net, [nick])
|
||||
print("CHANNELS", channels)
|
||||
users = get_users(net, [nick])
|
||||
print("USERS", users)
|
||||
num_users = annotate_num_users(net, channels)
|
||||
print("NUM_USERS", num_users)
|
||||
num_chans = annotate_num_chans(net, users)
|
||||
print("NUM_CHANS", num_chans)
|
||||
if channels:
|
||||
inter_users = get_users(net, channels)
|
||||
else:
|
||||
inter_users = []
|
||||
print("INTER_USERS", inter_users)
|
||||
if users:
|
||||
inter_chans = get_chans(net, users)
|
||||
else:
|
||||
inter_chans = []
|
||||
print("INTER_CHANS", inter_chans)
|
||||
# if settings.HASHING:
|
||||
# hash_list(request.user, inter_chans)
|
||||
# hash_list(request.user, inter_users)
|
||||
|
||||
# hash_list(request.user, num_chans, hash_keys=True)
|
||||
# hash_list(request.user, num_users, hash_keys=True)
|
||||
|
||||
# hash_list(request.user, channels)
|
||||
# hash_list(request.user, users)
|
||||
|
||||
# if settings.RANDOMISATION:
|
||||
# randomise_list(request.user, num_chans)
|
||||
# randomise_list(request.user, num_users)
|
||||
|
||||
# SAFE BLOCK END #
|
||||
|
||||
unique = str(uuid.uuid4())[:8]
|
||||
context = {
|
||||
@@ -490,5 +572,4 @@ class ThresholdInfoModal(APIView):
|
||||
"num_chans": num_chans,
|
||||
"unique": unique,
|
||||
}
|
||||
print("CON", context)
|
||||
return render(request, self.template_name, context)
|
||||
|
||||
Reference in New Issue
Block a user