Commit 120f1d56 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Add interaction info to UserStatsSerializer

parent e69dc69a
......@@ -6,6 +6,7 @@ from django.utils import timezone
from datetime import timedelta
from courses.models import CourseStudent
from discussion.models import Comment, Topic, TopicLike, CommentLike
User = get_user_model()
......@@ -133,11 +134,15 @@ class UsersByClassSerializer(serializers.Serializer):
class UserStatsSerializer(serializers.ModelSerializer):
last_access = serializers.SerializerMethodField()
accesses_count = serializers.SerializerMethodField()
created_topics = serializers.SerializerMethodField()
reactions = serializers.SerializerMethodField()
comments = serializers.SerializerMethodField()
class Meta:
model = User
fields = ('name', 'username', 'email', 'id', 'image', 'groups',
'last_access', 'accesses_count')
'last_access', 'accesses_count', 'created_topics', 'reactions',
'comments')
def get_last_access(self, obj):
count = obj.actor_actions.count()
......@@ -146,3 +151,13 @@ class UserStatsSerializer(serializers.ModelSerializer):
def get_accesses_count(self, obj):
time_delta = timezone.now() - timedelta(days=7)
return obj.actor_actions.filter(timestamp__gte=time_delta).count()
def get_created_topics(self, obj):
return Topic.objects.filter(author=obj).count()
def get_reactions(self, obj):
return TopicLike.objects.filter(user=obj).count() + CommentLike.objects.filter(user=obj).count()
def get_comments(self, obj):
return Comment.objects.filter(author=obj).count()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment