Commit 38acf3c2 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Simplify serialization for LearningObjects

parent a1f871c8
......@@ -14,6 +14,8 @@ from courses.classroom.serializers import BasicClassroomSerializer
from discussion.models import Comment, Topic, Forum
from courses_learning_objects.models import LearningObject, Answer
from datetime import datetime
class UserActionsSerializer(serializers.Serializer):
......@@ -39,7 +41,7 @@ class GenericRelatedField(serializers.Field):
class ActionSerializer(serializers.ModelSerializer):
actor = GenericRelatedField(read_only=True)
target = GenericRelatedField(read_only=True)
target = serializers.SerializerMethodField()
action_object = GenericRelatedField(read_only=True)
verb = GenericRelatedField(read_only=True)
action_phrase = serializers.SerializerMethodField()
......@@ -49,6 +51,14 @@ class ActionSerializer(serializers.ModelSerializer):
model = Action
fields = ('actor', 'target', 'action_object', 'verb', 'action_phrase', 'timestamp')
def get_target(self, obj):
if isinstance(obj.target, LearningObject):
return obj.target.unit.lesson.course.name
if isinstance(obj.target, Forum):
print(obj.target)
return obj.target.title
return obj.target
def get_timestamp(self, obj):
timestamp_str = obj.timestamp.strftime('%d-%m-%Y %H:%M:%S')
date, hour = timestamp_str.split()
......@@ -78,6 +88,13 @@ class ActionSerializer(serializers.ModelSerializer):
return ' no fórum ' + obj.title
if isinstance(obj, AccessibleArea):
return ' na área ' + self.get_accesible_area_name(obj.name)
if isinstance(obj, Answer):
return ' respondeu'
if isinstance(obj, LearningObject):
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)
return ''
def get_action_phrase(self, obj):
......
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