From 6acd109ecaf1adb8bafe46c4cd9e679388efc887 Mon Sep 17 00:00:00 2001 From: Te Llamas Date: Wed, 9 Nov 2022 02:41:53 +0000 Subject: [PATCH] collection: show follows; order by follows --- books/views.py | 3 ++- collection/templates/detail.html | 3 +++ collection/views.py | 16 +++------------- games/views.py | 3 ++- movies/views.py | 3 ++- music/views.py | 6 ++++-- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/books/views.py b/books/views.py index 10d31b77..2d5d286c 100644 --- a/books/views.py +++ b/books/views.py @@ -210,7 +210,8 @@ def retrieve(request, id): review_list_more = True if len( review_list) > REVIEW_NUMBER else False review_list = review_list[:REVIEW_NUMBER] - collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, CollectionItem.objects.filter(book=book))) + all_collections = CollectionItem.objects.filter(book=book).annotate(num_marks=Count('collection__collection_marks')).order_by('-num_marks')[:20] + collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, all_collections)) # def strip_html_tags(text): # import re diff --git a/collection/templates/detail.html b/collection/templates/detail.html index d78513b9..63d2a3d9 100644 --- a/collection/templates/detail.html +++ b/collection/templates/detail.html @@ -92,6 +92,9 @@ {{ collection.title }} + {% if follower_count %} + 被 {{ follower_count }} 人关注 + {% endif %} diff --git a/collection/views.py b/collection/views.py index 45aee25b..11272e65 100644 --- a/collection/views.py +++ b/collection/views.py @@ -153,12 +153,8 @@ def retrieve(request, id): raise PermissionDenied() form = CollectionForm(instance=collection) - if request.user.is_authenticated: - following = True if CollectionMark.objects.filter(owner=request.user, collection=collection).first() is not None else False - followers = [] - else: - following = False - followers = [] + following = True if request.user.is_authenticated and CollectionMark.objects.filter(owner=request.user, collection=collection).first() is not None else False + follower_count = CollectionMark.objects.filter(collection=collection).count() return render( request, @@ -167,7 +163,7 @@ def retrieve(request, id): 'collection': collection, 'form': form, 'editable': request.user.is_authenticated and collection.is_editable_by(request.user), - 'followers': followers, + 'follower_count': follower_count, 'following': following, } ) @@ -184,10 +180,6 @@ def retrieve_entity_list(request, id): raise PermissionDenied() form = CollectionForm(instance=collection) - followers = [] - if request.user.is_authenticated: - followers = [] - return render( request, 'entity_list.html', @@ -195,8 +187,6 @@ def retrieve_entity_list(request, id): 'collection': collection, 'form': form, 'editable': request.user.is_authenticated and collection.is_editable_by(request.user), - 'followers': followers, - } ) diff --git a/games/views.py b/games/views.py index 0f0fc166..ac65c2c0 100644 --- a/games/views.py +++ b/games/views.py @@ -212,7 +212,8 @@ def retrieve(request, id): review_list_more = True if len( review_list) > REVIEW_NUMBER else False review_list = review_list[:REVIEW_NUMBER] - collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, CollectionItem.objects.filter(game=game))) + all_collections = CollectionItem.objects.filter(game=game).annotate(num_marks=Count('collection__collection_marks')).order_by('-num_marks')[:20] + collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, all_collections)) # def strip_html_tags(text): # import re diff --git a/movies/views.py b/movies/views.py index d4fc21f5..68187f18 100644 --- a/movies/views.py +++ b/movies/views.py @@ -211,7 +211,8 @@ def retrieve(request, id): review_list_more = True if len( review_list) > REVIEW_NUMBER else False review_list = review_list[:REVIEW_NUMBER] - collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, CollectionItem.objects.filter(movie=movie))) + all_collections = CollectionItem.objects.filter(movie=movie).annotate(num_marks=Count('collection__collection_marks')).order_by('-num_marks')[:20] + collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, all_collections)) # def strip_html_tags(text): # import re diff --git a/music/views.py b/music/views.py index af37c2f2..0a45c845 100644 --- a/music/views.py +++ b/music/views.py @@ -218,7 +218,8 @@ def retrieve_song(request, id): review_list_more = True if len( review_list) > REVIEW_NUMBER else False review_list = review_list[:REVIEW_NUMBER] - collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, CollectionItem.objects.filter(song=song))) + all_collections = CollectionItem.objects.filter(song=song).annotate(num_marks=Count('collection__collection_marks')).order_by('-num_marks')[:20] + collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, all_collections)) # def strip_html_tags(text): # import re @@ -780,7 +781,8 @@ def retrieve_album(request, id): review_list_more = True if len( review_list) > REVIEW_NUMBER else False review_list = review_list[:REVIEW_NUMBER] - collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, CollectionItem.objects.filter(album=album))) + all_collections = CollectionItem.objects.filter(album=album).annotate(num_marks=Count('collection__collection_marks')).order_by('-num_marks')[:20] + collection_list = filter(lambda c: c.is_visible_to(request.user), map(lambda i: i.collection, all_collections)) # def strip_html_tags(text): # import re