popular tags

This commit is contained in:
Your Name 2024-06-03 00:14:34 -04:00 committed by Henri Dickson
parent 680df2e4ad
commit c9466ac770
5 changed files with 50 additions and 37 deletions

View file

@ -10,7 +10,13 @@ from loguru import logger
from catalog.models import *
from common.models import BaseJob, JobManager
from journal.models import Collection, Comment, ShelfMember, q_item_in_category
from journal.models import (
Collection,
Comment,
ShelfMember,
TagManager,
q_item_in_category,
)
from users.models import APIdentity
MAX_ITEMS_PER_PERIOD = 12
@ -97,7 +103,7 @@ class DiscoverGenerator(BaseJob):
}
)
item_ids = self.get_popular_marked_item_ids(category, DAYS_FOR_TRENDS, [])[
:3
:5
]
if category == ItemCategory.Podcast:
item_ids += self.get_popular_commented_podcast_ids(
@ -131,9 +137,11 @@ class DiscoverGenerator(BaseJob):
.order_by("-edited_time")
.values_list("pk", flat=True)[:40]
)
tags = TagManager.popular_tags(days=14, limit=20)
cache.set(cache_key, gallery_list, timeout=None)
cache.set("trends_links", trends, timeout=None)
cache.set("featured_collections", collection_ids, timeout=None)
cache.set("popular_tags", tags, timeout=None)
logger.info(
f"Discover data updated, trends: {len(trends)}, collections: {len(collection_ids)}."
f"Discover data updated, trends: {len(trends)}, collections: {len(collection_ids)}, tags: {len(tags)}."
)

View file

@ -147,7 +147,7 @@
{% endif %}
</div>
{% if request.user.is_authenticated %}
{% include "_sidebar.html" with show_progress=1 identity=request.user.identity %}
{% include "_sidebar.html" with identity=request.user.identity %}
{% else %}
{% include "_sidebar_anonymous.html" %}
{% endif %}

View file

@ -22,7 +22,6 @@ from journal.models import (
q_piece_in_home_feed_of_user,
q_piece_visible_to_user,
)
from users.models.apidentity import APIdentity
from .forms import *
from .models import *
@ -261,36 +260,8 @@ def discover(request):
return redirect(reverse("users:register"))
layout = request.user.preference.discover_layout
identity = request.user.identity
podcast_ids = [
p.item_id
for p in identity.shelf_manager.get_latest_members(
ShelfType.PROGRESS, ItemCategory.Podcast
)
]
recent_podcast_episodes = PodcastEpisode.objects.filter(
program_id__in=podcast_ids
).order_by("-pub_date")[:10]
books_in_progress = Edition.objects.filter(
id__in=[
p.item_id
for p in identity.shelf_manager.get_latest_members(
ShelfType.PROGRESS, ItemCategory.Book
)[:10]
]
)
tvshows_in_progress = Item.objects.filter(
id__in=[
p.item_id
for p in identity.shelf_manager.get_latest_members(
ShelfType.PROGRESS, ItemCategory.TV
)[:10]
]
)
else:
identity = None
recent_podcast_episodes = []
books_in_progress = []
tvshows_in_progress = []
layout = []
collection_ids = cache.get("featured_collections", [])
@ -301,16 +272,16 @@ def discover(request):
else:
featured_collections = []
popular_tags = cache.get("popular_tags", [])
return render(
request,
"discover.html",
{
"identity": identity,
"gallery_list": gallery_list,
"recent_podcast_episodes": recent_podcast_episodes,
"books_in_progress": books_in_progress,
"tvshows_in_progress": tvshows_in_progress,
"featured_collections": featured_collections,
"popular_tags": popular_tags,
"layout": layout,
},
)

View file

@ -22,6 +22,24 @@
</article>
</section>
{% endif %}
{% if popular_tags is not None %}
<section>
<article>
<details class="auto-collapse" open>
<summary>{% trans 'Popular Tags' %}</summary>
<div class="tag-list">
{% for t in popular_tags %}
<span>
<a href="{% url 'catalog:search' %}?tag={{ t|urlencode }}">{{ t }}</a>
</span>
{% empty %}
<div class="empty">{% trans "nothing so far." %}</div>
{% endfor %}
</div>
</details>
</article>
</section>
{% endif %}
{% if show_profile %}
<section class="profile">
<article>

View file

@ -1,10 +1,12 @@
import re
from datetime import timedelta
from functools import cached_property
from typing import TYPE_CHECKING
from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
from django.db import connection, models
from django.db.models import Avg, Count, Q
from django.db.models import Avg, Count, F, Q
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from catalog.collection.models import Collection as CatalogCollection
@ -126,6 +128,20 @@ class TagManager:
def public_tags(self):
return TagManager.all_tags_by_owner(self.owner, public_only=True)
@staticmethod
def popular_tags(days: int = 30, limit: int = 20):
t = timezone.now() - timedelta(days=days)
titles = (
TagMember.objects.filter(created_time__gt=t)
.filter(parent__visibility=0)
.annotate(title=F("parent__title"))
.values("title")
.annotate(total=Count("parent_id", distinct=True))
.order_by("-total")
.values_list("title", flat=True)[:limit]
)
return list(titles)
def get_item_tags(self, item: Item):
return sorted(
[