Skip to content

Commit

Permalink
Added some statistics.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurora0000 committed May 30, 2015
1 parent 36b2a58 commit ef025b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion forums/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
url(r'tags/(?P<id>[0-9]+)/$', TagDetail.as_view()),
url(r'tags/', TagList.as_view()),
url(r'users/', UserList.as_view()),
url(r'avatars/(?P<pk>[0-9]+)/$', GravatarLink.as_view())
url(r'avatars/(?P<pk>[0-9]+)/$', GravatarLink.as_view()),
url(r'stats/', ForumStats.as_view())
]

19 changes: 18 additions & 1 deletion forums/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.contrib.auth.models import User, Group
from django.dispatch import receiver
from django.db.models import Case, Count, F, Max, When
from django.db.models import Avg, Case, Count, F, Max, When
from rest_framework import generics
from rest_framework.permissions import AllowAny, DjangoObjectPermissions
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView
from rest_framework.response import Response
from guardian.shortcuts import assign_perm
from djoser.signals import user_activated

Expand All @@ -23,6 +25,21 @@ class StandardThrottle(UserRateThrottle):
rate = '60/min' # 1 per second


class ForumStats(APIView):
def get(self, request, format=None):
resp_data = {'post_count': Post.objects.count(),
'topic_count': Post.objects.filter(is_topic=True).count(),
'most_replies': Post.objects.filter(is_topic=True).annotate(reply_count=Count('replies')) \
.aggregate(Max('reply_count'))['reply_count__max'],
'user_most_posts': User.objects.annotate(post_count=Count('posts')).order_by('-post_count')[0] \
.username,
'average_post_per_user': User.objects.annotate(post_count=Count('posts')) \
.aggregate(Avg('post_count'))['post_count__avg'],
'average_replies': Post.objects.filter(is_topic=True).annotate(reply_count=Count('replies')) \
.aggregate(Avg('reply_count'))['reply_count__avg'],
}
return Response(resp_data)

class TagDetail(generics.ListAPIView):
queryset = Post.objects.all()
serializer_class = TopicSerializer
Expand Down

0 comments on commit ef025b5

Please sign in to comment.