serializers.py 7.11 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()

10
from courses.models import Course, Unit
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 63
            if not obj.target.unit:
                 return ''
            return obj.target.unit.lesson.course.name               
64 65
        if isinstance(obj.target, Forum):
            return obj.target.title
66 67
        if isinstance(obj.target, Classroom):
            return obj.target.name
68 69
        if isinstance(obj.target, Topic):
            return obj.target.title
70 71
        return obj.target

72 73
        return obj.target
       
74
    def get_timestamp(self, obj):
75 76 77 78
        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)
79

80 81
    def get_accessible_area_name(self, name):
        accessible_areas = {
82 83 84 85 86 87 88 89
            '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',
90
            'Cards List': 'a Lista de Conteúdos'
91
        }
92
        return accessible_areas.get(name, name)
93

94
    def get_object_type(self, obj, verb):
Matheus Miranda's avatar
Matheus Miranda committed
95
        if isinstance(obj, Course):
96
            return ' no curso {}'.format(obj.name)
97
        if isinstance(obj, Classroom):
98
            return ' na sala {}'.format(obj.name)
99
        if isinstance(obj, Topic):
100 101 102 103 104
            if verb == 'reacted':
                topic = ' ao tópico {}'
            else:
                topic = ' no tópico {}'
            return topic.format(obj.title)
Matheus Miranda's avatar
Matheus Miranda committed
105
        if isinstance(obj, Forum):
106
            return ' no fórum {}'.format(obj.title)
107
        if isinstance(obj, AccessibleArea):
108
            return ' {}'.format(self.get_accessible_area_name(obj.name))
109
        if isinstance(obj, LearningObject):
110 111 112 113 114
            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)
115
        if isinstance(obj, Card):
116 117 118 119 120 121 122
            if verb == 'reacted':
                card = ' ao conteúdo {}'
            else:
                card = ' o conteúdo {}'
            return card.format(obj.title)
        if isinstance(obj, Unit):
            return ' a unidade {} na etapa {} do curso {}'.format(obj.title, obj.lesson, obj.lesson.course)
123 124 125 126 127 128 129
        return ''

    def get_action_phrase(self, obj):
        verbs = {
            'access': 'acessou',
            'answered': 'respondeu',
            'created': 'criou',
130
            'reacted': 'reagiu',
Matheus Miranda's avatar
Matheus Miranda committed
131
            'created comment': 'comentou'
132 133 134 135
        }

        action_phrase = ''

136
        action_phrase += obj.actor.name.split()[0]
137
        action_phrase += (' {}'.format(verbs.get(obj.verb)))
138 139

        if obj.action_object:
140
            action_phrase += self.get_object_type(obj.action_object, obj.verb)
Matheus Miranda's avatar
Matheus Miranda committed
141
        if obj.target:
142
            action_phrase += self.get_object_type(obj.target, obj.verb)
143 144

        return action_phrase
145 146 147

    def get_complements(self, obj):
        if obj.target:
148
            if isinstance(obj.target, LearningObject) and obj.target.unit:
Glaucia S. Santos's avatar
Glaucia S. Santos committed
149
                unit = 'a atividade {}'.format(obj.target.unit.title)
150 151 152 153
                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
154
                    
155
        if obj.action_object and obj.target:
156 157 158 159 160 161 162
            if isinstance(obj.action_object, Card) and isinstance(obj.target, Classroom):
                if obj.verb == 'reacted':
                    content = 'ao conteúdo {}'
                else:
                    content = 'o conteúdo {}'
        
                title = content.format(obj.action_object.title)
163
                classroom = 'na sala {}'.format(obj.target.name)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
                compl = {'title': title, 'classroom': classroom, 'curso': ''}
                return compl

            if isinstance(obj.action_object, Topic) and isinstance(obj.target, Forum):
                if obj.verb == 'reacted':
                    topic = 'ao tópico {}'
                else:
                    topic = 'no tópico {}'

                forum = 'no fórum {}'.format(obj.target.title)
                compl = {'topic': topic.format(obj.action_object.title), 'forum': forum, 'curso': ''}
                return compl

        if obj.action_object:
            if isinstance(obj.action_object, Unit):
                unit = 'a unidade {}'.format(obj.action_object.title)
                step = 'na etapa {}'.format(obj.action_object.lesson)
                course = 'do curso {}'.format(obj.action_object.lesson.course)
                compl =  {'atividade': unit, 'etapa': step, 'curso': course}
183
                return compl
184
        return ''