diff --git a/catalog/jobs/discover.py b/catalog/jobs/discover.py index 2a625ede..de5ef0d8 100644 --- a/catalog/jobs/discover.py +++ b/catalog/jobs/discover.py @@ -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)}." ) diff --git a/catalog/templates/discover.html b/catalog/templates/discover.html index 93f8e434..9d0af36a 100644 --- a/catalog/templates/discover.html +++ b/catalog/templates/discover.html @@ -147,7 +147,7 @@ {% endif %} {% 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 %} diff --git a/catalog/views.py b/catalog/views.py index 7160ac33..dcad6a43 100644 --- a/catalog/views.py +++ b/catalog/views.py @@ -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, }, ) diff --git a/common/templates/_sidebar.html b/common/templates/_sidebar.html index d63ace41..7539e623 100644 --- a/common/templates/_sidebar.html +++ b/common/templates/_sidebar.html @@ -22,6 +22,24 @@ {% endif %} + {% if popular_tags is not None %} +
+
+
+ {% trans 'Popular Tags' %} +
+ {% for t in popular_tags %} + + {{ t }} + + {% empty %} +
{% trans "nothing so far." %}
+ {% endfor %} +
+
+
+
+ {% endif %} {% if show_profile %}
diff --git a/journal/models/tag.py b/journal/models/tag.py index cb3fc6f4..b63820df 100644 --- a/journal/models/tag.py +++ b/journal/models/tag.py @@ -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( [