serializers.py 5.21 KB
Newer Older
1
from rest_framework import serializers
2 3
from actstream.models import Action
from courses.reports.serializers import UserStatsSerializer
4
from courses.serializers import SimpleUserSerializer
5 6 7 8 9

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
User = get_user_model()

Matheus Miranda's avatar
Matheus Miranda committed
10
from courses.models import Course
11
from courses.stats.models import AccessibleArea
12 13
from courses.classroom.models import Classroom
from courses.classroom.serializers import BasicClassroomSerializer
14

Matheus Miranda's avatar
Matheus Miranda committed
15
from discussion.models import Comment, Topic, Forum
16

17 18
from cards.models import Card

19 20
from courses_learning_objects.models import LearningObject, Answer

21 22
from datetime import datetime

23 24 25 26 27 28
class UserActionsSerializer(serializers.Serializer):
    verb = serializers.CharField(required=True, max_length=100)
    action_object_id = serializers.IntegerField(required=False)
    action_object_type = serializers.CharField(required=False, max_length=100)
    target_id = serializers.IntegerField(required=False)
    target_type = serializers.CharField(required=False, max_length=100)
29 30 31 32


class UserAccessSerializer(serializers.Serializer):
    area = serializers.CharField(required=True, max_length=100)
33 34
    target_id = serializers.IntegerField(required=False)
    target_type = serializers.CharField(required=False, max_length=100)
35 36 37 38 39


class GenericRelatedField(serializers.Field):
    def to_representation(self, value):
        if isinstance(value, User):
40
            return SimpleUserSerializer(value).data
41 42 43 44 45 46 47
        if isinstance(value, Classroom):
            return BasicClassroomSerializer(value).data
        return str(value)


class ActionSerializer(serializers.ModelSerializer):
    actor = GenericRelatedField(read_only=True)
48
    target = serializers.SerializerMethodField()
49 50
    action_object = GenericRelatedField(read_only=True)
    verb = GenericRelatedField(read_only=True)
51
    action_phrase = serializers.SerializerMethodField()
52
    complements = serializers.SerializerMethodField()
53
    timestamp = serializers.SerializerMethodField()
54 55 56

    class Meta:
        model = Action
57
        fields = ('actor', 'target', 'action_object', 'verb', 'action_phrase', 'timestamp', 'complements')
58

59 60
    def get_target(self, obj):
        if isinstance(obj.target, LearningObject):
61 62
            if obj.target.unit:
                return obj.target.unit.lesson.course.name
63 64
        if isinstance(obj.target, Forum):
            return obj.target.title
65 66
        if isinstance(obj.target, Classroom):
            return obj.target.name
67 68
        return obj.target

69
    def get_timestamp(self, obj):
70 71 72 73
        from django.utils import timezone
        tz_str = timezone.localtime(obj.timestamp)
        date, hour = tz_str.strftime('%d-%m-%Y %H:%M:%S').split()
        return '{} às {}'.format(date, hour)
74

75 76
    def get_accessible_area_name(self, name):
        accessible_areas = {
77 78 79 80 81 82 83 84
            'Dashboard': 'a Página Inicial',
            'Chat Main Page': 'os Chats',
            'Profile Page': 'a Página de perfil',
            'Notes': 'as Anotações',
            'Classrooms': 'as Salas',
            'Courses': 'a Lista de Espaços Formativos',
            'Messages': 'os Avisos',
            'Forums': 'os Fóruns',
85
            'Cards List': 'a Lista de Conteúdos'
86
        }
87
        return accessible_areas.get(name, name)
88

89
    def get_object_type(self, obj):
Matheus Miranda's avatar
Matheus Miranda committed
90
        if isinstance(obj, Course):
91
            return ' no curso {}'.format(obj.name)
92
        if isinstance(obj, Classroom):
93
            return ' na sala {}'.format(obj.name)
94
        if isinstance(obj, Topic):
95
            return ' no tópico {}'.format(obj.title)
Matheus Miranda's avatar
Matheus Miranda committed
96
        if isinstance(obj, Forum):
97
            return ' no fórum {}'.format(obj.title)
98
        if isinstance(obj, AccessibleArea):
99
            return ' {}'.format(self.get_accessible_area_name(obj.name))
100
        if isinstance(obj, LearningObject):
101 102 103 104 105
            if obj.unit:
                unit = obj.unit.title
                step = obj.unit.lesson.name
                course = obj.unit.lesson.course.name
                return ' a atividade {} na etapa {} do curso {}'.format(unit, step, course)
106 107
        if isinstance(obj, Card):
           title = obj.title
108
           return ' o conteúdo {}'.format(title)
109 110 111 112 113 114 115
        return ''

    def get_action_phrase(self, obj):
        verbs = {
            'access': 'acessou',
            'answered': 'respondeu',
            'created': 'criou',
Matheus Miranda's avatar
Matheus Miranda committed
116 117
            'reacted': 'reagiu a',
            'created comment': 'comentou'
118 119 120 121
        }

        action_phrase = ''

122
        action_phrase += obj.actor.name.split()[0]
123
        action_phrase += (' {}'.format(verbs.get(obj.verb)))
124 125

        if obj.action_object:
Matheus Miranda's avatar
Matheus Miranda committed
126 127 128
            action_phrase += self.get_object_type(obj.action_object)
        if obj.target:
            action_phrase += self.get_object_type(obj.target)
129 130

        return action_phrase
131 132 133

    def get_complements(self, obj):
        if obj.target:
134
            if isinstance(obj.target, LearningObject) and obj.target.unit:
Glaucia S. Santos's avatar
Glaucia S. Santos committed
135
                unit = 'a atividade {}'.format(obj.target.unit.title)
136 137 138 139 140
                step = 'na etapa {}'.format(obj.target.unit.lesson.name)
                course = 'do curso {}'.format(obj.target.unit.lesson.course.name)
                compl =  {'atividade': unit, 'etapa': step, 'curso': course}
                return compl
        return ''