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.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.humanize',
'mwmbl', 'mwmbl',
'django_htmx', 'django_htmx',
'django_vite', 'django_vite',

View file

@ -1,4 +1,5 @@
{% load result_filters %} {% load result_filters %}
{% load humanize %}
{% include "title.html" %} {% include "title.html" %}
{% if query %} {% if query %}
{% if results %} {% if results %}
@ -28,9 +29,11 @@
</li> </li>
{% endif %} {% endif %}
{% else %} {% else %}
<li class="home"> {% for item in activity %}
<h1> <li class="activity">
Welcome to Mwmbl, the free, open-source and non-profit search engine. <h1>
</h1> {{ item.user }} made {{ item.num_curations | apnumber }} changes to <a href="{{ item.url }}">{{ item.query }}</a> {{ item.timestamp | naturaltime }}.
</li> </h1>
</li>
{% endfor %}
{% endif %} {% 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 justext
import requests import requests
from django.contrib.auth.decorators import login_required 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 django_htmx.http import push_url
from mwmbl.format import format_result from mwmbl.format import format_result
from mwmbl.models import UserCuration, MwmblUser
from mwmbl.search_setup import ranker from mwmbl.search_setup import ranker
from justext.core import html_to_dom, ParagraphMaker, classify_paragraphs, revise_paragraph_classification, \ 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): def index(request):
query = request.GET.get("q") activity, query, results = _get_results_and_activity(request)
results = ranker.search(query) if query else None
return render(request, "index.html", { return render(request, "index.html", {
"results": results, "results": results,
"query": query, "query": query,
"user": request.user, "user": request.user,
"activity": activity,
}) })
def home_fragment(request): def home_fragment(request):
query = request.GET["q"] activity, query, results = _get_results_and_activity(request)
results = ranker.search(query) response = render(request, "home.html", {
response = render(request, "home.html", {"results": results, "query": query}) "results": results,
"query": query,
"activity": activity,
})
current_url = request.htmx.current_url current_url = request.htmx.current_url
# Replace query string with new query # Replace query string with new query
stripped_url = current_url[:current_url.index("?")] if "?" in current_url else current_url stripped_url = current_url[:current_url.index("?")] if "?" in current_url else current_url
@ -65,6 +74,42 @@ def home_fragment(request):
return response 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): def fetch_url(request):
url = request.GET["url"] url = request.GET["url"]
query = request.GET["query"] query = request.GET["query"]