2017-06-23 21:00:42 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2018-03-01 22:41:51 +00:00
|
|
|
from funkwhale_api.activity import serializers as activity_serializers
|
|
|
|
from funkwhale_api.music.serializers import TrackActivitySerializer
|
|
|
|
from funkwhale_api.users.serializers import UserActivitySerializer
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
from . import models
|
|
|
|
|
|
|
|
|
2018-03-01 22:41:51 +00:00
|
|
|
class ListeningActivitySerializer(activity_serializers.ModelSerializer):
|
|
|
|
type = serializers.SerializerMethodField()
|
|
|
|
object = TrackActivitySerializer(source='track')
|
|
|
|
actor = UserActivitySerializer(source='user')
|
2018-03-25 15:17:33 +00:00
|
|
|
published = serializers.DateTimeField(source='creation_date')
|
2018-03-01 22:41:51 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.Listening
|
|
|
|
fields = [
|
|
|
|
'id',
|
|
|
|
'local_id',
|
|
|
|
'object',
|
|
|
|
'type',
|
|
|
|
'actor',
|
|
|
|
'published'
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_actor(self, obj):
|
|
|
|
return UserActivitySerializer(obj.user).data
|
|
|
|
|
|
|
|
def get_type(self, obj):
|
|
|
|
return 'Listen'
|
|
|
|
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
class ListeningSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.Listening
|
2018-05-06 09:18:28 +00:00
|
|
|
fields = ('id', 'user', 'track', 'creation_date')
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
2018-05-06 09:18:28 +00:00
|
|
|
validated_data['user'] = self.context['user']
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
return super().create(validated_data)
|