serializers.py 5.16 KB
Newer Older
1
from django.contrib.auth import get_user_model
2
from django.contrib.auth.models import User
3 4

from rest_framework import serializers
5 6
from django.utils import timezone
from datetime import timedelta
7

8
from courses.models import CourseStudent, Classroom
9
from discussion.models import Comment, Topic, TopicLike, CommentLike
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

User = get_user_model()


class UserInDetailSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = sorted((
            'name',
            'cpf',
            'email',
            'institution',
            'city',
            'occupation',
Bruno Martin's avatar
Bruno Martin committed
25
            'groups',
26 27 28
            'courses',
            'date_joined',
            'last_login',
29 30 31
            'topics_created',
            'number_of_likes',
            'comments_created',
32 33
        ))

34 35 36
    comments_created = serializers.SerializerMethodField()
    topics_created = serializers.SerializerMethodField()
    number_of_likes = serializers.SerializerMethodField()
37
    courses = serializers.SerializerMethodField()
Bruno Martin's avatar
Bruno Martin committed
38
    groups = serializers.SerializerMethodField()
39 40 41 42 43 44 45

    def get_comments_created(self, obj):
        return obj.comment_author.count()

    def get_courses(self, obj):
        needed_stuff = [
            {'percent_progress': x.percent_progress(),
46
             'course_finished':  x.can_emmit_receipt(),
47 48
             'course_name': x.course.name,
             # FIXME
49
             #  'has_certificate': x.certificate.document_type == 'certificate',
50 51
             'has_certificate': False,
             'class_name': x.get_current_class().name
Bruno Martin's avatar
Bruno Martin committed
52
            } for x in obj.coursestudent_set.all()]
53 54 55 56 57 58 59 60
        return needed_stuff

    def get_topics_created(self, obj):
        return obj.topic_author.count()

    def get_number_of_likes(self, obj):
        return obj.topiclike_set.count() + obj.commentlike_set.count()

Bruno Martin's avatar
Bruno Martin committed
61 62 63
    def get_groups(self, obj):
        return ', '.join([group.name for group in obj.groups.all()])

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

class UsersByClassSerializer(serializers.Serializer):
    class Meta:
        model = CourseStudent
        fields = sorted((
            'name',
            'cpf',
            'email',
            'last_login',
            # 'percent_progress_by_lesson',
            'percent_progress',
            'course_finished',
            'class_name',
            'city',
            'occupation',
            # 'institution',
            # 'has_certificate',
Bruno Martin's avatar
Bruno Martin committed
81
            'groups',
82 83 84 85 86 87 88 89
        ))

    cpf = serializers.SerializerMethodField()
    email = serializers.SerializerMethodField()
    last_login = serializers.SerializerMethodField()
    # has_certificate = serializers.SerializerMethodField()
    percent_progress_by_lesson = serializers.SerializerMethodField()
    percent_progress = serializers.SerializerMethodField()
90
    course_finished = serializers.SerializerMethodField()
91 92 93 94
    class_name = serializers.SerializerMethodField()
    city = serializers.SerializerMethodField()
    occupation = serializers.SerializerMethodField()
    # institution = serializers.SerializerMethodField()
Bruno Martin's avatar
Bruno Martin committed
95
    groups = serializers.SerializerMethodField()
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    def get_city(self, obj):
        return obj.user.city

    def get_occupation(self, obj):
        return obj.user.occupation

    def get_institution(self, obj):
        return obj.user.institution

    def get_cpf(self, obj):
        return obj.user.cpf

    def get_email(self, obj):
        return obj.user.email

    def get_last_login(self, obj):
        return obj.user.last_login

    # def get_has_certificate(self, obj):
116
    #     return obj.certificate.document_type == 'certificate'
117 118 119 120 121 122 123 124 125 126 127 128

    def get_percent_progress_by_lesson(self, obj):
        return obj.percent_progress_by_lesson()

    def get_percent_progress(self, obj):
        return obj.percent_progress()

    def get_course_finished(self, obj):
        return obj.can_emmit_receipt()

    def get_class_name(self, obj):
        return obj.get_current_class().name
Bruno Martin's avatar
Bruno Martin committed
129 130 131

    def get_groups(self, obj):
        return ', '.join([group.name for group in obj.user.groups.all()])
132 133


134
class UserStatsSerializer(serializers.ModelSerializer):
135 136
    last_access = serializers.SerializerMethodField()
    accesses_count = serializers.SerializerMethodField()
Matheus Miranda's avatar
Matheus Miranda committed
137
    topics = serializers.SerializerMethodField()
138 139
    reactions = serializers.SerializerMethodField()
    comments = serializers.SerializerMethodField()
140

141 142
    class Meta:
        model = User
143 144
        fields = ('name', 'username', 'email', 'id', 'image', 'last_access',
                  'accesses_count', 'topics', 'reactions', 'comments')
145 146

    def get_last_access(self, obj):
Matheus Miranda's avatar
Matheus Miranda committed
147 148
        if obj.last_login:
            timestamp_str = obj.last_login.strftime('%d-%m-%Y %H:%M:%S')
149 150 151
            date, hour = timestamp_str.split()
            return date + ' às ' + hour
        return ''
152 153 154

    def get_accesses_count(self, obj):
        time_delta = timezone.now() - timedelta(days=7)
155
        return obj.actor_actions.filter(verb='access', timestamp__gte=time_delta).count()
156

Matheus Miranda's avatar
Matheus Miranda committed
157
    def get_topics(self, obj):
158 159 160 161 162 163 164
        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()