diff --git a/catalog/jobs/discover.py b/catalog/jobs/discover.py index fff5d238..458bbf5e 100644 --- a/catalog/jobs/discover.py +++ b/catalog/jobs/discover.py @@ -10,7 +10,8 @@ from loguru import logger from catalog.models import * from common.models import BaseJob, JobManager -from journal.models import Comment, ShelfMember, q_item_in_category +from journal.models import Collection, Comment, ShelfMember, q_item_in_category +from users.models import APIdentity MAX_ITEMS_PER_PERIOD = 12 MIN_MARKS = settings.MIN_MARKS_FOR_DISCOVER @@ -123,6 +124,14 @@ class DiscoverGenerator(BaseJob): } ) trends.sort(key=lambda x: x["history"][0]["accounts"], reverse=True) + collection_ids = ( + Collection.objects.filter(visibility=0) + .annotate(num=Count("interactions")) + .filter(num__gte=MIN_MARKS) + .order_by("-edited_time") + .values_list("pk", flat=True)[:40] + ) cache.set(cache_key, gallery_list, timeout=None) cache.set("trends_links", trends, timeout=None) + cache.set("featured_collections", collection_ids, timeout=None) logger.info(f"Discover data updated, trends: {len(trends)}.") diff --git a/catalog/templates/discover.html b/catalog/templates/discover.html index 5f5c0aee..93f8e434 100644 --- a/catalog/templates/discover.html +++ b/catalog/templates/discover.html @@ -66,6 +66,35 @@ {% endfor %} + {% if request.user.is_authenticated %}
diff --git a/catalog/views.py b/catalog/views.py index 27d6a3c5..7160ac33 100644 --- a/catalog/views.py +++ b/catalog/views.py @@ -12,6 +12,7 @@ from django.views.decorators.http import require_http_methods from common.utils import PageLinksGenerator, get_uuid_or_404, user_identity_required from journal.models import ( + Collection, Comment, Mark, Review, @@ -21,6 +22,7 @@ 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 * @@ -291,6 +293,14 @@ def discover(request): tvshows_in_progress = [] layout = [] + collection_ids = cache.get("featured_collections", []) + if collection_ids: + i = rot * len(collection_ids) // 10 + collection_ids = collection_ids[i:] + collection_ids[:i] + featured_collections = Collection.objects.filter(pk__in=collection_ids) + else: + featured_collections = [] + return render( request, "discover.html", @@ -300,6 +310,7 @@ def discover(request): "recent_podcast_episodes": recent_podcast_episodes, "books_in_progress": books_in_progress, "tvshows_in_progress": tvshows_in_progress, + "featured_collections": featured_collections, "layout": layout, }, )