Show activity list when there are no queries

This commit is contained in:
Daoud Clarke 2023-11-08 09:59:29 +00:00
parent cce962e845
commit dc2bd082cf
3 changed files with 59 additions and 10 deletions

View file

@ -29,6 +29,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'mwmbl',
'django_htmx',
'django_vite',

View file

@ -1,4 +1,5 @@
{% load result_filters %}
{% load humanize %}
{% include "title.html" %}
{% if query %}
{% if results %}
@ -28,9 +29,11 @@
</li>
{% endif %}
{% else %}
<li class="home">
<h1>
Welcome to Mwmbl, the free, open-source and non-profit search engine.
</h1>
</li>
{% for item in activity %}
<li class="activity">
<h1>
{{ item.user }} made {{ item.num_curations | apnumber }} changes to <a href="{{ item.url }}">{{ item.query }}</a> {{ item.timestamp | naturaltime }}.
</h1>
</li>
{% endfor %}
{% endif %}

View file

@ -1,3 +1,8 @@
from dataclasses import dataclass
from datetime import datetime
from itertools import groupby
from urllib.parse import urlparse, parse_qs
import justext
import requests
from django.contrib.auth.decorators import login_required
@ -5,6 +10,7 @@ from django.shortcuts import render
from django_htmx.http import push_url
from mwmbl.format import format_result
from mwmbl.models import UserCuration, MwmblUser
from mwmbl.search_setup import ranker
from justext.core import html_to_dom, ParagraphMaker, classify_paragraphs, revise_paragraph_classification, \
@ -42,19 +48,22 @@ def justext_with_dom(html_text, stoplist, length_low=LENGTH_LOW_DEFAULT,
def index(request):
query = request.GET.get("q")
results = ranker.search(query) if query else None
activity, query, results = _get_results_and_activity(request)
return render(request, "index.html", {
"results": results,
"query": query,
"user": request.user,
"activity": activity,
})
def home_fragment(request):
query = request.GET["q"]
results = ranker.search(query)
response = render(request, "home.html", {"results": results, "query": query})
activity, query, results = _get_results_and_activity(request)
response = render(request, "home.html", {
"results": results,
"query": query,
"activity": activity,
})
current_url = request.htmx.current_url
# Replace query string with new query
stripped_url = current_url[:current_url.index("?")] if "?" in current_url else current_url
@ -65,6 +74,42 @@ def home_fragment(request):
return response
@dataclass
class Activity:
user: MwmblUser
num_curations: int
timestamp: datetime
query: str
url: str
def _get_results_and_activity(request):
query = request.GET.get("q")
if query:
results = ranker.search(query)
activity = None
else:
results = None
curations = UserCuration.objects.order_by("-timestamp")[:100]
sorted_curations = sorted(curations, key=lambda x: x.user.username)
groups = groupby(sorted_curations, key=lambda x: (x.user.username, x.url))
activity = []
for (user, url), group in groups:
parsed_url_query = parse_qs(urlparse(url).query)
activity_query = parsed_url_query.get("q", [""])[0]
group = list(group)
activity.append(Activity(
user=user,
num_curations=len(group),
timestamp=max([i.timestamp for i in group]),
query=activity_query,
url=url,
))
print("Activity", activity)
return activity, query, results
def fetch_url(request):
url = request.GET["url"]
query = request.GET["query"]