From 8da84dffe08b3d8c8a629bbbb8b8daed530dec87 Mon Sep 17 00:00:00 2001 From: Bruno Martin Date: Sat, 2 May 2020 01:07:34 -0300 Subject: [PATCH 1/2] add filter horizontal to groups and category in forum admin --- discussion/admin.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/discussion/admin.py b/discussion/admin.py index 75fd478..e44e76b 100644 --- a/discussion/admin.py +++ b/discussion/admin.py @@ -2,12 +2,18 @@ from discussion.models import Category, Forum, Topic, Comment, Tag, TopicNotification +class ForumAdmin(admin.ModelAdmin): + filter_horizontal = [ + 'groups', + 'category', + ] + class TopicAdmin(admin.ModelAdmin): search_fields = ['title', 'content'] admin.site.register(Category) -admin.site.register(Forum) +admin.site.register(Forum, ForumAdmin) admin.site.register(Topic, TopicAdmin) admin.site.register(Comment) admin.site.register(Tag) From 2eb1dcba9eb17e9fc22b7d27c13d2cb0aebca6cd Mon Sep 17 00:00:00 2001 From: Bruno Martin Date: Mon, 4 May 2020 10:30:27 -0300 Subject: [PATCH 2/2] performance improvements in forum viewset --- discussion/serializers.py | 52 ++++++++++++++++++++++++++++++++++++++- discussion/views.py | 8 +++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/discussion/serializers.py b/discussion/serializers.py index 3096f06..5a5e0ab 100644 --- a/discussion/serializers.py +++ b/discussion/serializers.py @@ -8,7 +8,13 @@ class BaseUserSerializer(serializers.ModelSerializer): class Meta: model = get_user_model() - exclude = ('password',) + fields = ( + 'id', + 'name', + 'first_name', + 'last_name', + 'username', + ) class CategorySerializer(serializers.ModelSerializer): @@ -69,6 +75,9 @@ class Meta: depth = 1 def get_latest_topics(self, obj): + queryset = Topic.objects.filter(forum=obj).order_by('-last_activity_at')[:5] + queryset = queryset.select_related('author') + queryset = queryset.prefetch_related('categories', 'tags', 'forum') return BaseTopicSerializer(Topic.objects.filter(forum=obj).order_by('-last_activity_at')[:5], many=True, **{'context': self.context}).data def get_topics(self, obj): @@ -82,6 +91,47 @@ def get_topics(self, obj): if tags: queryset = queryset.filter(tags__id__in=tags) + queryset = queryset.select_related('author') + queryset = queryset.prefetch_related('categories', 'tags', 'forum') + + # only exec the query if any filter is present + if categories or tags: + return BaseTopicSerializer(queryset.order_by('-last_activity_at'), many=True, **{'context': self.context}).data + else: + return BaseTopicSerializer(queryset.order_by('-last_activity_at')[:5], many=True, **{'context': self.context}).data + + +class ForumSumarySerializer(serializers.ModelSerializer): + + author = BaseUserSerializer(read_only=True) + latest_topics = serializers.SerializerMethodField() + topics = serializers.SerializerMethodField() + + class Meta: + model = Forum + fields = ('id', 'title', 'text', 'slug', 'timestamp', 'is_public', 'author', 'category', 'latest_topics', 'forum_type', 'topics', ) + depth = 1 + + def get_latest_topics(self, obj): + queryset = Topic.objects.filter(forum=obj).order_by('-last_activity_at')[:5] + queryset = queryset.select_related('author') + queryset = queryset.prefetch_related('categories', 'tags', 'forum') + return BaseTopicSerializer(queryset , many=True, **{'context': self.context}).data + + def get_topics(self, obj): + request = self.context.get("request") + if request: + queryset = Topic.objects.filter(forum=obj) + categories = request.query_params.getlist('categories', None) + if categories: + queryset = queryset.filter(categories__id__in=categories) + tags = request.query_params.getlist('tags', None) + if tags: + queryset = queryset.filter(tags__id__in=tags) + + queryset = queryset.select_related('author') + queryset = queryset.prefetch_related('categories', 'tags', 'forum') + # only exec the query if any filter is present if categories or tags: return BaseTopicSerializer(queryset.order_by('-last_activity_at'), many=True, **{'context': self.context}).data diff --git a/discussion/views.py b/discussion/views.py index aa38b9f..fe2a6ae 100644 --- a/discussion/views.py +++ b/discussion/views.py @@ -102,7 +102,9 @@ def get_queryset(self): if not self.request.user.is_superuser: queryset = queryset.filter(Q(is_public=True) | Q(groups__in=self.request.user.groups.all())) - return queryset.distinct() + queryset = queryset.distinct() + queryset = queryset.select_related('author') + return queryset.prefetch_related('topics', 'category') def perform_create(self, serializer): serializer.save(author=self.request.user) @@ -111,7 +113,7 @@ def perform_update(self, serializer): serializer.save(author=self.request.user) -class ForumSearchViewSet(viewsets.ModelViewSet): +class ForumSearchViewSet(viewsets.ReadOnlyModelViewSet): """ """ @@ -123,7 +125,7 @@ class ForumSearchViewSet(viewsets.ModelViewSet): # search_fields = ('title', 'text', 'topics__title', 'topics__content', 'topics__comment__text', ) -class TopicTypeaheadViewSet(viewsets.ModelViewSet): +class TopicTypeaheadViewSet(viewsets.ReadOnlyModelViewSet): """ """