Skip to content

Commit

Permalink
Merge pull request #16 from hacklabr/develop
Browse files Browse the repository at this point in the history
Develop into master
  • Loading branch information
frclba authored May 4, 2020
2 parents 716c286 + 2eb1dcb commit 2040917
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
8 changes: 7 additions & 1 deletion discussion/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 51 additions & 1 deletion discussion/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions discussion/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -111,7 +113,7 @@ def perform_update(self, serializer):
serializer.save(author=self.request.user)


class ForumSearchViewSet(viewsets.ModelViewSet):
class ForumSearchViewSet(viewsets.ReadOnlyModelViewSet):
"""
"""

Expand All @@ -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):
"""
"""

Expand Down

0 comments on commit 2040917

Please sign in to comment.